2 like 0 dislike
3.4k 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.

 

[Normal] Coding (C) - asked in 2017-1 程式設計(一)AD by (30k points)
ID: 38162 - Available when: Unlimited - Due to: Unlimited
| 3.4k views

6 Answers

0 like 0 dislike
Hidden content!
* * * * * * **

int main()
{
* * ** *** int n;
* * * ** ** * int j, k, i;
* ** * int count;
* ** ** * * * int * ** *
* * * * int flag, a, temp;

* **** ** ** * ** * * ** *
*** * for(j = 0;j * n;j ++)
***** *** {
* *** * * * * * **** a = 0;
** ** ** *** ** * * *** ** *** ** * ** * *
* ** * *** * * * for(k =0 ;k < count;k ++)
* * *** * * * * *** * {
* * * * ** *** *** * * * * * * * * ** **** ** ***
** * ** * * * * }
* ****** * * * ** * ***** flag = 1;
* ***** **** **** * * ** *** ** **
****** * ** * * {
***** * * *** ** *** * ** *** * * * flag = 0;
** * *** ** *** * ** * * ** *** * for(k = 0;k *** * count - 1;k ++)
*** * *** * **** * * *** * * ** {
* * ** ** * ***** * * ** ** * * if(c[k] **** c[k + 1])
** *** * **** ** ** * * * * * * * * * *** ** * * {
**** * * ** ** ** * * * * * ** * * * * * ****** flag = 1;
* * * * **** * ** * ***** * * * * * * ***** * * * ** temp = c[k];
* ** * * * ***** * * * * *** * * ****** ***** ** * *** c[k] = c[k + 1];
*** * ** * * * * * * * **** * * *** ** * * * * * ** * c[k + 1] = temp;
*** * * ** * * *** * * * * * * ** ** * * ** * * a ++;
* *** * * * * * ** *** ** **** * *** * *** }
** **** * *** ***** ** * * ** **** * * }
** ** * * ** ** **** }
* * * ****** ** * * **
***** ** * *** * * * s[j]=a;
*** * * ** }
** ** ** * ** ** * *** * *** * train * * takes %d ** * s[0]);
** ** ** * * ** * *** *
****** * ** * * {
* * * * ** * ** * ** * * * ** ***** * * *
***** * * * * * ** * * * ** *** ** ** train swapping takes %d *** ** ** s[j]);
*** *** * ** * * * * ** *****
** * *
**** * ** return 0;
}
answered by (160 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: Correct output
Case 1: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>
void swap(int a,int b)
{
    int t=a;
    a=b;
    b=t;
}
int main()
{
    int n,num;
    int i,j,k;
** *** * * *** * * * * ** ** * ****
    for(i=0;i<n;i++)
    {
** * ** ** * * **** ** * ****** * ** * ** *** **** * *** *
*** * ** * *** * ****** **** * * * * train[num],times=0;
** * * * * * **** *** ** * *** ** * ** *** **
***** ** * * * * ** ** ** * * * ******* ** *
** ** * ** * * ** * ***** ** ** ** ** * * * * **** * * * ** * * * ** * ** *** * **
* * ** * * * *** ** * * * ***** *
* *** * *** ******* ** ** * * ** **** * * *** * *
* * * * ** * ** * ** * ** * ** *** **** ***
**** *** * * * * * * * *** * * * ****** ** * *** **** * * ** * *** *
******* * * * * **** *** * ***** ** * *** ******* ** ** *
** **** **** *** ** ***** ** ** * * ** * ** *** ** *** * *** ** * *** ** ** * ** * **** ** ** ** * * *
** * * *** ** **** *** * * ** ***** * * *** ** ** * * * * *** * ** *
*** * * * * ** * * * ** * ** * ** ** * ***** ** * * * ** ** ** * * ** * ** * *** * **** ** * * * *
** *** *** ** * * * * ** ** * * * ** ** * * ** ******* **** *** ** * * ******* * * * *** ** ** * *** *** ***
* **** * **** ** * ** * * **** **** * * * *** ** ** **** * * * * * * ** ***** ** * * *
* * ** * *** * **** *** * * **** ** * ** * * **** * ** *
* ** *** ** * ** * * ***** ** * * * ** * ** *
*** * * * * ****** * * ** * * ** ** * ****** **** *
** ** * *** * *** * ** *** **** * * * *** ** * **** * ** *** * * *** ** train swapping takes %d swaps.",times);
** * * ** * ** ** *** * ** * **** ** * * **
*** *** * * **** **** ** * * ********* ** ** ** ** * * ** ** ** *** train swapping takes %d swaps.\n",times);
    }
    return 0;
}
answered
edited by
0 0
prog.c: In function 'main':
prog.c:16:10: error: 'true' undeclared (first use in this function)
   flag = true;
          ^~~~
prog.c:16:10: note: each undeclared identifier is reported only once for each function it appears in
prog.c:19:11: error: 'false' undeclared (first use in this function)
    flag = false;
           ^~~~~
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!
***** * * ***

int main(){
*** * * int * **** *
* ***  
  ** * * ******** * * * **
*** ** ** ** *
*** * **** ** * * * *** * *
* * * * * * **** **** times=0;
** * * * * *   * * * * * * **** **
*** * * * ***** **** * * * * * int arr[num];
* * ***** * * * ****
* *** ** * * * ** ** * ** **
* * * ***** *** * * * * * * *** * * * * ** * * * *
****** *     }
* **       ** ** *** ** * **
** ***           ** * **
**** ** * * **     * * * * ****** * * * **** * *
***** *** * * ** ** * *** * * * *** ** * s=arr[i];
* * ** ** **** ** *** * * * * ** * **** * * *** * *** * * *
* * * * *** ** * * **** * ** * * ** * arr[j]=s;
* ** * ** ** * ** *** * * * * ** * times++;
** * * * ** * * * * * * ****** }
** * ** * * **** **** ** *** ** *
***** * ** ***** ** }
** *** ** ** * ** ** *** ***
*** * * ***** * * *** ** * ** * * *** * * * ** ****** *** ****** * ** **** train swapping takes %d ******** *
* ** *** * * ** * * ** ***** *** * * ** * **
*** * * ** * * * * * *** ******* * **** * * ** * *** ** * *** *** * ** * train swapping takes %d * * ****** ** *
* *** **** *** * * ** ** ***** ** train swapping takes %d ** **** ******
* ** *** * }
}
answered by (192 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: 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.",times);
*** ******* * * ** * *** * ** * * ** * *** * *** **
****** *** * ** * * *** * * * ** * ** * **** ** **** ** * *** * * * ***** train swapping takes %d swaps.\n",times);
    }
    return 0;
}
answered by (174 points)
edited by
0 0
Case 0: Correct output
Case 1: Correct output
0 0
Case 0: Correct output
Case 1: Correct output
0 like 0 dislike
Hidden content!
* * ** * ***
int main()
{
* * * * ** ** int n, c, swap, temp, flag;
* * ** * * int j, k;
** * * ** int *** **
* * *** ****** * **** * ********
* ***** ** for(j = 0;j < n;j++)
* ** *** {
** *** * ** ** ** * * swap = 0;
* ** * *** * ** *** * * ** ** *
* * *** for(k = 0;k < c;k ++)
* **** * * * * * *** * *** ** ** * * * * ** * ***
*** * * *** **** flag = 1;
**** ** ** ** ** ** * * ** * ** ==1)
* * * * ** *** *** {
* ***** * * * * * * * * ** *** flag = 0;
** ** * ** ** * * * * * * *** *** for(k = 0;k < c -1;k ++)
* ** ***** ** * * * ** *** * * * {
* * * ***** * ***** * * * * *** * * ** ** ** * ** > **** **
** ** * * * * * *** ** ** * * ** * ** *** ** {
* ** * ** *** **** * * * * * ** *** * *** * * ** * * **** flag = 1;
** * *** ** * * ** * * * *** * *** * ** ** ** * ** temp = ** *
* * * * ***** * * * ** * * * *** ** * ** * * * * ** array[k] = * ***
*** * ** * * ** ***** * * * * * *** ** ******* * * * ** * ** * = temp;
* ** **** * ***** * * * * * *** * ***** * ** * * swap++;
** * ***** *** ** ** * *** * ** * **** ***** }
* ** *** **** * * * * * }
*** * * * * * * * }
** * **** ** ** * * ** ** *** * * train swapping takes %d **** * ** * ** * * * ** **** *
** * ** **** }
* *** * **** return 0;
* * **
answered by (190 points)
edited by
0 0
prog.c: In function 'main':
prog.c:19:10: error: 'true' undeclared (first use in this function)
   flag = true;
          ^~~~
