aoj volume0

ここはAOJの解答記事です。
JAVA初心者ですので、アドバイス頂けると幸いです。
003-009までの解答を載せます。

問題一覧aoj volume0

共通のファイル読み込みクラス

package levelZero;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileOpen {

	//File名の引数あり
	List<String> fileList = new ArrayList<String>();

	FileOpen(File filename){
		try {
			FileReader fr = new FileReader(filename);
			BufferedReader br = new BufferedReader(fr);
			
			//行数
			//setLength(br.readLine());
			//一行づつListにつめる
			String s;	
			while((s = br.readLine()) != null){
				fileList.add(s);
			}
			
			
			br.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	String line;
	int length;
	
	
	public int getLength(){
		return length;
	}

	
	public String getString(){
		return line;
	}
	
}


003の解答

package levelZero;

import java.io.File;
import org.apache.commons.lang3.StringUtils;

public class IsItARigthTriangle {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		FileOpen fo = new FileOpen(new File ("/home/akiko/work/eclipse/triangle.txt"));
		int[] test = new int[fo.getLength()];
		
		for(String s: fo.fileList){			
			String[] str = StringUtils.split(s," ");
		
			for(int i=0; str.length > i ; i++){
				test[i] = Integer.parseInt(str[i]);
			}
			java.util.Arrays.sort(test);
			int sum = 0;
			sum= test[0]*test[0] + test[1]*test[1];
			if(sum == test[2]*test[2]){
				System.out.println("YES");
			}else{
				System.out.println("NO");
			}
		}
	}

004の解答

package levelZero;

import java.io.File;

import org.apache.commons.lang3.StringUtils;

public class SimultaneousEquation {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		FileOpen fo = new FileOpen(new File(
				"/home/akiko/work/eclipse/simultaneous.txt"));
		double[] sample = new double[6];

		for (String s : fo.fileList) {

			String[] str = StringUtils.split(s, " ");
			for (int i = 0; str.length > i; i++) {
				sample[i] = Double.parseDouble(str[i]);
			}

			if ((sample[1] * Math.abs(sample[4]))
					+ (sample[4] * Math.abs(sample[1])) != 0) {
				double i = sample[2] * sample[4] - sample[5] * sample[1];
				double j = sample[0] * sample[4] - sample[3] * sample[1];
				System.out.printf("%.3f ",i / j );
			} else {
				double i = sample[2] * Math.abs(sample[4]) + sample[5]
						* Math.abs(sample[1]);
				double j = sample[0] * Math.abs(sample[4]) + sample[3]
						* Math.abs(sample[1]);
				System.out.printf("%.3f ",i / j);
			}

			if ((sample[0] * Math.abs(sample[3]))
					+ (sample[3] * Math.abs(sample[0])) != 0) {
				double i = sample[2] * sample[3] - sample[5] * sample[0];
				double j = sample[1] * sample[3] - sample[4] * sample[0];
				System.out.printf("%.3f\n",i / j);
			} else {
				double i = sample[2] * Math.abs(sample[3]) + sample[5]
						* Math.abs(sample[0]);
				double j = sample[1] * Math.abs(sample[3]) + sample[4]
						* Math.abs(sample[0]);
				System.out.printf("%.3f\n",i / j);
			}
		}
	}
}

005の解答

package levelZero;

import java.io.File;

import org.apache.commons.lang3.StringUtils;

public class GcdAndLcm {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		FileOpen fo = new FileOpen(new File("/home/akiko/work/eclipse/gcdlcm.txt"));

		//List<Integer> answer = new ArrayList<Integer> ();
		long[] sample = new long[2];
		for (String s : fo.fileList) {

			String[] str = StringUtils.split(s, " ");
			for (int i = 0; str.length > i; i++) {
				sample[i] = Integer.parseInt(str[i]);
			}


			int i = 1;
			long gcd = 0;
			long lcm = 0;
			if(sample[0]>sample[1]){
				while(sample[0]*i%sample[1]!=0){
					i++;
				}
				lcm=sample[0]*i;
			}else{
				while(sample[1]*i%sample[0]!=0){
					i++;
				}
				lcm=sample[1]*i;
			}
			gcd = sample[0]*sample[1] / lcm;

			StringBuilder answer = new StringBuilder();
			answer.append(gcd).append(" ").append(lcm);

			System.out.println(answer.toString());
		}

	}

}

006の解答

package levelZero;

import java.io.File;

public class ReverseSequence {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		FileOpen fo = new FileOpen(new File("/home/akiko/work/eclipse/reverse.txt"));
		
		StringBuilder sb =new StringBuilder();
		for(String o: fo.fileList){
			sb.append(o);
			sb.reverse();
		}
		
		System.out.println(sb.toString());

	}

}

007の解答

package levelZero;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class DebtHell {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		System.out.println("何週間後の返済額を出力しますか?");
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		String buf = br.readLine();
		
		int week = Integer.parseInt(buf);
		double j = 100000;
		
		for(int i = 0; i < week; i++){
			j = (j * 1.05);
		}
		j = Math.ceil(j/10000) * 10000;
		
		System.out.println((int)j);
	}

}

008

package levelZero;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

public class Sumof4Integers {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		FileOpen fo = new FileOpen(new File("/home/akiko/work/eclipse/sum.txt"));
		int[] answer = new int[4];
		Set <Integer> set;
		for(String o: fo.fileList){
			int input = Integer.parseInt(o);
			int i = 0;
			set = new HashSet<Integer> ();
			
			if(input>36){
				return;
			}
			while(i < 4){
				answer[i] = input/(4-i);
				set.add(answer[i]);
				input = input - answer[i];
				i++;
			}
			if(4 == set.size()){
				System.out.println("24");
			}else if(3 == set.size()){
				System.out.println("12");
			}else if(2 == set.size()){
				System.out.println("4");
			}else{
				System.out.println("1");
			}			
		}
	}
}


009

package levelZero;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

public class PrimeNumber {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		FileOpen fo = new FileOpen(new File("/home/akiko/work/eclipse/prime.txt"));
		
		for(String o: fo.fileList){
			Set <Integer> set = new HashSet <Integer>(); 
			int n = Integer.parseInt(o);
			for (int i = 2; i < n; i++){
				set.add(i);
				for(int j = 2; j <= i-1; j++){
					if(i%j==0){
						set.remove(i);
					}
				}
			}
			System.out.println(set.size());
		}
	}
}