题目:
找出所有形如abc*de(三位数乘以两位数)的算式,使得在完整的竖式中,所有数字都属于一个特定的数字集合。输入数字集合(相邻数字之间没有空格),输出所有竖式。每个竖式前应有编号,之后应有一个空行。最后输出解的总数。具体格式见样例输出(为了便于观察,竖式中的空格改用小数点显示,但你的程序应该输出空格,而非小数点)。
样例输入:2357
样例输出:
<1>
..775
X..33
.—–
.2325
.—–
25575
The number of solutions = 1
分析:
尝试所有的abc和de,判断是否满足条件。
#include<iostream> #include<string.h> #include<cstdio> using namespace std; int main() { char s[15],f[25]; int n=1; cin>>s; for(int a=100;a<1000;a++) { for(int b=10;b<=99;b++) { int o=1; int x=a*(b%10),y=a*(b/10),z=a*b; sprintf(f,"%d%d%d%d%d",a,b,x,y,z);//将abxyz输入到f中 for(int i=0;i<strlen(f);i++) if(strchr(s,f[i])==NULL) o=0; if(o) printf("<%d>\n%5d\nX%4d\n-----\n%5d\n%4d \n-----\n%5d\n\n",n++,a,b,x,y,z); } } cout<<"The number of solutions = "<<n-1<<endl; return 0; }