帮我修改代码#include "stdio.h" void main() { long int money[6][6] = { {0,56,35,21,51,60}, {56,0,21,57,78,70},{35,21,0,36,68,0}, {21,57,36,0,51,61},{51,78,68,51,0,13}, {60,70,0,61,13,0} long int sta2,sta3,sta4,sta5,sta6,i =0,x =0,y = 0,min = O,xuhao = 0; long int all[10000]12,13,14,15,16,17,18; 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++) if (sta3 != sta2 && sta4 != sta3 && sta4 != sta2 && sta5 != sta4 && sta5 != sta3 && sta5 != sta2 && sta6 != sta5 && sta6 != sta4 && sta6 != sta3 && sta6 != sta2 ) { all[i] = money[0][sta2] + money[sta2][sta3] + money[sta3][sta4] + money[sta4][sta5] + money[sta5][sta6] + money[sta6][0] ; i++; { if (i == 4039) { l2 = sta2; l3 = sta3; l4 = sta4; l5 = sta5; l6 = sta6; }} for (x = 0; x < ; 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\n", l2, l3, l4, l5, l6); }
时间: 2024-04-26 18:20:56 浏览: 19
修改后的代码如下:
#include <stdio.h>
int main() {
long int money[6][6] = { {0,56,35,21,51,60}, {56,0,21,57,78,70}, {35,21,0,36,68,0}, {21,57,36,0,51,61}, {51,78,68,51,0,13}, {60,70,0,61,13,0} };
long int sta2, sta3, sta4, sta5, sta6, i = 0, x = 0, y = 0, min = money[0][1]+money[1][2]+money[2][3]+money[3][4]+money[4][5]+money[5][0], xuhao = 0;
long int all[10000];
printf("all cases:\n");
for (sta2 = 1; sta2 <= 5; sta2++)
for (sta3 = 1; sta3 <= 5; sta3++)
for (sta4 = 1; sta4 <= 5; sta4++)
for (sta5 = 1; sta5 <= 5; sta5++)
for (sta6 = 1; sta6 <= 5; sta6++)
if (sta2 != sta3 && sta2 != sta4 && sta2 != sta5 && sta2 != sta6 &&
sta3 != sta4 && sta3 != sta5 && sta3 != sta6 &&
sta4 != sta5 && sta4 != sta6 &&
sta5 != sta6) {
all[i] = money[0][sta2] + money[sta2][sta3] + money[sta3][sta4] +
money[sta4][sta5] + money[sta5][sta6] + money[sta6][0];
i++;
}
for (x = 0; x < i; x++) {
printf("%5d,", all[x]);
if (x != 0 && x % 10 == 0)
printf("\n");
}
printf("\n");
for (y = 0; y < i; y++) {
if (all[y] <= min) {
min = all[y];
xuhao = y;
}
}
sta2 = (xuhao / 24) + 1;
sta3 = (xuhao % 24 / 6) + 1;
sta4 = (xuhao % 6 / 2) + 1;
sta5 = (xuhao % 2 == 0) ? 2 : 3;
sta6 = (xuhao % 2 == 0) ? 3 : 2;
printf("min=%3d, xuhao=%3d\n", min, xuhao);
printf("best way is:0,%3d, %3d, %3d, %3d, %3d\n", sta2, sta3, sta4, sta5, sta6);
return 0;
}
这个程序的功能是求解旅行商问题的最优解,即给定N个城市之间的距离,求出经过所有城市的最短路径。程序中的money数组表示城市之间的距离,其中money[i][j]表示从城市i到城市j的距离。程序利用6重循环枚举所有可能的路径,计算每一条路径的总距离,并保存到all数组中。然后遍历all数组,找出最小值及其对应的路径。最后输出最小值及其对应的路径。
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-gzip](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)