看得都郁闷
俺发表第2题的答案:
#include<stdio.h>
void main( )
{int a,b,c,d,e,i=0,j=0,k=0,l=0,m=0;
for(a=0;a<=1;a++)
for(b=0;b<=1;b++)
for(c=0;c<=1;c++)
for(d=0;d<=1;d++)
for(e=0;e<=1;e++)
{if(a)
{if(b==1)
i++;
else i--;};
if(b+c==1)
j++;
if(c+d!=1)
k++;
if(d+e>=1)
l++;
if(e)
{if(a+d==2)
m++;
else m--;
};
if((i>=0)&&(j==1)&&(k==1)&&(l==1)&&(m>=0))
printf("%d %d %d %d %d \n",a,b,c,d,e);
else
{i=0;j=0;k=0;l=0;m=0;};
};
}
结果c和d参加
受23楼的启发,修该了一下我第二题的程序,并修饰了输出语句,程序如下:
main()
{int p[5],i,j=0,k=0;
char player[5]={'A','B','C','D','E'};
clrscr();
for(p[0]=0;p[0]<=1;p[0]++)
{for(p[1]=0;p[1]<=1;p[1]++)
{for(p[2]=0;p[2]<=1;p[2]++)
{for(p[3]=0;p[3]<=1;p[3]++)
{for(p[4]=0;p[4]<=1;p[4]++)
{if(p[0] && !p[1]) continue;
if(!(p[1]+p[2]) || (p[1]*p[2])) continue;
if((p[2]+p[3]) == 1) continue;
if(!(p[3]+p[4])) continue;
if(p[4] && (!p[0] || !p[3])) continue;
k=1;
for(i=0;i<5;i++)
{if(p[i])
{if(j == 0) {printf("Only %c",player[i]);j=j+1;}
else {printf(" and %c",player[i]);j=j+1;}}
}
if(j) {printf(" attende the competition!\n");j=0;}
}
}
}
}
}
if(!k) printf("Nobody attende the competition!");getch();
}
刚把第一题也简化了一下:
#define NUM 10
main()
{int a,b,c,d,e,f,g,x,y,z,inde[NUM]={-1,1,1,1,1,1,1,1,1,1};
clrscr();
/* 先做循环,把所有可能排列组合一下,有点头晕了,inde[NUM]作为指示记号,如果某数字i已经被赋过值,那么
inde[i]=0,那循环中将判断不再将i赋给其他变量*/
for(a=1;a<9;a++)
{x=a+1; /* 首先做了个判断,x必定等于a+1,小作弊了下,呵呵*/
inde[a]=0;inde[x]=0;
for(b=0;b<=9;b++)
{if(inde[b]) {inde[b]=0;}
else continue;
for(c=0;c<=9;c++)
{if(inde[c]) {inde[c]=0;}
else continue;
for(d=0;d<=9;d++)
{if(inde[d]) {inde[d]=0;}
else continue;
for(e=0;e<=9;e++)
{if(inde[e]) {inde[e]=0;}
else continue;
for(f=0;f<=9;f++)
{if(inde[f]) {inde[f]=0;}
else continue;
for(g=0;g<=9;g++)
{if(inde[g]) {inde[g]=0;}
else continue;
for(y=0;y<=9;y++)
{if(inde[y])
{inde[y]=0;
z=1+2+3+4+5+6+7+8+9-a-b-c-d-e-f-g-x-y;
if((a*10000+b*1000+c*100+d*10+e+2*(d*100+f*10+g)) ==
(x*10000+y*1000+z*100+d*10+e)) /*比较条件*/
{printf(" A B C D E\n");
printf(" D F G\n");
printf(" + D F G\n");
printf("-----------\n");
printf(" X Y Z D E\n");
printf("\n");
printf("%2d%2d%2d%2d%2d\n",a,b,c,d,e);
printf(" %2d%2d%2d\n",d,f,g);
printf(" + %2d%2d%2d\n",d,f,g);
printf("-----------\n");
printf("%2d%2d%2d%2d%2d\n",x,y,z,d,e);
printf("\n");
printf("a =%2d,b =%2d,c =%2d,d =%2d,e =%2d,f =%2d,g =%2d,x =%2d,y =%
2d,z =%2d\n",a,b,c,d,e,f,g,x,y,z);/*这是输出部分*/
getch();
}
}
else continue;
inde[y]=1; /* 记得要还原inde[y]=1哦*/
}
inde[g]=1;
}
inde[f]=1;
}
inde[e]=1;
}
inde[d]=1;
}
inde[c]=1;
}
inde[b]=1;
}
inde[a]=1;
}