C语言练习题
迴圈上的最短路徑 (營火晚會)
Time Limit: 1.0s Memory Limit: 256M
問題簡述: 營火晚會上,所有同學任選一條長度1~40不等的繩索,圍成一個圓圈以進行活動。其中一個活動為目測某位同學到
所有同學的最短距離,也就是串連該同學與其他同學的最短繩索長度。例如在下列圖中,8 位同學(編號0到7) 圍成一
圈,0號同學到2號同學距離為 20+30=50。但0號同學到4號同學最短距離為25+15+20+10=70。而0號同學所有其他同學
(0號到7號)的最短距離分別為0, 20, 30, 20, 70, 60, 40, 25。
需要完成及繳交的函式:
void distance (int n, int rope[], dist[]) 函式
- n :同學數量,編號為 0, 1,…,n − 1 。 rope [] : arr[0] 為0號同學與1號同學之間繩索長度,rope[1]為1號同學與2號同學之間繩索長度,…, arr[n − 1] 為n-1號同學與0號同學之間繩索長度。
- dist [] :函式結束時,arr[]的 應為0號同學到所有同學的最短距離。
void distance (int n, int rope[], dist[]) { //請完成並繳交本函式 }
程式樣板:
#include <stdio.h>
void distance( int n, int rope[], int dist[] );
int main( int argc, char *argv[] )
{
int a;
int n = 8;
int rope[8];
int dist[8] = {0};
for( a = 0; a < 8; a++ )
scanf( "%d", &rope[a] );
distance( n, rope, dist );
for( a = 1; a < 8; a++ )
printf( "%d ", dist[a] );
return 0;
}
void distance( int n, int rope[], int dist[] )
{
//你的程式碼
}
完整练习题及解法请下载下方文档:
C语言练习题-最短路径.c
https://download.csdn.net/download/weixin_40441194/12631675
C语言练习题-回圈上的最短路径.pdf
https://download.csdn.net/download/weixin_40441194/12631654