写一个函数int foo(int n),对于给定的n,求出对于任意{x,y,z}>=0,1x+2y+5z=n,组合数
#include<iostream>
using namespace std;
int foo1(int n)
{
int count=0;
for(int z =0 ; z <= n/5; z++)
{
for(int y = 0; y <= (n - 5*z)/2; y++)
{
for(int x = 0; x <= (n-5*z-2*y); x++)
{
count++;
}// 这个循环可以替换成count += n-5*z-2*y + 1
}
}
return count;
}
int foo2(int n)
{
int count = 0;
for(int z = 0; z <= n/5; z++)
{
for(int y = 0; y <= (n - 5*z)/2; y++)
{
count += n-5*z-2*y + 1;
}//这个循环可以替换,y是递增数列
}
return count;
}
int foo3(int n)
{
int count = 0;
for(int z = 0; z <= n/5; z++)
{
count += (n - 5*z + 2)*(n - 5*z + 2)/4;
}
return count;
}
int main()
{
cout << foo1(10) << endl;
cout << foo2(10) << endl;
cout << foo3(10) << endl;
system("pause");
return 0;
}
using namespace std;
int foo1(int n)
{
int count=0;
for(int z =0 ; z <= n/5; z++)
{
for(int y = 0; y <= (n - 5*z)/2; y++)
{
for(int x = 0; x <= (n-5*z-2*y); x++)
{
count++;
}// 这个循环可以替换成count += n-5*z-2*y + 1
}
}
return count;
}
int foo2(int n)
{
int count = 0;
for(int z = 0; z <= n/5; z++)
{
for(int y = 0; y <= (n - 5*z)/2; y++)
{
count += n-5*z-2*y + 1;
}//这个循环可以替换,y是递增数列
}
return count;
}
int foo3(int n)
{
int count = 0;
for(int z = 0; z <= n/5; z++)
{
count += (n - 5*z + 2)*(n - 5*z + 2)/4;
}
return count;
}
int main()
{
cout << foo1(10) << endl;
cout << foo2(10) << endl;
cout << foo3(10) << endl;
system("pause");
return 0;
}