竖式问题

问题描述:找出所有形如abc*de(三位数乘两位数)的算式,使得在完整的竖式中,所有的数字都属于一个特定的数字集合。输入数字集合(相邻数字之间没有空格),输出所有竖式。每个竖式前应有编号,之后有一个空行。最后输出解得总数。具体格式详见样例输出(为了便于观察,竖式中的空格改用小数点表示,但是所写程序中应该输出空格,而非小数点)
样例输入:2357
样例输出:

<1>
..775
X..33
-----
.2325
2325.
-----
25575

The number of solutions = 1

参考代码如下

//思路一,方法一:用数组实现
//整体思路:从所给集合中挑取三位数乘两位数,进行判断;  参考代码的思路是穷举出所有三位数乘两位数的情况在进行判断
//步骤如下:①确定abc和de--这就是那个五重循环 ②判断x,y,z是否满足要求
//注意,输入时,输完数字,要ctrl+Z 再按回车才能输入
#include<stdio.h>
int IsIn(int x, int array[], int n);
int main()
{
    int a, b, c, d, e, n = 0, count = 0, array[10];
    for(int i=0; ; i++)
    {
        if(scanf("%1d", &array[i]))  n++;
        else  break;
    }
    for(int i=0; i<n; i++)
    {
        if(array[i])  a = array[i];
        else  continue;
        for(int j=0; j<n; j++)
        {
            b = array[j];
            for(int k=0; k<n; k++)
            {
                c = array[k];
                for(int s=0; s<n; s++)
                {
                    if(array[s])  d = array[s];
                    else  continue;
                    for(int t=0; t<n; t++)
                    {
                        e = array[t];
                        int x = (100*a + 10*b + c) * e;
                        int y = (100*a + 10*b + c) * d;
                        int z = (100*a + 10*b + c) * (10*d + e);
                        if(IsIn(x, array, n) && IsIn(y, array, n) && IsIn(z, array, n))
                        {
                            printf("<%d>\n", ++count);
                            printf("%5d\nX%4d\n-----\n%5d\n%4d\n-----\n%5d\n\n", 100*a+10*b+c, 10*d+e, x, y, z);
                            /*
                            printf("  %d", 100*a+10*b+c);
                            printf("X  %d\n", 10*d+e);
                            printf("-----\n");
                            if(!x)  printf("    0\n");
                            else
                            {
                                if(x / 1000 == 0)  printf("  %d\n", x);
                                else  printf(" %d\n",x);
                            }
                            if(y / 1000 == 0)  printf(" %d\n", y);
                            else  printf("%d\n",y);
                            printf("-----\n");
                            if(z / 10000 == 0)  printf(" %d\n", z);
                            else  printf("%d\n", z);
                            printf("\n");
                            */
                        }
                    }
                }
            }
        }
    }
    printf("The number of solution is %d", count);
    return 0;
}

int IsIn(int x, int array[], int n)
{
    int split[5], flag = 0;
    split[0] = array[0];
    split[1] = array[1];
    if((x % 100) && (x / 1000 == 0))
    {
        split[2] = x / 100;  split[3] = x / 10 % 10;  split[4] = x % 10;

    }
    else if((x % 1000) && (x / 10000 == 0))
    {
        split[1] = x / 1000;  split[2] = x / 100 % 10;  split[3] = x / 10 % 10;  split[4] = x % 10;
    }
    else
    {
        split[0] = x / 10000;  split[1] = x / 1000 % 10;  split[2] = x / 100 % 10;  split[3] = x / 10 % 10;  split[4] = x % 10;
    }
    for(int i=0; i<5; i++)
    {
        flag = 0;
        for(int j=0; j<n; j++)
        {
            if(split[i] == array[j])  {flag = 1; break;}
        }
        if(flag == 0)  return 0;
    }
    return 1;
}

//思路一,方法二
//用字符串实现,由于库中有不少字符串函数,所以代码整体简洁了许多,但是总的思想不变
#include<stdio.h>
#include<string.h>
int main()
{
    char str[11], buf[25];
    scanf("%s", str);
    int n = strlen(str), a, b, c, d, e, count = 0;
    for(int i=0; i<n; i++)   //五个嵌套循环,作用是获得abcde
    {
        if(str[i] - '0')  a = str[i] - '0';  //因为str[i]是个字符型数字,要想获得原本的数字,
                                        //需要-‘0’,不减‘0’得到的是数字对应的ASCII码
        else  continue;
        for(int j=0; j<n; j++)
        {
            b = str[j] - '0';
            for(int k=0; k<n; k++)
            {
                c = str[k] - '0';
                for(int s=0; s<n; s++)
                {
                    if(str[s]-'0')  d = str[s] - '0';
                    else  continue;
                    for(int t=0; t<n; t++)
                    {
                        e = str[t] - '0';
                        int ok = 1;
                        int x = (100*a + 10*b + c) * e;
                        int y = (100*a + 10*b + c) * d;
                        int z = (100*a + 10*b + c) * (10*d + e);
                        sprintf(buf, "%d%d%d%d%d", x, y, z, 100*a+10*b+c, 10*d+e);
                        //该函数的作用是:将想向屏幕输出的内容原封不动,从地址buf开始,输入到buf[]数组中,
                        //并将内容原封不动转化为字符串,不需要转化成ASCII
                        //所以常用sprintf来把整数打印到字符串中
                        for(int m=0; m<strlen(buf); m++)
                        {//看buf中的字符(也就是竖式中出现的所有数字)是否都在str[]中
                            if(strchr(str, buf[m]) == NULL) {ok = 0; break;}
                            //strchr()作用是判断某个字符是否在字符串中
                        }
                        if(ok)
                        {
                            printf("<%d>\n", ++count);
                            printf("%5d\nX%4d\n-----\n%5d\n%4d\n-----\n%5d\n\n", 100*a+10*b+c, 10*d+e, x, y, z);
                        }
                    }
                }
            }
        }
    }
    printf("The number of solution is %d", count);
    return 0;
}



//思路二,方法一
//思路:穷举出所有三位数乘两位数的情形,判断是否符合条件
//代码很简单,就不写了

程序运行截图如下:
在这里插入图片描述

在这里插入图片描述

在强调一下本题中需要注意的地方,严格来说所给代码中思路一,方法一是不符合要求的,因为需要ctrl+z enter才能实现输入,这是因为,但用scanf()从键盘中获得位置数目的数据时(也就是用while(scanf() != EOF),windows下必须使用上述方式告诉系统输入结束。但是 采用文件输入时就比较方便了,这里只是提供一种思路,在采用数组的方法中,也锻炼了数字处理的能力
再者,就是学习了几个字符处理函数sprintf()和strchr()
sprintf(buf,"",参数表)的作用就是,将""内的内容不转化为ASCII而原样输入从buf地址开始的连续空间,并将这些内容转化为字符型,常用于把整数打印到字符串中
strchr(str, ch)作用就是在一个字符串中查找单个字符,返回第一次出现的地址

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值