有一个非常低效的方法,不过能算出来:
#include
#include
int main() {
int a, b, c, d, e, f;
for (a = 2; a <= 6; a++) {
for (b = 1; b <= 6; b++) {
for (c = 1; c <= 6; c++) {
for (d = 1; d <= 6; d++) {
for (e = 1; e <= 6; e++) {
for (f = 1; f <= 6; f++) {
if ((abs(a - b) == 1)
&& (d - c == 1)
&& (b - e == 2)
&& (c - f == 4)
&& (a != b)
&& (a != c) && (a != d)
&& (a != e) && (a != f)
&& (b != c) && (b != d)
&& (b != e) && (b != f)
&& (c != d) && (c != e)
&& (c != f) && (d != e)
&& (d != f) && (e != f)) {
printf("A: %d\nB: %d\nC: %d\nD: %d\nE: %d\nF: %d\n", a, b, c, d, e, f);
}
}
}
}
}
}
}
return 0;
}
结果为:
不过如果不是必要的话,非常不建议在普通程序中使用太多的循环嵌套(最好不要超过 3 个)。
#include
int main()
{
int a,b,c,d,e,f;
int i;
for(i=1;i<=6;i++){
c=i;
d=c+1;
if(d>6){
continue;
}
f=c-4;
if(f<1){
continue;
}
int j;
for(j=1;j<=6;j++){
if(j==c||j==d||j==f){
continue;
}
b=j;
e=b-2;
if(e<1||e==c||e==d||e==f){
continue;
}
a=b-1;
if(a<=1||a==c||a==d||a==f){
continue;
}
printf("A=%d B=%d C=%d D=%d E=%d F=%d\n",a,b,c,d,e,f);
}
}
return 0;
}