2 like 0 dislike
8.2k views

到老舊的火車站你也許會遇到少數僅存的"車箱置換員",車箱置換員是車站的員工,他的工作是重新排列火車車箱。

 

當每節車箱(尤其是裝貨的車箱)都經過適當的排列之後,火車司機在每個車站卸貨時只需從最後車箱一節一節依序放下即可。

 

"車箱置換員"的職稱起源於一座位於河畔的火車站,鐵路橫跨在河的兩岸,當船隻要通行時,橋上的鐵路不像其他地方一樣會垂直升起,而是以河中央的橋墩為中心旋轉90度,此時船隻可從橋的左右兩旁經過。

 

另外,首位車箱置換員發現一件有趣的事,當橋在作旋轉時最多可乘載兩節車箱的重量,所以當橋作180度的旋轉之後,其上的車箱就被掉換位置了,因此他就能重新排列車箱(副作用就是車箱前後位置會相反,不過因為車箱被設計成可以前後移動,所以無所謂)。

 

現在幾乎所有車箱置換員都死光了,鐵路公司必須做車箱掉換的自動化,需要寫一個程式來計算共需置換相鄰的車箱幾次才能使所有車箱依序排好,寫程式的工作就交給你了。

 

輸入說明:

第一列有一個整數表示接下來有N組測試資料,每組測試資料兩列,第一列是一個整數L,為車箱的長度,第二列為整數1~L的一種排列組合,表示車箱的起始位置,最後車箱要依照編號1到L的順序依序排好。

 

輸出說明:

每一列請輸出 'Optimal train swapping takes S swaps.',S為一整數,表示最少需要做幾次車箱置換的步驟。

 

輸入範例:

3
3
1 3 2
4
4 3 2 1
2
2 1

 

輸出範例:

Optimal train swapping takes 1 swaps.
Optimal train swapping takes 6 swaps.
Optimal train swapping takes 1 swaps.
[Exam] asked in 2017-1 程式設計(一)AD by (30k points)
ID: 38941 - Available when: 2017-12-23 09:00 - Due to: 2017-12-23 12:10
| 8.2k views

18 Answers

0 like 0 dislike
Hidden content!
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,num,i,j,k,times;

** * * ** *** ** ***** * ******* ** * *
  
* *** ** * * **** * ** * * ** * *** * ** ** * *** **** ** * ** * * *** * ** ** ** ** * ** ** * *** ** * * ** * *
   {
* *** *** * *** * *** ** * ** ** * * ***** ** ** * *** *
   }
   
int t[num],temp;
* * * * *** ** * **** **
******* **** * *** * * ** ***** ** * ** * * * * ** *
** * ***** * * ** ** **  for(k=j+1;k<num;k++)
*** * ** * * * ** * ** ** ** ** ** ** * * ** **
** ** * * * ** * * * ***** ** * ** ** * * ** *** ** ** ** ** * * * ** ** *** * * * **
* **** ** * *** **** ***** *** * **** * * * * * *** * * **** ** * *
* *** ** ***** ******* * * * ** * * * ** ** * ** * ***** ***
** * * * **** * * * *** * * ****** * ** * * ** * *** ** **** ** ** ** * **** * * ** *
** *** *** ** * **** *** ** * ** * * * ** *** ***** * * ** *** * ** ** * * * * * *** *
* * * ** ** ** ***** * * * * * * * ** ** * * * * **** ** * * * ** * * * ** * * * ***** ** * **** * * **** * *** * ** * * **** * * ** ***** * * * * * * * **
***** * * * **** * ** * ** * * * * * * ** ** ** **** * **
** ****** ******* ** **** * * * * *** ***** **
** *** * * ** * **** * * * * *** * * **** * * * **** * ***** * **** * *** * ** ** * ** * * ** * *** *** * ** * * **

    if(n==j-1)
* ***** * * ** *** ** ** *** ** **** * **** *** train swapping takes %d swaps.\n",times);
    else
