ACMの過去問解いてみた!!(4)〜2005年国内予選ProblemA

とうとうACMまで10日をきったのですが、なかなかきあいが入らず、、、。
問題はこちら
大金持ちの利益を増やすやつです。
文章が複雑で読んで理解するまでで、9割完了といってもいいでしょう。

package acm2005.a;

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

public class Q {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(Q.class
				.getResourceAsStream("input.txt")));

		String line = null;
		while ((line = br.readLine()) != null) {

			int m = Integer.parseInt(line); // データセット数

ここまではいつものお決まり。
”データセット数”まで読み込み、その数の分だけループします。

			for (int i = 0; i < m; i++) {

				int found = Integer.parseInt(br.readLine()); // 初期運用資金量
				int num = Integer.parseInt(br.readLine()); // 運用年数
				int n = Integer.parseInt(br.readLine()); // 運用方法の種類数
				double operation[][] = new double[n][3]; // 運用方法

今回はデータの種類が多いので、変数をたくさん宣言します。
運用方法に関しては、利子が少数なので、とりあえずdouble型で用意します。

				for (int j = 0; j < n; j++) {
					line = br.readLine();
					String[] str2 = line.split(" ");
					for (int k = 0; k < 3; k++) {
						operation[j][k] = Double.parseDouble(str2[k]);
					}
				}

運用方法まで読み込みます。

				int total = 0;  //合計
				int max = 0; // max

				for (int j = 0; j < n; j++) {
					if (operation[j][0] == 0) {
						total = simCalc(found, num, operation[j][1],
								(int) operation[j][2]);
					} else {
						total = comCalc(found, num, operation[j][1],
								(int) operation[j][2]);
					}

					if (max < total)
						max = total;
				}
				System.out.println(max);
			}
		}
	}

ここから、実際の計算に入ります。
単利か複利かを判定し、メソッドに渡します。
運用方法の種類の数だけループさせ、返ってきた値の最大値を出力します。

//単利メソッド
	public static int simCalc(int found, int num, double interest,
			int commission) {

		// interest:利子, commission:手数料

		int balance = 0; // 利子残高

		for (int i = 0; i < num; i++) {
			balance = (int) (balance + found * interest);
			found -= commission;
		}
		return found + balance;
	}

//複利メソッド
	public static int comCalc(int found, int num, double interest,
			int commission) {

		// interest:利子, commission:手数料

		for (int i = 0; i < num; i++) {
			found = (int) (found + found * interest);
			found -= commission;
		}
		return found;
	}
}

単利、複利計算のメソッドです。


大会が近づいてきたのだか、思ったより問題を解くスピードが上がらない、、、。