33,317
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
bool diff(int *a,int n) //判断a数组是否有相同元素,若无,返回true
{
for(int i=0;i<n-1;++i)
for(int j=i+1;j<n;++j)
if(a[i]==a[j])
return false;
return true;
}
int main()
{
int num[10],first;
for(int a=1;a<=9;++a)
for(int b=1;b<=9;++b)
for(int c=1;c<=9;++c)
for(int d=1;d<=9;++d)
for(int e=1;e<=9;++e)
for(int f=1;f<=9;++f)
for(int g=1;g<=9;++g)
for(int h=1;h<=9;++h)
for(int i=1;i<=9;++i)
{
num[0]=a;
num[1]=b;
num[2]=c;
num[3]=d;
num[4]=e;
num[5]=f;
num[6]=g;
num[7]=h;
num[8]=i;
if(diff(num,9))
{
first=a*100+b*10+c; //算出第一个元素,即abc
printf("%d %d %d\n",first,first*2,first*3); //可由abc得到def和ghi
}
}
}
{
num[0]=a;
num[1]=b;
num[2]=c;
num[3]=d;
num[4]=e;
num[5]=f;
num[6]=g;
num[7]=h;
num[8]=i;
first=a*100+b*10+c; //算出第一个元素,即abc
second=d*100+e*10+f;
third=g*100+h*10+i;
if(diff(num,9)&&first*3==third&&first*2==second)
{
printf("%d %d %d\n",first,second,third); //可由abc得到def和ghi
}
}
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
char number[] = "123456789";
cout<<"abc def ghi\n-----------\n";
while(next_permutation(number,number+9))
{
char buff[4];
buff[3] = 0;
strncpy_s(buff,number,3);
int abc = atoi(buff);
strncpy_s(buff,number+3,3);
int def = atoi(buff);
strncpy_s(buff,number+6,3);
int ghi = atoi(buff);
if(abc*2 ==def && abc*3==ghi)
cout<<abc<<' '<<def<<' '<<ghi<<'\n';
}
return 0;
}
/*
abc def ghi
-----------
192 384 576
219 438 657
273 546 819
327 654 981
请按任意键继续. . .
*/