uva299 Train Swapping
简单的不得了,就是从小到大把所有的数字放在对应的号码上需要几步就输出几步就行了
View Code
1 #include<stdio.h> 2 int main() 3 { 4 int T; 5 scanf("%d",&T); 6 while(T--) 7 { 8 int m, f[60] = {0}, i, time = 0, j, k, t, flag; 9 scanf("%d",&m); 10 for(i = 1;i <= m; i++) 11 scanf("%d",&f[i]); 12 for(i = 1;i < m;i++) 13 { 14 for(j = 1;j <= m; j++) 15 { 16 if(f[j] == i) 17 { 18 if(i == j) 19 break; 20 for(;j > i;j--) 21 { 22 t = f[j]; 23 f[j] = f[j-1]; 24 f[j-1] = t; 25 time++; 26 } 27 break; 28 } 29 } 30 } 31 printf("Optimal train swapping takes %d swaps.\n",time); 32 } 33 return 0; 34 }