アットウィキロゴ

Sum of 4 Integers

0008 : Sum of 4 Integers



解説

入力された整数nに対し、0 ~ 9の範囲の整数 a, b, c, d の組で a + b + c + d = n を満たすものの組み合わせ数を出力する。
4重ループでやるのが一番手っ取り早いんじゃないでしょうか。

プログラム

C


C++

+ ...
#include<iostream>
using namespace std;
 
int main() {
    int n, sum, count;
    while (cin >> n) {
        count = 0;
        for (int i = 0; i <= 9; ++i) {
            for (int j =0; j <= 9; ++j) {
                for (int k = 0; k <= 9; ++k) {
                    for (int l = 0; l <= 9; ++l) {
                        sum = i + j + k + l;
                        if (sum == n) {
                            ++count;
                       }
                    }
                }
            }
        }
        cout << count << endl;
    }
 
    return 0;
}

Java

最終更新:2012年12月23日 16:15