#include"stdio.h" #define _CRT_SECURE_NO_WARNINGS #include <cstdio> void main() { long int money[8][8] = { {0,56,35,21,51,60,43,39}, {56,0,21,57,78,70,64,49},{35,21,0,36,68,0,70,60}, {21,57,36,0,51,61,65,26},{51,78,68,51,0,13,45,62}, {60,70,0,61,13,0,53,26}, {43,64, 70,65,45,53,0,50}, {39,49,60,26,62,26, 50,0} }; long int sta2, sta3, sta4, sta5, sta6, sta7, sta8, i = 0, x = 0, y = 0, min = 0, xuhao = 0; long int all[10000], l2=0, l3=0, l4=0, l5=0, l6=0, l7=0, l8=0; printf("all case:\n"); for (sta2 = 1; sta2 <= 7; sta2++) for (sta3 = 1; sta3 <= 7; sta3++) for (sta4 = 1; sta4 <= 7; sta4++) for (sta5 = 1; sta5 <= 7; sta5++) for (sta6 = 1; sta6 <= 7; sta6++) for (sta7 = 1; sta7 <= 7; sta7++) for (sta8 = 1; sta8 <= 7; sta8++) if (sta3 != sta2 && sta4 != sta3 && sta4 != sta2 && sta5 != sta4 && sta5 != sta3 && sta5 != sta2 && sta6 != sta5 && sta6 != sta4 && sta6 != sta3 && sta6 != sta2 && sta7 != sta6 && sta7 != sta5 && sta7 != sta4 && sta7 != sta3 && sta7 != sta2 && sta8 != sta7 && sta8 != sta6 && sta8 != sta5 && sta8 != sta4 && sta8 != sta3 && sta8 != sta2) { all[i] = money[0][sta2] + money[sta2][sta3] + money[sta3][sta4] + money[sta4][sta5] + money[sta5][sta6] + money[sta6][sta7] + money[sta7][sta8] + money[sta8][0]; i++; } if (i == 4039) { l2 = sta2; l3 = sta3; l4 = sta4; l5 = sta5; l6 = sta6; l7 = sta7; l8 = sta8; } } for (x = 0; x < i; x++){ { printf("%5d%", all[x]); if (x != 0 && x % 10 == 0) printf("\n"); printf("\n"); min = all[0]; for (y = 1; y < i; y++) { if (all[y] <= min) { min = all[y]; xuhao = y; } } printf("min=%3d, xuhao=%3d\n", min, xuhao); printf("best way is:0,%3d, %3d, %3d, %3d, %3d,% 3d,%3d\n", l2, l3, l4, l5, l6, l7, l8); }
时间: 2024-03-09 21:50:33 浏览: 142
#包括stdio.h int main() char s81,c; int i,number=0,word=
根据您提供的代码,我发现其中存在一些错误:
1. `void main()` 应该改为 `int main()`,因为 `main()` 函数应该返回一个整型值。
2. 在 `printf("%5d%", all[x]);` 这行代码中,`%` 符号应该只有一个,表示格式控制符的开始和结束,应该改为 `printf("%5d", all[x]);`。
3. 在 `printf("best way is:0,%3d, %3d, %3d, %3d, %3d,% 3d,%3d\n", l2, l3, l4, l5, l6, l7, l8);` 这行代码中,`% 3d` 中的空格不应该存在,应该改为 `%3d`。
另外,您的代码中存在一些逻辑错误。在第一个 `for` 循环结束后,`sta2` 到 `sta8` 的值都将是 8,因此在最后的 `if` 语句中判断 `i` 是否等于 4039 的操作将永远不会被执行。您需要将最后的 `if` 语句放置在第一个 `for` 循环的内部,以便在每次迭代结束后判断条件。
修改后的代码如下所示:
```
#include <cstdio>
#define _CRT_SECURE_NO_WARNINGS
int main() {
long int money[8][8] = {
{0, 56, 35, 21, 51, 60, 43, 39},
{56, 0, 21, 57, 78, 70, 64, 49},
{35, 21, 0, 36, 68, 0, 70, 60},
{21, 57, 36, 0, 51, 61, 65, 26},
{51, 78, 68, 51, 0, 13, 45, 62},
{60, 70, 0, 61, 13, 0, 53, 26},
{43, 64, 70, 65, 45, 53, 0, 50},
{39, 49, 60, 26, 62, 26, 50, 0}
};
long int sta2, sta3, sta4, sta5, sta6, sta7, sta8, i = 0, x = 0, y = 0, min = 0, xuhao = 0;
long int all[10000], l2 = 0, l3 = 0, l4 = 0, l5 = 0, l6 = 0, l7 = 0, l8 = 0;
printf("all case:\n");
for (sta2 = 1; sta2 <= 7; sta2++) {
for (sta3 = 1; sta3 <= 7; sta3++) {
for (sta4 = 1; sta4 <= 7; sta4++) {
for (sta5 = 1; sta5 <= 7; sta5++) {
for (sta6 = 1; sta6 <= 7; sta6++) {
for (sta7 = 1; sta7 <= 7; sta7++) {
for (sta8 = 1; sta8 <= 7; sta8++) {
if (sta3 != sta2 &&
sta4 != sta3 && sta4 != sta2 &&
sta5 != sta4 && sta5 != sta3 && sta5 != sta2 &&
sta6 != sta5 && sta6 != sta4 && sta6 != sta3 && sta6 != sta2 &&
sta7 != sta6 && sta7 != sta5 && sta7 != sta4 && sta7 != sta3 && sta7 != sta2 &&
sta8 != sta7 && sta8 != sta6 && sta8 != sta5 && sta8 != sta4 && sta8 != sta3 && sta8 != sta2) {
all[i] = money[0][sta2] + money[sta2][sta3] + money[sta3][sta4] +
money[sta4][sta5] + money[sta5][sta6] + money[sta6][sta7] + money[sta7][sta8] + money[sta8][0];
i++;
if (i == 4039) {
l2 = sta2;
l3 = sta3;
l4 = sta4;
l5 = sta5;
l6 = sta6;
l7 = sta7;
l8 = sta8;
}
}
}
}
}
}
}
}
}
for (x = 0; x < i; x++) {
printf("%5d", all[x]);
if (x != 0 && x % 10 == 0) {
printf("\n");
}
}
printf("\nmin=%3d, xuhao=%3d\n", all[0], 0);
for (y = 1; y < i; y++) {
if (all[y] <= min) {
min = all[y];
xuhao = y;
}
}
printf("min=%3d, xuhao=%3d\n", min, xuhao);
printf("best way is:0,%3d, %3d, %3d, %3d, %3d,%3d,%3d\n", l2, l3, l4, l5, l6, l7, l8);
return 0;
}
```
希望以上信息能够帮助到您!
阅读全文