C++入门必做题
1. 给定等式 A B C D E 其中每个字母代表一个数字,且不同数字对应不
D F G 同字母。编程求出这些数字并且打出这个数字的
+ D F G 算术计算竖式。
───────
X Y Z D E
其实g,f是可以算出来的,本人技术有限,用了个笨办法
下面是代码
#include <iostream>
using namespace std;
int main()
{
int a,b,c,d,e,f,g,x,y,z;
for(a=0;a<10;a++)
{
for(b=0;b<10;b++)
{
for(c=0;c<10;c++)
{
for(d=0;d<10;d++)
{
for(e=0;e<10;e++)
{
for(f=0;f<10;f++)
{
for(g=0;g<10;g++)
{
for(x=0;x<10;x++)
{
for(y=0;y<10;y++)
{
for(z=0;z<10;z++)
{
int v1=a*10000+b*1000+c*100+d*10+e;
int v2=d*100+f*10+g;
int v3=x*10000+y*1000+z*100+d*10+e;
if((v1+v2+v2)==v3)
{
if((a!=b)&&(a!=c)&&(a!=d)&&(a!=e)&&(a!=f)&&(a!=g)&&(a!=x)&&(a!=y)&&(a!=z)
&&(b!=c)&&(b!=d)&&(b!=e)&&(b!=f)&&(b!=g)&&(b!=x)&&(b!=y)&&(b!=z)
&&(c!=d)&&(c!=e)&&(c!=f)&&(c!=g)&&(c!=x)&&(c!=y)&&(c!=z)
&&(d!=e)&&(d!=f)&&(d!=g)&&(d!=x)&&(d!=y)&&(d!=z)
&&(e!=f)&&(e!=g)&&(e!=x)&&(e!=y)&&(e!=z)
&&(f!=g)&&(f!=x)&&(f!=y)&&(f!=z)
&&(g!=x)&&(g!=y)&&(g!=z)
&&(x!=y)&&(x!=z)
&&(y!=z))
{
cout<<" "<<a<<b<<c<<d<<e<<endl;
cout<<" "<<d<<f<<g<<endl;
cout<<"+ "<<d<<f<<g<<endl;
cout<<"__________________"<<endl;
cout<<" "<<x<<y<<z<<d<<e<<endl;
cin.get();
exit(1);
}
}
}
}
}
}
}
}
}
}
}
}
cin.get();
cin.get();
return 0;
}