prog.c:19:10: note: each undeclared identifier is reported only once for each function it appears in
prog.c:22:11: error: 'false' undeclared (first use in this function)
    flag = false;
           ^~~~~
0 0
prog.c: In function 'main':
prog.c:19:10: error: 'true' undeclared (first use in this function)
   flag = true;
          ^~~~
prog.c:19:10: note: each undeclared identifier is reported only once for each function it appears in
prog.c:22:11: error: 'false' undeclared (first use in this function)
    flag = false;
           ^~~~~
0 0
prog.c: In function 'main':
prog.c:19:10: error: 'true' undeclared (first use in this function)
   flag = true;
          ^~~~
prog.c:19:10: note: each undeclared identifier is reported only once for each function it appears in
prog.c:22:11: error: 'false' undeclared (first use in this function)
    flag = false;
           ^~~~~
0 0
Case 0: Wrong output
Case 1: Wrong output
0 0
-----------Re-judge-----------
Case 0: Wrong output
Case 1: Wrong output
0 like 0 dislike
Hidden content!
#include<stdio.h>
void swap(int a,int b)
{
    int t=a;
    a=b;
    b=t;
}
int main()
{
    int n,num;
    int i,j,k;
* * * *** ****** * * * **** ** ** * * * *
    for(i=0;i<n;i++)
    {
** *** * *** ** ** ***** ** * **** * ******* *** * * ** * *
*** * * **** * * ** * * * ** * ** *** * train[num],times=0;
*** *** *** *** * ** **** ** *** * * ** *** ** **** ** **
** ** ** *** ** * * * ** **** * * * * *
* * ** * **** * * * **** ***** ** ** * * *** ** * ** * * ** * ** ** * * * * * * * * ** **
* ****** ***** ** * ***** * * * ** ** * ** **
*** * *** * ** *** * *** ** * **** * ** ** *** * *
* * ** * ** * ** *** * *** * * *
* * * *** * *** *** * ** ** **** **** * * * * *** ***** * * * * **** ** * *
********* * ****** ** * * * ** *** * * ** * ** **** * * *
* * ** * ** *** ** *** **** * *** * *** ** ** **** * *** *** ** ** * *** * * * ** ** * ** ** **
*** * * ** ** ** *** * ** *** * ** ** **** ** ****** *** * **** * **** **** * * *** * **
** * * * * * ** * * * * *** * * ** ** **** ** * ** *** ** ** *** * *** * * **** *** ** * ** * * ** ** * * * ** * * **
** *** ***** *** ** * * * * * *** * ** **** * * ** ** **** * * **** * * * ** ** * **** * ***** * * * ****** ****
** * * ** * * * * ** ******* ** *** *** ** * * **** * ** * ** ** * * *
** * * * * **** * ** * *** * * * * * *** ** * * ***
* * ***** * * ** ***** ***** * ** *
* *** ** * * * * * *** *** * * *** * *** *
* * * ** * * *** * ***** *** * ** ***** * * *** ** * * * * ** *** * * *** *** train swapping takes %d swaps.",times);
* ** * ** ** ** * **** ** ** ** * * * ** *
* * * ** * ** *** * * * ** * ** * ** ** * * * ** ** * **** *** * * * * ** * train swapping takes %d swaps.\n",times);
    }
    return 0;
}
answered by (30k 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.65
©2016-2026

Related questions

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
2 like 0 dislike
5 answers
[Normal] Coding (C) - asked Dec 20, 2017 in 2017-1 程式設計(一)AD by 楊修俊 (30k points)
ID: 38165 - Available when: Unlimited - Due to: Unlimited
| 3.1k views
2 like 0 dislike
7 answers
[Normal] Coding (C) - asked Dec 20, 2017 in 2017-1 程式設計(一)AD by 楊修俊 (30k points)
ID: 38159 - Available when: Unlimited - Due to: Unlimited
| 3.3k views
2 like 0 dislike
3 answers
[Normal] Essay (Open question) - asked Dec 20, 2017 in 2017-1 程式設計(一)AD by 楊修俊 (30k points)
ID: 38158 - Available when: Unlimited - Due to: Unlimited
| 1.6k views
12,783 questions
183,442 answers
172,219 comments
4,824 users