** * *** * ** ** *** ** ** ** *** * * *** train swapping takes %d swaps.\n",times);
    

return 0;
}
answered by (194 points)
edited by
0 0
prog.c: In function 'main':
prog.c:5:9: error: expected expression before '%' token
   scanf(%d,&n);
         ^
prog.c:7:8: error: 'arr' undeclared (first use in this function)
     if(arr[k]<arr[k+1]){
        ^~~
prog.c:7:8: note: each undeclared identifier is reported only once for each function it appears in
prog.c:9:7: error: expected ';' before 'arr'
       arr[k]=arr[k+1]
       ^~~
prog.c:13:13: error: expected ';' before ')' token
   for(flag=1){
             ^
prog.c:13:13: error: expected expression before ')' token
prog.c:15:10: error: 'c' undeclared (first use in this function)
     swap=c[k];
          ^
prog.c:20:5: error: expected ';' before 'return'
     return 0;
     ^~~~~~
0 0
prog.c: In function 'main':
prog.c:5:7: error: array size missing in 'arr'
   int arr[];
       ^~~
prog.c:12:7: error: expected ';' before 'arr'
       arr[k]=arr[k+1]
       ^~~
prog.c:16:13: error: expected ';' before ')' token
   for(flag=1){
             ^
prog.c:16:13: error: expected expression before ')' token
prog.c:18:10: error: 'c' undeclared (first use in this function)
     swap=c[k];
          ^
prog.c:18:10: note: each undeclared identifier is reported only once for each function it appears in
prog.c:23:5: error: expected ';' before 'return'
     return 0;
     ^~~~~~
0 0
Case 0: Wrong output
Case 1: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
0 like 0 dislike
Hidden content!
#include<stdio.h>
int main()
{
    int n,i,k,j,x,count,temp;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
* ** * * ******* *** * ** * ** ** ** *
* * ** * * * * ** ** * ** ** * * ** **** * * ** * * * *** ** *
* *** ** ** * ** * ** **** ** * * **** ** * ** arr[x];
* * ****** ** ** ** * * * * **** * * ** * *** * * * * *
* ** **** * **** * *** ** * * * * * * ** ********** ** ** ** *** * ** *** * ** ** *** ****
** * * * ** * *** ********* * * * * *** ***** ** ** ** **
        {
*** *** ***** * ***** * * * ** * * * * * ** * ** ** * *** * * ** * *** * **
*** * ****** *** * ** ** *** ** ** * ***** *** ****** ******* ****
** * *** * *** * ** ** *** * ** ****** * **** * * * * ********** ** * ** * * ** **** ****** %d",j,k);
** * * *** ** ** * * * ** *** * * *** * **** *** ***** ** * * * ** * ** *** * * * * *** * **** **** ** *
**** * * *** *** *** *** ** * ***** * * * * *** * ** ** ** ** * *** *** * ** *** **** *
** * ** * ** * ** ** * *** * *** **** * ** **** ***** *** * * *** * * ** ***** ******** ** * ** ****
*** *** * * ** ** ***** ** ** ** ** * * * * * * ******* ** ** * * * **** **** **** * * * * * * * * * ** ** *
* * * * ** ** * ** * * * *** * * ******** * * *** * * **** *** ** ** * * ** * ********* *** **** * *
* ** * ** * * * * ** ** ** *** * *** * ** ** ** *** * **** ** ** * * * * * * * *** ** * * **** ****** **** * ** * * **
* *** ** ** * ** *** * * * **** **** * *** *** *** ** ** * * **** * *** * * * ** ** * **
*** ***** ** * *** *** * * * * *** * *** * * ** * **** * *
        }
* ****** ** ** ********* * * ** * ****** * * * ** train swapping takes %d swaps.\n",count);
    }
    return 0;
}
answered by (100 points)
edited by
0 0
Case 0: Wrong output
Case 1: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
0 0
Case 0: Correct output
Case 1: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>
int main() ** * *** * * ** * *** * * * *** ** ******** ** ** * ** *** ** ** * ****** * ** ** *** *** *
{
 int n,c,swap,temp,flag;
 int j,k;
 int array[10000];
******* ***** * ** **** *
 for(j=0;j<n;j++)
 {
    swap = 0;
****** ** ** ** ** *** ** ** *** * ** ** * * *
** * ** **** ** * ** ******** * * * *
    {
* * * **** * * ** * ***** **** * * ** ** * * **** * * **
    } * * * * * ** *** ** **** *** * * * ** *** * * ** * * ** ***
    flag=1;
** * *** *** * * ** *
    {
* ** **** * * ** * ** * * ******** * = 0;
* *** * ** * ** * * * *** ****** * * ****
*********** ** * **** ** ***** ****** ** **
*** *** ** ** * ** * * * ** ** ****** ****** ****** ** * * ** * ****** ******* *
* ** * * * * * ** **** ** *** ** * *** *** ** *** ***** ***
* * * **** ** * ** ** **** ** *** * ** * ***** *** ** ** * **** * * * ** * * *
* * *** *** ** **** ** * * * ** * *** ** * ** **** * ** ** **** * * ** *** ** * ** * ***** * *
* * ** * * * * *** * ** *** * * * * * * * * * * * ** * * *** * * *
* * * * ** * * * ** ** ** ** *** **** ** * ** **** * *** * * * *** * * ** ******
* *** ** ***** *** ** * * *** ** ** **** *** * * * * ** ** ** * * * ++;
* ** * **** ** * ** * * * * * * * * * ******** * * *** **
* ********* ** * * * * * * * * ** * ***
    }
* * *** * * ** *** **** **** * * train swapping takes %d swaps.\n",swap) ;
 } * * * **** * ** * ** * * ** ** * * * * **** ** ****** * * *
* *** * *** * *** ** * ** *** * *
    return 0;
}
answered by (185 points)
0 0
prog.c: In function 'main':
prog.c:34:5: warning: implicit declaration of function 'system' [-Wimplicit-function-declaration]
     system("pause");
     ^~~~~~
0 like 0 dislike
Hidden content!
#include <stdio.h>

int main ()
{
    int n,j,temp,k,z,i,sum = 0,c;
    int arr[1000]={0};
    int t[100]= {0};
** * * * * ** ** * ** ** ** * ** * ** ** * **
    for (j = 0; j < i ;j++ )
    {
*** ** * ** *** ** * * * ** * * = 0;
* * ** * ** ** ** * * *** *** * ** ******** ******* * * **
* * * ** ** ******* * ** ** * * * * ** ***** *** ** * = 0; k < n ; k++)
* ** ** ** ** * *** * ** **** *
*** *** ** ** **** * *** * *** ** *** * * ** * ** *** *** *** *** **** * * ***
* * *** ** ****** * * * ** *********** ***** ***
***** ** * **** * *** ** * * **** * ** * * * **** *** * * **** = 1;
*** * *** ** * * *** * * ***** ** ** ***** *** * * * * * *
* * ** * ** ** * * ** * **** * * * * **** *** * *** *
** * * * * * * * *** * *** * * * * * ** ** ** ** * * ** * ** * * * * ** ** * = 0;
***** * * * **** * * *** * ** ** * * *** ** * ******** ** * * * * * **** * ** * * = 0 ; c < n-1 ; c++ )
* * * * * * ** * ** * * * *** *********** ** * * ** **** ** * ** * ** * ** ** *
** * * * * ** * ** *** * * ** ******* ** * * * ** * * * ****** *** *** * *** *** * ** ****** * arr[c] > arr[c + 1] )
* ** * * * ** * ** * *** * * *** *** **** ** ** * ** * ** * *** *** * ** * ** *** * * * *
** ** * * ** * *** * **** **** ** * ** * * * * **** ** * *** * * *** * ** ***** **** *** * * * *** ********* ** = arr[c];
* **** * * * ** ******** *** *** * ** * * ** * *** * * ** * * ** ** ** ** ** * * * * ** **** * *** * ** ** * ** * ******** **** * = arr[c+1];
** *** * ***** * ***** ** * * * * ** *** * * ******* *** * * ** * * ***** *** ** *** ** * **** ** ***** = temp;
* * * * ** * * ** ******** * ****** *** ***** * ** ** * *** ** ** ***** * ** *** * * ** ** * ****** **** * ** * = 1;
** *** ** *** *** **** ** * *** * * ** * * * * *** ** * * ** * * *** * **** * * * * * * * *** ** * * *** * * *** *** * ++;
* *** * * * *** ** * * ** * * ** * * * ** **** *** ** *** * ***** * * **** * ** *** * * * * ** * **
* ** *** * * * * *** * ***** *** ** ** * * ***** * * * * ** * ** * ***** * ** * * *
* ** * *** ** * * * * ** * * *** ** *** *** * * **
* *** *** * ** **** * * * *** * sum;
    }
    for (j = 0; j < i ;j++ )
    {
* * * ** * *** ** * * * * * ** * **** * * * * ** *** ** * train swapping takes %d swaps.\n",t[j]);
    }
    return 0;
}
  
