【openjudge 计算概论(A)】[函数递归练习(1)]
1:数组逆序重放
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[110],n;
int main()
{
int i;
scanf("%d",&n);
for(i=1;i<=n;++i) scanf("%d",&a[i]);
for(i=n;i>0;--i) printf("%d ",a[i]);
return 0;
}
2:逆波兰表达式
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
char s[1010][10];
double d[1010];
int td,t;
double do1(double a,double b,char c1)
{
if (c1=='+') return a+b;
if (c1=='-') return a-b;
if (c1=='*') return a*b;
if (c1=='/') return a/b;
return 0;
}
int main()
{
int i;
while (cin>>s[++t]);
t--;
while (t>0)
{
if (s[t][0]>='0'&&s[t][0]<='9')
{
d[++td]=atof(s[t]);
t--;
}
else
{
double x;
x=do1(d[td],d[td-1],s[t][0]);
td--;
d[td]=x;
t--;
}
}
printf("%lf\n",d[1]);
return 0;
}
3:放苹果
#include<cstdio>
#include<cstring>
using namespace std;
int f[15][15],n,m,t;
int main()
{
int i,j,k;
scanf("%d",&t);
for (k=1;k<=t;k++)
{
scanf("%d%d",&n,&m);
f[0][0]=1;
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
if (i>=j)
f[i][j]=f[i-1][j-1]+f[i-j][j];
for (i=1;i<m;i++)
f[n][m]+=f[n][i];
printf("%d\n",f[n][m]);
memset(f,0,sizeof(0));
}
}
4:排队游戏
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct chuan{
int x,op;
}a[110];
int top,len;
char s[110],c;
int main()
{
int i;
scanf("%s",s);
len=strlen(s);
c=s[0];
for (i=0;i<len;++i)
{
if (s[i]==c) a[++top].x=i,a[top].op=1;
else
{
if(a[top].op==1) printf("%d %d\n",a[top--].x,i);
else a[++top].op=2,a[top].x=i;
}
}
return 0;
}
#include<cstdio>
#include<algorithm>
using namespace std;
char ch[70];
int a[70],n,ans;
int find(int l,int r)
{
if(l==r) return a[l];
int sum=0;
for(int i=l;i<r;++i)
if(ch[i]=='+') sum=max(sum,find(l,i)+find(i+1,r));
else
if(ch[i]=='*') sum=max(sum,find(l,i)*find(i+1,r));
return sum;
}
int main()
{
int i;
scanf("%d",&n);
for(i=1;i<=n;++i) scanf("%d %c",&a[i],&ch[i]),a[n+i]=a[i],ch[n+i]=ch[i];
for(i=1;i<=n;++i) ans=max(ans,find(i,i+n-1));
printf("%d\n",ans);
return 0;
}
既然无能更改,又何必枉自寻烦忧