標籤:acm java hdu 簡單類比 農夫山泉
To and FroTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5161 Accepted Submission(s): 3573
Problem DescriptionMo and Larry have devised a way of encrypting messages. They first decide secretly on the number of columns and write the message (letters only) down the columns, padding with extra random letters so as to make a rectangular array of letters. For example, if the message is “There’s no place like home on a snowy night” and there are five columns, Mo would write down
t o i o y
h p k n n
e l e a i
r a h s g
e c o n h
s e m o t
n l e w x
Note that Mo includes only letters and writes them all in lower case. In this example, Mo used the character ‘x’ to pad the message out to make a rectangle, although he could have used any letter.
Mo then sends the message to Larry by writing the letters in each row, alternating left-to-right and right-to-left. So, the above would be encrypted as
toioynnkpheleaigshareconhtomesnlewx
Your job is to recover for Larry the original message (along with any extra padding letters) from the encrypted one.
InputThere will be multiple input sets. Input for each set will consist of two lines. The first line will contain an integer in the range 2. . . 20 indicating the number of columns used. The next line is a string of up to 200 lower case letters. The last input set is followed by a line containing a single 0, indicating end of input.
OutputEach input set should generate one line of output, giving the original plaintext message, with no spaces.
Sample Input
5toioynnkpheleaigshareconhtomesnlewx3ttyohhieneesiaabss0
Sample Output
theresnoplacelikehomeonasnowynightxthisistheeasyoneab
SourceEast Central North America 2004
RecommendIgnatius.L | We have carefully selected several similar problems for you: 1196 1073 1161 1039 1020
比起動態規劃什麼的,這個就是水果了。因為沒有轉彎沒有坑,直來直往就行.首先用二維數組按照下面這種形式儲存.這個儲存方式是奇數行從左往右存,偶數行從右往左邊存,這應該很好理解,用一個判斷語句就可以做到.
然後按照從上到下,即外層迴圈控制列數,內層迴圈控制行數輸出就行了!
import java.io.*;import java.util.*;public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);while (input.hasNext()){int n = input.nextInt();if(n==0) break;input.nextLine();String str = input.nextLine();char c1[] = str.toCharArray(); // 把每個字元單個存起來char c2[][] = new char[1010][1010];int line = c1.length / n; //記錄行數int k = 0; // 記錄c1字元的位元for (int i = 0; i < line; i++){if (i % 2 == 1){for (int j = n - 1; j >= 0; j--){c2[i][j] = c1[k++];}} else{for (int j = 0; j < n; j++){c2[i][j] = c1[k++];}}}for (int j = 0; j < n; j++){for (int i = 0; i < line; i++){System.out.print(c2[i][j]);}}System.out.println();}}}
HDU-1200-To and Fro(Java農夫山泉+簡單類比)