answered by (209 points)
0 0
prog.c: In function 'main':
prog.c:39:5: warning: implicit declaration of function 'system' [-Wimplicit-function-declaration]
     system("pause");
     ^~~~~~
0 0
Case 0: Correct output
Case 1: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>
int main()
{
    int n,num;
    int i,j,k,temp;
***** *** * * * ** * * *** *******
    for(i=0;i<n;i++)
    {
**** ** ** * *** * * * * **** ** * ** * *** ********* * * *
* *** *** * *** *** * *** * ** * * * ** * * * * train[num],times=0;
******* * ** * *** * ** * ** * ** ** * * * *********** *
        {
* *** *** * **** * *** *** **** *** ** * * ** * * * ** * * * ********* * * **** *
        }
* * * * *** **** ****** ** * * * ** ** * ***** * * * **** * *
* *** * *** * * *** * * * * ** * * * ** ***
** * *** ** ******* * *** ** * * * **** ** ** * * **** * * * * **
* **** * *** *** ** ** * *** * *** *** * *** * ** * * *** * ** ***********
*** * *** * * * *** * * *** *** ** * **** * ** * *** ** ** * *** * * * ** * * * * * **** *
* ** * *** ** ** * ** * ** * * * *** * *** ** ** * ** * * ** * ** ** *** *** * * *   temp=train[j];
** * * * *** ** * *** ** * *** ** **** * *** ** ******* * ** * *** * * * * * * * ** * * **** * * ** * * * **** ** ******* ***
* * * * * * *** ** * * ** * * * ** ** ** ** * * ** * * *** ****** *** * * ***** ** ** ** ****** ** ** *
*** * * ** * * ** ** ** * * *** ** *** ** ***** * *** ** * ** ** * * * ******** ** * * *
* * *** ****** ***** * * ** * ** * ***** * *** ** * ** ******* * * * * ** ** * ***** ** ** ** *** ****** ***
** * ***** * * ** * ** * * * * ** * * ***** * ** * ** * * *** * ** * * **
* ********* ** *** * * * * * * * ** ****** * ** **** ***** * ** * * *
* ** **** * *** * ******* * *** ** * ** **
****** * **** * ****** *** * * * * * ** * ** * ***
** * *** ****** ** ** * * * ** * ** **** **** * ** *** * * **** * *** * * * **** train swapping takes %d swaps.\n",times);
**** * * * * * ** ********* * *** * * * ** *
* * * ******* * ***** * *** ** *** * * *** *** * ** * ** ** *** ** train swapping takes %d swaps.\n",times);
    }
    return 0;
}
answered by (190 points)
edited by
0 0
Case 0: Wrong output
Case 1: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
0 0
Case 0: Correct output
Case 1: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>
int main()
{
    int n, c, arr[10000], temp, swap = 0, flag;
    int j, k, b;
* ** *** * * ** * **** ******* * n);
    for(j = 0;j < n;j++)
    {
* ** ** * ** *** *** * * *** * ** **** * * *** *** * *
* * ** * * * * * * ** * * ***** ****** *** * * = 0;k < c;k++)
* * **** * * *** * * ** **** * * * * ** **** ** *
** * * **** * ****** ******** ******* ** ** * * * *** * * * **** * ** * *** * **** * ******* * *** * arr[k]);
* * *** ** ** * * **** * * * * * *** ****** ***     
*** * * * * * ** * * = 1;
* * *** **** ** ** ** * * * ** * == 1)     
     {
* **** * **** * *** * * *** *** ** ** * ** ** ***** * *** *** ** ** * *** ** * = 0;k < c-1;k++)
* ** * * * * ** * ** *** ******* * ** * ** * * **** ** ** * * ** **** ** * * *** ** ***
* * * * * * * * * * * *** *** ****** ** *** ** * ****** * **** * * *** * * * **** * * ** * * ** *** ** * ***** * ** ****** **
* * ** * ****** ** ** ** * *** * * *** ** * * **** ******* * * * * *** **** ** **** ** ** ** ****** ***** * * * * *** * * **** = 0;
* ** * * * * ***** * ** ** ** * *** * * ** ** * ** **** * *** ***** **** * * * * * **** ** * = arr[k];
* *** * *** ** ** ** * ** * ** * **** ** * * ** ** ***** * * * * * * * * * * * ** ** **** ****** ** ** **** **** * * ** = arr[k++];
* ** ** ****** * ** * * ** *** ****** ** ** * * *** * * * * * ** ** **** * * ** *** * *** ******* * * * ** ** * ** ** ** = temp;
*** *** * *** * *** ** **** ** ***** ** ** * *** * ** ****** * * ** * ** * ***** * ***** * **** * * ****
* *** * * ** * * ** * * * * ******* ** ****** * * * * * * ** ** *** * **********
** * * **** * ** * ** *
** * * * ** * * ****** * train swapping takes %d swaps.\n",&swap);
     }
