提出詳細
ソースコード
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();
}
}
}
}
提出情報
提出出力結果
テストケース情報