競技プログラミング Sum of Three Integers
問題:Sum of Three Integers
https://atcoder.jp/contests/abc051/tasks/abc051_b
■2重ループ
-----------
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
int s = sc.nextInt(); // 最大値
sc.close();
int index = 0;
int x,y,z;
for (x = 0;x <= k;x++) {
for(y = 0;y <= k;y++) {
z = s - x - y; // 最大値から x と y を引いて z の数を求める。
if(0 <= z && z <= k) {
index++;
}
}
}
System.out.print(index);
}
}
■3重ループ
// 3重ループ
for(x = 0;x <= k;x++) {
for (y = 0;y <= k;y++) {
for(z = 0;z <= k;z++) {
if (x + y + z == s) {
index++;
}
}
}
}
System.out.print(index);
}