* * ** * * * * * * ** ** * 0;
   
}
answered by (240 points)
0 0
prog.c: In function 'main':
prog.c:6:13: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat=]
     scanf("%d", n);
             ^
prog.c:9:19: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat=]
           scanf("%d",c);
                   ^
prog.c:12:25: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat=]
                 scanf("%d", arr[k]);
                         ^
prog.c:27:45: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
      printf("'Optimal train swapping takes %d swaps.\n",&swap);
                                             ^
0 like 0 dislike
Hidden content!
#include<stdio.h>

int main(){
    int n,c,i,j,k,num,times,temp;
* ** * * * * * ** *** ** * **** * * *
    for(c=0;c<n;c++){
* * *** *** *** * * * * * * * ***
** ** * * * ** ****** *** ****** **** **** * ** **** **
       int train[num];
** * ********* ** * *** *** * ** * * ** *** *
** * * ** ** * ** * * ** * ** ** ** * **** ** ** * *** * *** * **** *** ** * * *** **
       }
** * * ***** ******* ********* * * ****
* * * * * * * * * ** * * * * * **** *** ****** * ** *** **
* ** ** * * **** * ** ** *** ** *** ** * **** ** * * * * * * * * ** **
* ** * **** * * **** ** ***** **** *** **** ** ** * ** * * *** * *** *******
** * ***** * *** * * **** *** * * * * * * * ***** **** * ** * * * *** * * ** **** * *
* * * *** * * * * * *** ** * * * * * * *** **** ** *** * * * * * * ** *
**** * ** *** ** * * * ***** * * ** * ** **** * *** * ** * ** * ** * ****** * ** * * **** ****
* ****** * ***** * * * * ** ** * ** * * * ** * * * * * ** * * * * ** * *** *****
***** ** * ** ** ** * * * ** *** * ** *** * * * *** **** ***
**** * *** * *** * * *** * ***** *** ******* * * * ** **
* ** * ** ** * ***** **** **** * *** *** * ** * *
       }
