PAT 甲級 A1046 (2019/01/28)
阿新 • • 發佈:2019-02-22
長度 () name lse urn ret i++ clu include
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 const int MAXN = 100001; 5 int dis[MAXN], A[MAXN]; 6 int main(){ 7 int n, query, sum_1, sum_2, start, end; 8 scanf("%d",&n); 9 for(int i = 1; i <= n; i++) 10 scanf("%d",&dis[i]);11 12 scanf("%d",&query); 13 for(int i = 0; i < query; i++){ 14 scanf("%d%d",&start,&end); 15 if(start>end){ 16 swap(start,end); 17 } 18 //計算順時針的路徑長度 19 for(int j = start; j < end; j++){ 20 sum_1 += dis[j];21 } 22 //計算逆時針的路徑長度 23 for(int j = end; j <= n; j++){ 24 sum_2 += dis[j]; 25 } 26 for(int k = 1; k < start; k++){ 27 sum_2 += dis[k]; 28 } 29 //比較順和逆路徑長度大小 30 if(sum_1 > sum_2) 31 A[i] = sum_2;32 else 33 A[i] = sum_1; 34 sum_1 = 0; 35 sum_2 = 0; 36 } 37 for(int i = 0; i < query; i++){ 38 printf("%d\n",A[i]); 39 } 40 return 0; 41 }
PAT 甲級 A1046 (2019/01/28)