andy6804tw
10/3/2016 - 12:42 PM

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=431 這題純粹是字串翻轉不過需要一些小技巧才會AC,簡單來說就是把每個字串讀入二維

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=431

這題純粹是字串翻轉不過需要一些小技巧才會AC,簡單來說就是把每個字串讀入二維陣列,每次存入之前檢查字數是否為該行最大值,最後翻轉印出來,這裡要確認當該列數小於最大值時要印出空白不然會拋出例外

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scn=new Scanner (System.in);
		char arr[][]=new char[100][100];
		int count=0,max=0;
		while(scn.hasNext()){
			String str=scn.nextLine();
			arr[count++]=str.toCharArray();
			if(str.length()>max)
				max=str.length();
		}
		for(int i=0;i<max;i++){
			for(int j=count-1;j>=0;j--){
				if(arr[j].length<=i)
					System.out.print(" ");
				else
					System.out.print(arr[j][i]);
			}
			System.out.println();
		}
		
	}
    /*題目:Q490: Rotating Sentences
    作者:1010
    時間:西元 2016 年 10 月 */
}