トップ 一覧 Farm 検索 ヘルプ RSS ログイン

hana1.javaの変更点

  • 追加された行はこのように表示されます。
  • 削除された行はこのように表示されます。
 import java.util.*;  // StringTokenizer 文字で切り分け
 import java.io.*;    // BufferedReader  ファイル入出力
 
 public class hana1 {
 	public static void main(String[] args) {
 		try {
 			BufferedReader fr = new BufferedReader(new FileReader("inj.txt"));
 			PrintWriter fw = new PrintWriter(new FileWriter("outj.txt"));
 			
 			int[] card = new int[50];
 			int[] dat = new int[2];;
 			
 			while(true){
 				String str = fr.readLine();
 				
 				split(str, dat); // 文字列から空白を境に2整数(配列[0],[1])に分割
 				int n=dat[0];
 				int r=dat[1];
 				
 				if(n==0 && r==0) break;
 				
 				System.out.println("*" + n + " " + r);
 				
 				// card set
 				for(int i=0; i<n; i++){
 					card[i]=n-i;
 				}
 				print(card, n);
 				
 				// r times input
 				for(int i=0; i<r; i++){
 					str = fr.readLine();
 					split(str, dat);
 					int p=dat[0];
 					int c=dat[1];
 					System.out.println("-" + p + " " + c);
 					for(int j=p-1; j>=1; j--){
 						for(int k=0; k<c; k++){
 							int tmp=card[j+k];
 							card[j+k]=card[j+k-1];
 							card[j+k-1]=tmp;
 						}
 					}
 					print(card, n);
 				}
 				fw.println(card[0]);
 			}
 			fr.close();
 			fw.close();
 		} catch (FileNotFoundException e) {
 			System.out.println("ファイルがないよ");
 		} catch (IOException w) {
 			System.out.println("IOエラーだな");
 		}
 	}
 	
 	static void split(String s, int[] a) {
 		StringTokenizer as = new StringTokenizer(s," ");
 		if (as.countTokens() != 2) {
         	System.out.print("Input Error\n");
        		System.exit(1);
      	}
 		a[0]=Integer.parseInt(as.nextToken());
 		a[1]=Integer.parseInt(as.nextToken());
 	}
 	
 	static void print(int[] a, int n) {
 		for(int i=0; i<n; i++){
 			System.out.print(a[i]+" ");
 		}
 		System.out.println();
 	}
 }