競プロ・数学を頑張りたい(願望)

競技プログラミングの問題を解いたときや数学に関してのメモにしようと思っています。競プロはAOJを、数学は数検準1を目標で。

AOJ0008 Sum of 4 Integers

コメント

4重のfor文で若干見づらいが、実行時間はO(10^4)なので十分早い。
やってることはすごく単純で、0+0+0+0から10+10+10+10を計算して順番に調べていくだけ。

ソース

public class Main {

	void run() {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int n = sc.nextInt();
			int count = 0;

			for (int i = 0; i < 10; i++) {
			  for (int j = 0; j < 10; j++) {
				for (int k = 0; k < 10; k++) {
				  for (int l = 0; l < 10; l++) {
					if (n == i + j + k + l) {
					  count++;
					}
				  }
				}
			  }
			}
			System.out.println(count);
		}
		sc.close();
	}

	public static void main(String[] args) {
		new Main().run();
	}