提出詳細


ソースコード

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;

public class Sothnacci {
	private Vector<Integer> fibonacci;
	private Vector<Integer> sothnacci;

	public Sothnacci() {
		fibonacci = new Vector<Integer>();
		sothnacci = new Vector<Integer>();
		calcFibonacciSothnacci(46);

		// System.out.println("---finonacci---");
		// for (int i = 0; i < fibonacci.size(); i++) {
		// System.out.println((i + 1) + " > " + fibonacci.get(i));
		// }
		// System.out.println("---------------");
		// System.out.println("---sothnacci---");
		// for (int i = 0; i < sothnacci.size(); i++) {
		// System.out.println((i + 1) + " > " + sothnacci.get(i));
		// }
		// System.out.println("---------------");
	}

	// フィボナッチを計算する
	public int calcFibonacciSothnacci(int limit) {
		return calcFibonacciSothnacci(0, 1, 1, limit);
	}

	// count番目のフィボナッチを計算し、それが素数であればソスナッチに加える
	public int calcFibonacciSothnacci(int n1, int n2, int count, int limit) {
		fibonacci.add(n2);
		
		if (isPrimeNumber(n2))
			sothnacci.add(n2);

		if (count == limit)
			return n2;
		else
			return calcFibonacciSothnacci(n2, n1 + n2, count + 1, limit);
	}

	// 引数の値が素数かどうかを返す
	public boolean isPrimeNumber(int num) {
		if(num == 1) return false;
		
		// 偶数ならば2のみ素数
		if (num % 2 == 0)
			return num == 2;

		// 奇数ならば同数以外で割り切れなければ素数
		for (int i = 3; i < num; i += 2) {
			if (num % i == 0) {
				return false;
			}
		}
		return true;
	}

	// n番目のフィボナッチ数列の値を返す
	// 存在しない場合は-1
	public int getFibonacci(int n) {
		if (n >= 0 && n < fibonacci.size())
			return fibonacci.get(n);
		else
			return -1;
	}

	// n番目のソスナッチ数列の値を返す
	// 存在しない場合は-1
	public int getSothnacci(int n) {
		if (n >= 0 && n < sothnacci.size())
			return sothnacci.get(n);
		else
			return -1;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File inFile = new File("C.txt");
		BufferedReader reader = null;
		FileReader fr = null;
		File outFile = new File("result.txt");
		BufferedWriter writer = null;
		FileWriter fw = null;
		try {
			// リーダ生成
			fr = new FileReader(inFile);
			reader = new BufferedReader(fr);
			// ライター生成
			fw = new FileWriter(outFile);
			writer = new BufferedWriter(fw);

			Sothnacci sothnacci = new Sothnacci();
			
			// テストケース数を読み込み
			int testCaseNum = Integer.parseInt(reader.readLine());

			// テストケース毎に計算、出力
			for (int i = 1; i <= testCaseNum; i++) {
				String caseNumStr = "Case #" + i + ":";
				
				int n = Integer.parseInt(reader.readLine());
				StringBuilder resultBuilder = new StringBuilder();

				Integer fibo = sothnacci.getFibonacci(n-1);
				Integer soth = sothnacci.getSothnacci(n-1);
				
				resultBuilder.append(fibo == -1 ? "NG" : fibo.toString());
				resultBuilder.append(" ");
				resultBuilder.append(soth == -1 ? "NG" : soth.toString());
				
				String result = resultBuilder.toString();
				// 標準出力
				System.out.println(caseNumStr);
				System.out.println(result);

				// テキストファイルとして書き込み処理
				writer.write(caseNumStr);
				writer.newLine();
				writer.write(result);
				writer.newLine();
			}
			// テキストファイルをフラッシュ
			writer.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 各IOインスタンスのクローズ処理
			try {
				if (reader != null)
					reader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if (reader != null)
					writer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if (fr != null)
					fr.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if (reader != null)
					fw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

提出情報

提出時間 2018-07-01 01:18:26
問題 C - ソスナッチ数列
ユーザ名 soutarou1210
状態 正解
正解率 40/40
提出出力結果

テストケース情報

# 状態 詳細情報
テストケース 1 正解 詳細を見る
テストケース 2 正解 詳細を見る
テストケース 3 正解 詳細を見る
テストケース 4 正解 詳細を見る
テストケース 5 正解 詳細を見る
テストケース 6 正解 詳細を見る
テストケース 7 正解 詳細を見る
テストケース 8 正解 詳細を見る
テストケース 9 正解 詳細を見る
テストケース 10 正解 詳細を見る
テストケース 11 正解 詳細を見る
テストケース 12 正解 詳細を見る
テストケース 13 正解 詳細を見る
テストケース 14 正解 詳細を見る
テストケース 15 正解 詳細を見る
テストケース 16 正解 詳細を見る
テストケース 17 正解 詳細を見る
テストケース 18 正解 詳細を見る
テストケース 19 正解 詳細を見る
テストケース 20 正解 詳細を見る
テストケース 21 正解 詳細を見る
テストケース 22 正解 詳細を見る
テストケース 23 正解 詳細を見る
テストケース 24 正解 詳細を見る
テストケース 25 正解 詳細を見る
テストケース 26 正解 詳細を見る
テストケース 27 正解 詳細を見る
テストケース 28 正解 詳細を見る
テストケース 29 正解 詳細を見る
テストケース 30 正解 詳細を見る
テストケース 31 正解 詳細を見る
テストケース 32 正解 詳細を見る
テストケース 33 正解 詳細を見る
テストケース 34 正解 詳細を見る
テストケース 35 正解 詳細を見る
テストケース 36 正解 詳細を見る
テストケース 37 正解 詳細を見る
テストケース 38 正解 詳細を見る
テストケース 39 正解 詳細を見る
テストケース 40 正解 詳細を見る