1. 找出所有形如ABC*DE(三位数*两位数)的算式,使得在完整的算式中,所有的数字数与特定的数字集合,该数字集合由键盘输入,相邻数字之间没有空格,输出样例见竖式(用,表示空格)。
input:
2357
output:
<1>
,,775
X,,33
_____
,2325
2325,
_____
25575
The number of solutions =1
代码:
#include<iostream>
#include<cstdio>//使用sprintf将数字转换成字符串
#include<iomanip>//输出的宽度控制
#include<string.h>//使用strlen函数获取char型数组的实际字符个数
using namespace std;
int main()
{
string str;
cin>>str;
int cishu=0;//计数有几个竖式满足条件
for(int abc=100; abc<1000; abc++)
{
for(int de=10; de<100; de++)
{
int out1=abc*(de%10);
int out2=abc*(de/10);
int out3=abc*de;
char ch[25];//存取竖式中的所有数字
sprintf(ch,"%d%d%d%d%d",abc,de,out1,out2,out3);
int check=0;//用来标志是否竖式中的所有数字均能在输入的数字字符串中找到
for(int k=0; k<strlen(ch); k++) //一定要用实际长度
{
size_t index=str.find(ch[k],0);
if(index==string::npos)//find函数找不到某个字符或字符串就返回string::npos
{
check=1;//不执行下面的输出工作
break;
}
}
if(check==0)
{
cishu++;
cout<<"<"<<cishu<<">"<<endl;
cout<<setw(5)<<abc<<endl;
cout<<"X"<<setw(4)<<de<<endl;
cout<<"_____"<<endl;
cout<<setw(5)<<out1<<endl;
cout.setf(ios::left);//设置为数字左对齐
cout<<setw(5)<<out2<<endl;
cout<<"_____"<<endl;
cout<<out3<<endl;
}
}
}
cout<<"The number of solutions="<<cishu<<endl;
}