【openjudge 计算概论(A)】[基础编程练习(运算成分)]
01:鸡尾酒疗法
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const double eps=0.05;
int n,x,y;
int main()
{
scanf("%d%d%d",&n,&x,&y);
double p=(double)y/x;
for(int i=1;i<n;++i)
{
int x,y;
scanf("%d%d",&x,&y);
double sum=(double)y/x;
if(sum-p>eps) printf("better\n");
else
if(p-sum>eps) printf("worse\n");
else printf("same\n");
}
return 0;
}
02:求出e的值
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
ll mi[20];
int n;
int main()
{
int i,j;
scanf("%d",&n);
mi[1]=1;
for(i=2;i<=n;++i) mi[i]=mi[i-1]*i;
double ans=1;
for(i=1;i<=n;++i) ans+=(double)1/mi[i];
printf("%.10lf\n",ans);
return 0;
}
03:球弹跳高度的计算(等比数列)
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
double ans,n;
int main()
{
int i;
scanf("%lf",&n);
ans=n;
i=9;
double last=n;
while(i--)
{
ans+=n;
n/=2;
}
n/=2;
printf("%g %g\n",ans,n);
return 0;
}
04:求分数序列和
#include<cstdio>
using namespace std;
double ans,a[1100],b[1100];
int n;
int main()
{
scanf("%d",&n);
a[1]=2.0; a[2]=3.0;
b[1]=1.0; b[2]=2.0;
for(int i=3;i<=n;++i) a[i]=a[i-1]+a[i-2],b[i]=b[i-1]+b[i-2];
for(int i=1;i<=n;++i) ans+=(a[i]/b[i]);
printf("%.4lf\n",ans);
return 0;
}
06:角谷猜想
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int x;
void pract(int x)
{
if(x==1)
{printf("End\n"); return;}
if (x%2)
{int a=x*3+1; printf("%d*3+1=%d\n",x,a); pract(a);}
else
{int a=x/2; printf("%d/2=%d\n",x,a); pract(a);}
return;
}
int main()
{
scanf("%d",&x);
pract(x);
return 0;
}
07:分数求和
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int fm[20],fz[20],n,lcm,sum;
int gcd(int a,int b)
{
if (!(a%b)) return b;
else return gcd(b,a%b);
}
int main()
{
int i,j;
scanf("%d",&n);
for (i=1;i<=n;++i)
{
char c;
scanf("%d%c%d",&fz[i],&c,&fm[i]);
}
lcm=fm[1];
for (i=2;i<=n;++i) lcm=lcm*fm[i]/gcd(lcm,fm[i]);
for (i=1;i<=n;++i)
{
int sm=fz[i]*(lcm/fm[i]);
sum+=sm;
}
if (!(sum%lcm)) {printf("%d\n",sum/lcm); return 0;}
int ans=gcd(lcm,sum);
sum/=ans; lcm/=ans;
printf("%d/%d",sum,lcm);
return 0;
}
08:人民币支付
#include<cstdio>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
printf("%d\n",n/100);
n%=100;
printf("%d\n",n/50);
n%=50;
printf("%d\n",n/20);
n%=20;
printf("%d\n",n/10);
n%=10;
printf("%d\n",n/5);
n%=5;
printf("%d\n",n);
return 0;
}
09:年龄与疾病
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int age[110];
int n,num[5];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
int x;
scanf("%d",&x);
if(x>=1&&x<=18) num[1]++;
if(x>=19&&x<=35) num[2]++;
if(x>35&&x<=60) num[3]++;
if(x>60) num[4]++;
}
printf("1-18: %.2lf%%\n",(double)num[1]/(double)n*(100.0));
printf("19-35: %.2lf%%\n",(double)num[2]/(double)n*(100.0));
printf("36-60: %.2lf%%\n",(double)num[3]/(double)n*(100.0));
printf("60-: %.2lf%%\n",(double)num[4]/(double)n*(100.0));
return 0;
}
10:求特殊自然数
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[5],b[5],opt;
inline bool check()
{
for(int i=1;i<=3;++i)
if(a[i]!=b[i]) return 0;
return 1;
}
int main()
{
int i;
scanf("%d",&opt);
for(i=81;i<=342;++i)
{
int x=i,y=i;
a[1]=x%9; x/=9;
a[2]=x%9; x/=9;
a[3]=x%9; x/=9;
b[3]=y%7; y/=7;
b[2]=y%7; y/=7;
b[1]=y%7; y/=7;
if(check()) break;
}
if(opt==1) printf("%d\n",i);
if(opt==2) printf("%d%d%d\n",b[1],b[2],b[3]);
if(opt==3) printf("%d%d%d\n",a[3],a[2],a[1]);
return 0;
}
既然无能更改,又何必枉自寻烦忧