#include<iostream>
using namespace std;
int main()
{
int array[21][21][21];
for (int i=0; i<=20; i++)
{
for (int j=0; j<=20; j++)
{
for (int k=0; k<=20; k++)
{
if (i == 0 || j == 0 || k == 0)
{
array[i][j][k] = 1;
}
}
}
}
for (int i=1; i<=20; i++)
{
for (int j=1; j<=20; j++)
{
for (int k=1; k<=20; k++)
{
if (i < j && j < k)
{
array[i][j][k] = array[i][j][k-1] + array[i][j-1][k-1]
- array[i][j-1][k];
}
else
{
array[i][j][k] = array[i-1][j][k] + array[i-1][j-1][k]
+ array[i-1][j][k-1] - array[i-1][j-1][k-1];
}
}
}
}
int a, b, c;
cin >> a >> b >> c;
while (!(a == -1 && b == -1 && c == -1))
{
cout << "w" << "(" << a << ", " << b << ", " << c << ")" << " = ";
if (a <= 0 || b <= 0 || c <= 0)
{
cout << "1" << endl;
}
else if (a > 20 || b > 20 || c > 20)
{
cout << "1048576" << endl;
}
else
{
cout << array[a][b][c] << endl;
}
cin >> a >> b >> c;
}
return 0;
}
一开始提交时有错,参考了一下http://www.cppblog.com/hunter/archive/2008/11/16/67046.aspx,把 while 循环的前两个 if 语句换了位置就对了。