1 #include < iostream >
2 using namespace std;
3 // Author: Tanky Woo
4 // www.wutianqi.com
5 const int _max = 10001 ;
6 // c1是保存各项质量砝码可以组合的数目
7 // c2是中间量,保存没一次的情况
8 int c1[_max], c2[_max];
9 int main()
10 { // int n,i,j,k;
11 int nNum; //
12 int i, j, k;
13
14 while (cin >> nNum)
15 {
16 for (i = 0 ; i <= nNum; ++ i) // ---- ①
17 {
18 c1[i] = 1 ;
19 c2[i] = 0 ;
20 }
21 for (i = 2 ; i <= nNum; ++ i) // ----- ②
22 {
23
24 for (j = 0 ; j <= nNum; ++ j) // ----- ③
25 for (k = 0 ; k + j <= nNum; k += i) // ---- ④
26 {
27 c2[j + k] += c1[j];
28 }
29 for (j = 0 ; j <= nNum; ++ j) // ---- ⑤
30 {
31 c1[j] = c2[j];
32 c2[j] = 0 ;
33 }
34 }
35 cout << c1[n] << endl;
36 }
37 return 0 ;
38 }
母函数模板