** ** * * *** ** ** * * *** ** * * * ** ** * * train swapping takes %d swaps.\n",times);
    }
* * * ** *** * * ** ** * * ***** * *
}
answered by (192 points)
0 0
Case 0: Correct output
Case 1: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,i,k,j,input,temp;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
** * **** * * * * * * ** ** * *********** count=0;
*** * * * ******* *** * * *** ****** * * * ** * * **
* ** ******* * ** * * * * * **** *** **** * array[input];
* * * * ** * * * ** ** * * ** * *** ** * * *** ** * *****
* * * ** * ** * ** ** * **** *** *** ** * *** ** * ****** **** * * * * ** * ** * **
*** ** *** ** * ** ** *** **** * *** * * ** **** ** ****
** **** * * ** * * ** ** ** * *** ** ** **
******* **** * * * ** * * * * ** ** * ** **** *** * **** ** **** * * * *
* * * *** * * **** * ** * * * * ** * * ** * ** ** **
* * * *** ** * * ** ** * ** ** * * ** * *** * ** ** ** ** * * **** * * * * **
** ***** * *** * * * * *** * * * ** * * * **** *** * * *** ** ****** ** ***
** * ** * ** * * * ** * **** * ***** * * * * * * * *** * ** *********** * ** ** ** ** * ** ** **** *
* **** *** ** *** ****** ** * *** ** ** * *** ** ** ** ** ** * **** ** * ***** * ** ** * ** ** * ** ** * ***
** ** * *** ** * ** ** *** *** *** * * *** ** * **** ** * *** * ** * * * * ** * * ****** * ** *** * ** *
** * * * ** ** * * ** * *** * * * ** ** ***** *** * *** *** * **** ******* * * *** ** * ** ** ******
*** * *** *** * ****** *** ** *** ** ** ** * * ** ** ** *** ** ******** * * * ** *** * ** ** *
** ** * * * * * *** * * ** * ********* * ** *** * ** * *** ** *
* ****** ** ** * * **** * ** * ***** * **
*** *** * ** ** * *** ** * * ** * * * * **** * train swapping takes %d swaps.\n",count);
    }
    system("pause");
    return 0;
}
answered by (114 points)
0 0
Case 0: Correct output
Case 1: Correct output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:104.23.197.64
©2016-2026

