主题:求助出错原因
1到9这几个数字,每三个组成一个三位数,每个数字只能用一次,要求其中两个的和等于第三个的值。求出所有的符合要求的数字
下面是我写的,编译没问题,运行时出错了,求高手帮忙看看。
#include <iostream>
#include <stdio.h>
using namespace std;
void prcs(int x,int y,int z)
{
int vd[9];
memset(vd, 0, sizeof(vd));
int tx = x, ty = y,tz=z;
while(x!=0)
{
vd[x%10]++;
x/=10;
}
while(y!=0)
{
vd[y%10]++;
y/=10;
}
while(z!=0)
{
vd[z%10]++;
z/=10;
}
for(int i=1;i<=9;i++)
if(vd[i]!=1)
return ;
cout << tx << ' ' << tx << ' '<< tz << endl;
}
int main()
{
int x,y,z;
for (int i=1; i<=9; i++)
for (int j=1; j<=9; j++) if (j != i)
for (int k=1; k<=9; k++) if (k != i && k != j)
x=i*100+j*10+k;
for (int a=1; a<=9; a++)
for (int b=1; b<=9; b++) if (b != a)
for (int c=1; c<=9; c++) if (c != a && c != b)
y=a*100+b*10+c;
for (int g=1; g<=9; g++)
for (int h=1; h<=9; h++) if (h != g)
for (int l=1; l<=9; l++) if (l != g && l != h)
z=g*100+h*10+l;
prcs(x,y,z);
return 0;
}
下面是我写的,编译没问题,运行时出错了,求高手帮忙看看。
#include <iostream>
#include <stdio.h>
using namespace std;
void prcs(int x,int y,int z)
{
int vd[9];
memset(vd, 0, sizeof(vd));
int tx = x, ty = y,tz=z;
while(x!=0)
{
vd[x%10]++;
x/=10;
}
while(y!=0)
{
vd[y%10]++;
y/=10;
}
while(z!=0)
{
vd[z%10]++;
z/=10;
}
for(int i=1;i<=9;i++)
if(vd[i]!=1)
return ;
cout << tx << ' ' << tx << ' '<< tz << endl;
}
int main()
{
int x,y,z;
for (int i=1; i<=9; i++)
for (int j=1; j<=9; j++) if (j != i)
for (int k=1; k<=9; k++) if (k != i && k != j)
x=i*100+j*10+k;
for (int a=1; a<=9; a++)
for (int b=1; b<=9; b++) if (b != a)
for (int c=1; c<=9; c++) if (c != a && c != b)
y=a*100+b*10+c;
for (int g=1; g<=9; g++)
for (int h=1; h<=9; h++) if (h != g)
for (int l=1; l<=9; l++) if (l != g && l != h)
z=g*100+h*10+l;
prcs(x,y,z);
return 0;
}