题意
找出形如 abc*de (三位数乘以两位数) 的算式,使得在完整的竖式中,所有数字属于一个特定的数字集合。输入数字集合 (相邻数字之间没有空格),输出所有竖式。每个竖式前应有编号,之后应有一个空行。最后输出解的总数。
样例输入:
2357
样例输出:
The number of solutions = 1
题意分析
意思就是在输入的那几个数中,选出几个数来可以组成三位数*两位数的竖式
此处直接上大神的代码
CODE:
#include<stdio.h>
#include<string.h>
int main()
{
char s[20], buf[99];
int count = 0;
scanf("%s", &s);
for (int abc = 111; abc <= 999; abc++)
{
for (int de = 11; de <= 99; de++)
{
int x = abc*(de % 10);
int y = abc*(de / 10);
int z = abc * de;
sprintf(buf, "%d%d%d%d%d", abc, de, x, y, z);
int ok = 1;
for (int i = 0; i < strlen(buf); i++)
if (strchr(s, buf[i]) == NULL)
ok = 0;
if (ok)
{
printf("<%d>\n", ++count);
printf("%5d\nX%4d\n-----\n%5d\n%4d\n-----\n%5d\n\n", abc, de, x, y, z);
}
}
}
printf("The numbver of solutions = %d\n", count);
return 0;
}
代码很好理解,就是把每个数遍历一遍,如果每一个数字都是那个数字集合中的数,那么就输出。
其中有两个函数,注意一下
sprntf:输入到字符串,可用于int->char
strchr:在一个字符串中查找一个字符。=返回指向第一次出现此字符位置的指针,如果没找到则返回NULL。