Related questions

2 like 0 dislike
5 answers
[Exam] asked Dec 23, 2017 in 2017-1 程式設計(一)AD by 楊修俊 (30k points)
ID: 38942 - Available when: 2017-12-23 09:00 - Due to: 2017-12-23 12:10
| 3.2k views
4 like 0 dislike
16 answers
[Exam] asked Dec 23, 2017 in 2017-1 程式設計(一)AD by 楊修俊 (30k points)
ID: 38940 - Available when: 2017-12-23 09:00 - Due to: 2017-12-23 12:10
| 13k views
3 like 0 dislike
16 answers
[Exam] asked Dec 23, 2017 in 2017-1 程式設計(一)AD by 楊修俊 (30k points)
ID: 38939 - Available when: 2017-12-23 09:00 - Due to: 2017-12-23 12:10
| 9.4k views
3 like 0 dislike
12 answers
[Exam] asked Dec 23, 2017 in 2017-1 程式設計(一)AD by 楊修俊 (30k points)
ID: 38938 - Available when: 2017-12-23 09:00 - Due to: 2017-12-23 12:10
| 6.6k views
2 like 0 dislike
3 answers
[Normal] Coding (C) - asked Dec 20, 2017 in 2017-1 程式設計(一)AD by 楊修俊 (30k points)
ID: 38168 - Available when: Unlimited - Due to: Unlimited
| 2.1k views
12,783 questions
183,442 answers
172,219 comments
4,824 users