"Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says.
"The second problem is, given an positive integer N, we define an equation like this:
N=a[1]+a[2]+a[3]+...+a[m];
a[i]>0,1<=m<=N;
My question is how many different equations you can find for a given N.
For example, assume N is 4, we can find:
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
so the result is 5 when N is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" is the same in this problem. Now, you do it!"
Input
The input contains several test cases. Each test case contains a positive integer N(1<=N<=120) which is mentioned above. The input is terminated by the end of file.
Output
For each test case, you have to output a line contains an integer P which indicate the different equations you have found.
Sample Input
4
10
20
Sample Output
5
42
"The second problem is, given an positive integer N, we define an equation like this:
N=a[1]+a[2]+a[3]+...+a[m];
a[i]>0,1<=m<=N;
My question is how many different equations you can find for a given N.
For example, assume N is 4, we can find:
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
so the result is 5 when N is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" is the same in this problem. Now, you do it!"
Input
The input contains several test cases. Each test case contains a positive integer N(1<=N<=120) which is mentioned above. The input is terminated by the end of file.
Output
For each test case, you have to output a line contains an integer P which indicate the different equations you have found.
Sample Input
4
10
20
Sample Output
5
42
627
感叹母函数的强大,第一次接触,明白其中的道理后还是很容易用代码实现的
转载一篇小飞大佬的文章
小飞_Xiaofei
http://blog.csdn.net/xiaofei_it/article/details/17042651
这里面介绍的非常详细,看完这个后就基本上懂得原理了,那就来看一下代码实现
对于这个题1~~n都可能用到
那我要用几个1,几个2,几个n都得计算进去,所以就得有n个多项式相乘
(1 + x^1 + x^2..+x^n)(1 + x^2 + x^4 + x^6 + ...)(1 + x^3 + x^6 +..)(..)(1 + x^n)
//第一个表达式(1 + x^1 + x^2..+x^n)中 x的指数代表【解中'1'的出现次数】 比如x^2 = x^(1 * 2) 这是'1'出现了两次 x^3 = x^(1 * 3) '1'出现3次
这样明白了多项式之后就是代码实现相乘的结果了
首先得准备好一个n1的数组存放当前各个系数(结果),n2存放当前结果(首端个多项式*下一个一个多项式后的结果)(首端个多项式,前两个多项式相乘都会留下一个多项式——首端多项式(自取名))
一个循环:循环剩下的每一个多项式
更新两个数组的之,为下一次相乘做准备
一个循环:循环循环首端多项式的每一个项
一个循环:循环相乘跳跃的步数
#include<iostream>
#include<algorithm>
#include<memory.h>
using namespace std;
int main()
{
int n1[125],n2[125];
int n;
while(cin>>n)
{
// memset(n1,1,sizeof(n1));//用memset没法赋值为1???
// memset(n2,0,sizeof(n2));
// cout<<n1[n]<<endl;
for(int i = 0; i <= n; i++) //初始化 第一个表达式 目前所有指数项的系数都为1
{
n1[i] = 1;
n2[i] = 0;
}
for(int i = 2;i <= n;i++)
{
for(int j = 0;j <= n;j++)
{
for(int k = 0;j + k <= n;k += i)
{
n2[j + k] += n1[j];
}
}
for(int l = 0;l <= n;l++)
{
n1[l] = n2[l];
n2[l] = 0;
}
}
cout<<n1[n]<<endl;
}