【openjudge 计算概论(A)】[基础编程练习1]
1:大象喝水
#include<cmath>
#include<cstdio>
using namespace std;
const double pi=3.14159;
int main()
{
int h,r;
scanf("%d%d",&h,&r);
double v=(double)h*r*r*pi;
double sum=20000/v;
int ans=ceil(sum);
printf("%d\n",ans);
return 0;
}
2:苹果和虫子
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
int n,x,y;
scanf("%d%d%d",&n,&x,&y);
int sum=n-ceil((double)y/(double)x);
printf("%d\n",sum);
return 0;
}
3:晶晶赴约会
#include<cstdio>
using namespace std;
int main()
{
int i;
scanf("%d",&i);
if(i!=1&&i!=3&&i!=5) printf("YES\n");
else printf("NO\n");
return 0;
}
4:求一元二次方程的根(这道题的坑点在于要限制精度)
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define eps 1e-5
using namespace std;
int n;
double a,b,c;
inline double solve(double x)
{
if (x<eps&&x>-eps) return 0;
return x;
}
int main()
{
scanf("%d",&n);
while(n--)
{
scanf("%lf%lf%lf",&a,&b,&c);
double check=(b*b-4*a*c);
if(check<eps&&check>-eps)
{
double ans=(-b)/(2*a);
printf("x1=x2=%.5lf\n",solve(ans));
}
else
if(check>eps)
{
double ans1=(-b+sqrt(check))/(2*a);
double ans2=(-b-sqrt(check))/(2*a);
if(ans1<ans2) swap(ans1,ans2);
printf("x1=%.5lf;x2=%.5lf\n",solve(ans1),solve(ans2));
}
else
{
double shi=(-b)/(2*a);
double ans=sqrt(-check)/(2*a);
printf("x1=%.5lf+%.5lfi;x2=%.5lf-%.5lfi\n",solve(shi),solve(ans),solve(shi),solve(ans));
}
}
return 0;
}
5:鸡兔同笼(别忘了考虑腿是奇数的情况)
#include<cstdio>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
if(n%2) {printf("0 0\n"); return 0;}
int sm=n/4+(n%4)/2;
int sx=n/2+(n%2)/4;
printf("%d %d\n",sm,sx);
return 0;
}
6:判断闰年(这个题,听说春天的时候有小伙伴忘记怎么判闰年。。。)
#include<cstdio>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
if(!(n%400)||!(n%4)&&(n%100)) printf("Y");
else printf("N");
return 0;
}
7:奇数求和
#include<cstdio>
using namespace std;
int ans,m,n;
int main()
{
scanf("%d%d",&n,&m);
for(int i=n;i<=m;++i)
if(i%2) ans+=i;
printf("%d\n",ans);
return 0;
}
8:与7无关的数
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n;
int sum;
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
int a=i/10,b=i%10;
if (i%7==0)
continue;
if (a==7||b==7)
continue;
sum+=i*i;
}
printf("%d\n",sum);
return 0;
}
既然无能更改,又何必枉自寻烦忧