Untitled Web Page

とある高専生の割と誰得なウェブページです。

Problem 0008:Sum of 4 Integers

問題

Sum of 4 Integers

50 以下の正の整数 n を入力し、0 ~ 9 の範囲の整数 a, b, c, d の組で

a + b + c + d = n

を満たすものの組み合わせ数を出力するプログラムを作成して下さい。

例えば、n が 35 のとき、(a, b, c, d) は (8,9,9,9)、(9,8,9,9)、(9,9,8,9)、(9,9,9,8) の 4 通りですので、答えは 4 となります。

Input

複数のデータセットが与えられます。各データセットに n が1行に与えられます。入力の最後まで処理して下さい。

Output

各データセットごとに、a, b, c, d の組み合わせ個数を1行に出力して下さい。

Sample Input

35
1

Output for the Sample Input

4
4

解法:愚直

4重ループで a + b + c + d = n が成り立つとき,(解の)カウンタをインクリメントした。

#include <iostream>

using namespace std;

int main() {
   int n, ans = 0;
   while (cin >> n) {
      ans = 0;
      if (cin.eof())
         return 0;
      for (int a = 0; a <= 9; a++) {
         for (int b = 0; b <= 9; b++) {
            for (int c = 0; c <= 9; c++) {
               for (int d = 0; d <= 9; d++) {
                  if (a + b + c + d == n)
                     ans++;
               }
            }
         }
      }
      cout << ans << endl;
   }
   return 0;
}