<SDUTOJ>2050王小二切饼

<标签算法是递推..这题还用递推?>

题目描述:

王小二自夸刀工不错,有人放一张大的煎饼在砧板上,问他:“饼不许离开砧板,切n(1<=n<=100)刀最多能分成多少块?”

输入:

输入切的刀数n。

输出:

输出为切n刀最多切的饼的块数。

数学做法:(看一眼100和5050+1就能推出来(n*n+1)/2+1)

1 #include<cstdio>
2 using namespace std;
3 int main(){
4     int n;
5     scanf("%d",&n);
6     printf("%d",(n*n+n)/2+1);
7     return 0;
8 }

递推算法:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<string>
 6 #include<cmath>
 7 using namespace std;
 8 int f(int x){
 9     if(x==1){
10         return 2;
11     }if(x==2){
12         return 4;
13     }
14     return f(x-1)+x;
15 }
16 int main(){
17     int n;
18     cin>>n;
19     cout<<f(n);
20     return 0;
21 }

猜你喜欢

转载自www.cnblogs.com/Fylsea/p/9851923.html