リハビリプログラムAOJ【0500】
50年ぶりぐらいにプログラミングするからまずは、リハビリからしよ!!ってことで
AOJの問題(カードゲーム | Aizu Online Judge)を解いていて
まぁ、解き始めはこんなん2時間あればかけるやろって感じだったが
最近、プログラミングに触れてなさ過ぎてほぼ完全に書き方忘れとったww
結局、間にハリーポッターを鑑賞する事をはさんで8時間ぐらいかかったwwwwwww
まぁ、なにはともあれかけたのでとりあえずのせる
これからは、たいしたコードではなくとものせていこうと思う。
(特に、ブログにのせれるネタがないということも一因であるww)
たいしたコードではないが自分のコードを客観的に見るために
いつものエクリプス上のコードとは違う雰囲気の中見ていこうと考えたからである。
ちなみに言語はjavaで書いた。
リハビリとしてはちょうどいい優しさだった
今日、残ってる時間と明日からもいろんなコードを書いていこう
プチ解説としては、ファイル名はコマンドライン引数で指定
その他の事に関しては、AOJの問題が要求している仕様である。
実行例は、上の問題のページの通りの挙動をしているが
わざわざのせるのがめんどくさいのでここでは割愛。
このコードを書いていて新しく学んだ事はBufferedReaderを用いて
入力ファイルを1行ずつ読み込んで処理できるという事を学んだ(それぐらい…笑)
という実際に得られた知識どうこう以前に自分が夏休み中にプログラミングをしているという事が
もうそれは、大きな成長なのでまぁまぁ、よいスタートだと思う。
この調子でいきたい
~FileRead.java~
package cardGame_0500;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* データの読み込みを行うクラス
* 読み込みファイル名 cardGameInput.docx
* @author seiya
*/
public class FileRead {
//配列要素数の限界は10000である(n≦10000)
private ArrayList<Integer> A = new ArrayList<Integer>();
private ArrayList<Integer> B = new ArrayList<Integer>();
/**
* @return the a
*/
public ArrayList<Integer> getA() {
returnA;
}
/**
* @return the b
*/
public ArrayList<Integer> getB() {
returnB;
}
/**
* 入力ファイルのデータを1行ずつ読み込みデータをセットするメソッド
*/
public void fileReadSet(String filename){
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(filename);
br = new BufferedReader(fr);
String line;
int i;
while*1 != null){ //1行読む
i = Integer.parseInt(line); //数字に変換
if(i == 0) break; //EoFとしての0がきたらメソッド終了
for(int k = 0; k < i; k++){
line = br.readLine();
String word = line.split(" ", 0);
A.add(Integer.parseInt(word[0]));
B.add(Integer.parseInt(word[1]));
//複数ゲーム間において間のデータにnullを代入する。
if(k == i-1){
A.add(null);
B.add(null);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return;
}
}
~Battle.java~
package cardGame_0500;
/**
* 実際にカードゲームを行い点数を競うプログラム
* @author seiya
*
*/
public class Battle {
//カードゲームの勝負の結果を格納する変数
private ArrayList<Integer> Ra = new ArrayList<Integer>();
private ArrayList<Integer> Rb = new ArrayList<Integer>();
private static FileRead file = new FileRead();
public void numComp(){
int Ap = 0, Bp = 0;
for(int i = 0; i < file.getA().size(); i++){
if(file.getA().get(i) == null){
Ra.add(Ap);
Rb.add(Bp);
Ap = 0;
Bp = 0;
continue;
}
if(file.getA().get(i) < file.getB().get(i)){
Bp += (file.getA().get(i)+file.getB().get(i));
}
if(file.getA().get(i) > file.getB().get(i)){
Ap += (file.getA().get(i)+file.getB().get(i));
}
if(file.getA().get(i) == file.getB().get(i)){
Ap += file.getA().get(i);
Bp += file.getB().get(i);
}
}
}
public void showResult(){
for(int i = 0; i < Ra.size(); i++){
System.out.println(Ra.get(i)+" "+Rb.get(i));
}
}
//コマンドライン引数にファイル名を代入
public static void main(String args){
Battle battle = new Battle();
file.fileReadSet(args[0]); //入力データの読み込み
battle.numComp();
battle.showResult();
}
}
*1:line = br.readLine(