竖式表达式问题《算法竞赛入门经典》:找出所有形式如abc*de(三个数乘以两位数)的算式,使得在完整的竖式中
,所有数字都属于一个特定的数字集合,输入数字集合(相邻数字之间没有空
格),输出所有竖式,每个竖式前应有编号,之后应有一个空行。最后输出解
的总数。具体格式见样列输出(为了便于观察,竖式中的空格改用小数点显示
,但实际的程序应该输出空格,而非小数点)。
样例输入;2357
样例输出;
<1>
775
X.33
____
2325
2325
____
25575
The number of solutions=1
CODE:
#include <stdio.h>
#include <string.h>
int main()
{
int i, ok, abc, de, x, y, z, count=0;
char s[20], buf[99];
scanf("%s", s);
for (abc = 111; abc <= 999; abc++) //乘数3位
for (de = 11; de <= 99; de++) {//被乘数2位
x = abc*(de%10); //乘数与被乘数的个位的乘积保存到x
y = abc*(de/10); //乘数与被乘数的十位的乘积保存到y
z = abc*(de); //总结果
sprintf(buf, "%d%d%d%d%d", abc, de, x, y, z); //输出到buf字符串
ok = 1;
for (i = 0; i < strlen(buf); i++)
if (strchr(s, buf[i]) == NULL) {//查看buf[i]是否属于s集合
ok = 0; //buf[i]如果不属于s,设置标志
break; //跳出,不再检查后面的
}
if (ok) { //如果找到,则输出
printf("<%d>\n", ++count);
printf("%5d\n", abc);
printf("X%4d\n", de);
printf("-------\n");
printf("%5d\n", x);
printf("%4d\n", y);
printf("-------\n");
printf("%5d\n", z);
}
}
printf("The number of solution %d\n", count);
return 0;
}
示例:
输入:2357
输出:
<1>
775
X 33
-------
2325
2325
-------
25575
The number of solution 1
^_^ THE END