很老的题目了,但是看到一种方法,非递归,贪心算法,遍历所有的可能的组合,并打印所有的步骤。
void Calc_floors() { int n=1; for (int X=0; X <=20; X++) { for (int Y=0; Y <=10; Y++) { for (int Z=0; Z <7; Z++) { if (X + 2*Y + 3*Z == 20) cout << n++ << ": 1 per-" << X << " steps; 2 per-" << Y << " steps; 3 per-" << Z << " steps\n "; } } } cout << "Sum is: " << n << " ways!" <<endl; }