【AOJ】Java童貞&文系な俺が卒論そっちのけで競技プログラミングの問題といてみた。【vol0の0000~0004まで】

marugrammingの一環でAizu Online Judgeの問題をといてます。なんでこうなってるか詳細はこちら
marugrammingことはじめ - 負け犬がいろいろとかたったら
この記事は淡々と答えだけ載せます。

今回は簡単な問題5問だけ解いたので晒します。あまり難しくなさそうな問題なはずなのにクソい回答ばかりで恥ずかしいです。
でもぎーまるのみんながエレガントな解答してくれるはず!w


==

[Volume0]0000: QQ

問題

Write a program which prints multiplication tables in the following format:
リンク:QQ | Aizu Online Judge

回答
public class QQ {
    public static void main (String[] args) {
        for(int a=1; a<=9; a++){
            for(int b=1; b<=9; b++){
                System.out.printf(String.valueOf(a) + "*" + String.valueOf(b) + "=%d", a * b);
            }
        }
    }
}
感想

問題自体特にどうってことないけど掛け算を行う回数が2回じゃなくて3回、4回となっていったときfor文をネストしないで書く方法ないかなぁ。この前の「サイコロの場合分け」にも言えることだけど。

==

[Volume0]0001: List of Top 3 Hills

問題

There is a data which provides heights (in meter) of mountains. The data is only for ten mountains.
List of Top 3 Hills | Aizu Online Judge

回答
public class Top3Hills {
    static int[] hight = {1819, 2003, 876, 2840, 1723, 1673, 3776,2848,1592,922};
    public static void main (String[] args) {
        java.util.Arrays.sort(hight);
        for(int i=hight.length-1;i>hight.length-4;i--) {
            System.out.println(hight[i]);
        }
    }
}
感想

ソートを逆順に行う方法がわからなくて配列を後ろから回すという力技に。まだjavaなれしてない。
あとinputの扱いこれでいいの?ってもじゃさんと話ししてました。

==

[Volume0]0002: Digit Number

問題

Write a program which computes the digit number of sum of two integers a and b
Digit Number | Aizu Online Judge

回答
public class DigitNumber {
    public static void main (String args[]) {
        int inputA = 5;int inputB = 7;
        double i = 1;double by = 10;
        while (i>0) {
            by = Math.pow(by,i);
            if (inputA + inputB - by < 0) {
                System.out.println(i);
                break;
            }
            i++;
        }
    }
}
感想

だからinputの扱い(ry
==

[Volume0]0003: Is it a Right Triangle?

問題

Write a program which judges wheather given length of three side form a right triangle. Print "YES" if the given sides (integers) form a right triangle, "NO" if not so.
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0003

回答
public class RightTriangle {
    static int[][] input = {
        {4,3,5},{4,3,6},{8,8,8}
    };
    public static void main(String[] args) {
        for(int i=0;i<input.length;i++) {
            //念のためソートする
            java.util.Arrays.sort(input[i]);
            int a = input[i][0] * input[i][0];
            int b = input[i][1] * input[i][1];
            int c = input[i][2] * input[i][2];
            if (c==a+b) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }
    }
}
感想

三平方の定理とか懐かしい。三角形だけでなく四角形、五角形と増えていっても判定できるようにかけるとより美しいですな。
あとint a,b,cのくだりはもう少し綺麗に書けるはず。

==

[Volume0]0004: Simultaneous Equation

問題

Write a program which solve a simultaneous equation
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0004

回答
import java.math.BigDecimal;
public class Simultaneous {
    static double a = 1;static double b = 2;static double c = 3;
    static double d = 4;static double e = 5;static double f = 6;
    public static void main (String[] args) {
        BigDecimal x = new BigDecimal((b*f - e*c) / (b*d - a*e));
        BigDecimal y = new BigDecimal((a*f - d*c) / (a*e - d*b));
        BigDecimal resultX = x.setScale(4,BigDecimal.ROUND_HALF_UP);
        BigDecimal resultY = y.setScale(4,BigDecimal.ROUND_HALF_UP);
        System.out.println(resultX);
        System.out.println(resultY);
    }
}
感想

今回の中で一番酷い回答。hintを全く活かしていないという意味で。

==

全体を通しての感想

inputの扱いをどうしていいのかわかりますん。
てか俺卒論やろうな?な?