010:计算球的体积
//斐波那契数列
#include<iostream>
using namespace std;
int main()
{
int n1 = 1,n2 = 1,k,num;
cin >> k;
if(k == 1 || k == 2)
cout << 1;
else
{
for(int i = 3;i <= k;i++)
{
num = n1 + n2;
n1 = n2;
n2 = num;
}
cout << num;
}
return 0;
}
//求阶乘的和,解法1,一般思路
#include<iostream>
using namespace std;
int main()
{
int n,n1 = 1,n2 = 0;
cin >> n;
for(int i = 1;i <= n;i++)
{
n1 = 1;
for(int j = 1;j <= i;j++)
{
n1 *= j;
}
n2 += n1;
}
cout << n2;
return 0;
}
//解法2,更快的解法
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int sum = 0;
int factorial = 1;
for(int i = 1;i <= n;++i){
factorial *= i;
sum += factorial;
}
cout << sum;
return 0;
}