void PrintfAMGraph(AMGraph G) { G.vexnum=22; G.arcnum=29; for(int i=1;i<=G.vexnum;i++) { for(int j=1;j<=G.vexnum;j++) { if(i=j) G.arcs[i][j]=0; else G.arcs[i][j]=MaxInt; } } G.arcs[1][2]=45;G.arcs[1][3]=50; G.arcs[2][4]=200; G.arcs[3][5]=100; G.arcs[4][6]=250; G.arcs[5][6]=30; G.arcs[6][7]=100;G.arcs[6][8]=80;G.arcs[6][9]=180; G.arcs[7][8]=100; G.arcs[9][10]=300;G.arcs[9][11]=170;G.arcs[9][17]=320; G.arcs[10][12]=120; G.arcs[11][12]=250;G.arcs[11][13]=80; G.arcs[12][16]=220; G.arcs[13][14]=100;G.arcs[13][15]=150;G.arcs[13][16]=200;G.arcs[13][22]=550; G.arcs[14][15]=100;G.arcs[14][22]=480; G.arcs[15][16]=100; G.arcs[17][18]=50; G.arcs[18][19]=50; G.arcs[19][20]=50; G.arcs[20][21]=400; G.arcs[21][22]=300; for(int i=1;i<=22;i++) { for(int j=1;j<=22;j++) { G.arcs[j][i]=G.arcs[i][j]; } } for(int i=1;i<=G.vexnum;i++) { printf("v");printf("%2d",i);printf(":"); for(int j=1;j<=G.vexnum;j++) { if(G.arcs[i][j]==0) printf("%-5s","∞"); else printf("%-3d",G.arcs[i][j]); printf(" "); } printf("\n"); } } 这段代码怎么对齐输出?
时间: 2023-10-10 19:14:03 浏览: 38
你可以使用制表符 `\t` 来对齐输出。例如,可以在每次输出时使用 `printf("\t")` 来输出一个制表符,使输出向右对齐。以下是修改后的代码:
```
void PrintfAMGraph(AMGraph G) {
G.vexnum = 22;
G.arcnum = 29;
for (int i = 1; i <= G.vexnum; i++) {
for (int j = 1; j <= G.vexnum; j++) {
if (i = j)
G.arcs[i][j] = 0;
else
G.arcs[i][j] = MaxInt;
}
}
G.arcs[1][2] = 45;
G.arcs[1][3] = 50;
G.arcs[2][4] = 200;
G.arcs[3][5] = 100;
G.arcs[4][6] = 250;
G.arcs[5][6] = 30;
G.arcs[6][7] = 100;
G.arcs[6][8] = 80;
G.arcs[6][9] = 180;
G.arcs[7][8] = 100;
G.arcs[9][10] = 300;
G.arcs[9][11] = 170;
G.arcs[9][17] = 320;
G.arcs[10][12] = 120;
G.arcs[11][12] = 250;
G.arcs[11][13] = 80;
G.arcs[12][16] = 220;
G.arcs[13][14] = 100;
G.arcs[13][15] = 150;
G.arcs[13][16] = 200;
G.arcs[13][22] = 550;
G.arcs[14][15] = 100;
G.arcs[14][22] = 480;
G.arcs[15][16] = 100;
G.arcs[17][18] = 50;
G.arcs[18][19] = 50;
G.arcs[19][20] = 50;
G.arcs[20][21] = 400;
G.arcs[21][22] = 300;
for (int i = 1; i <= 22; i++) {
printf("v");
printf("%2d", i);
printf(":");
for (int j = 1; j <= G.vexnum; j++) {
if (G.arcs[i][j] == 0)
printf("%-5s", "∞");
else
printf("%-3d", G.arcs[i][j]);
printf("\t"); // 添加一个制表符
}
printf("\n");
}
}
```
这样可以使输出对齐,每个数字之间相隔一个制表符的位置。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)