信息学奥赛一本通 C++

信息学奥赛一本通(C++版) 第一部分 C++语言 第二章 第五节 顺序结构实例

2018年信息学奥赛NOIP资料下载

//1029 计算浮点数相除的余
//提交,未通过,搜索网络https://zhidao.baidu.com/question/1862253134086546187.html
//发现%g,猜测输出忽略小数部分,最后的一些连续的0
//搜索,http://blog.sina.com.cn/s/blog_771bd2c901011bmp.html摘抄如下:
// %g用来输出实数,它根据数值的大小,自动选f格式或e格式(选择输出时占宽度较小的一种),且不输出无意义的0。即%g是根据结果自动选择科学记数法还是一般的小数记数法
//收获,学习了%g的用法,修改,提交AC
#include <stdio.h>
int main(){
double a,b;
scanf("%lf%lf",&a,&b);
printf("%g",a-(int)(a/b)*b);//此处写成 printf("%.4lf",a-(int)(a/b)*b);
return 0;
}

//1030 计算球的体积
#include <stdio.h>
int main(){
double r;
scanf("%lf",&r);
printf("%.2lf",43.14rrr/3);
return 0;
}

//1031 反向输出一个三位数
#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
printf("%d%d%d",n%10,n/10%10,n/100);
return 0;
}

//1032 大象喝水查
#include <stdio.h>
int main(){
int h,r;
scanf("%d%d",&h,&r);
printf("%d",(int)(20101010/(3.14rrh))+1);//printf("%d",int(20101010/(3.14rrh))+1);编译错误
return 0;
}

//1033 计算线段长度
//提交,未通过,不敢相信,
//重新读题,发现输入是实数,而不是整数,读了半天题,发现这个没读出
//修改,提交,未通过,将float改成double ,提交AC
#include <stdio.h>
#include <math.h>
int main(){
double xa,ya,xb,yb;//float xa,ya,xb,yb;//int xa,ya,xb,yb;
scanf("%lf%lf%lf%lf",&xa,&ya,&xb,&yb);//scanf("%f%f%f%f",&xa,&ya,&xb,&yb);//scanf("%d%d%d%d",&xa,&ya,&xb,&yb);
printf("%.3lf",sqrt((xb-xa)(xb-xa)+(yb-ya)(yb-ya)));//printf("%.3f",sqrt((xb-xa)(xb-xa)+(yb-ya)(yb-ya)));
return 0;
}

//1034 计算三角形面积
//已知三条边,求面积,搜索网络,发现 https://baike.baidu.com/item/海伦公式/106956?fr=aladdin海伦公式
//提交,未通过,将float 全改成 double,提交AC
//输入仅一行,包括6个单精度浮点数,分别对应x1, y1, x2, y2, x3, y3。虽然题目说的是单精度浮点数,还是用double进行读取
#include <stdio.h>
#include <math.h>
int main(){
double x1,y1,x2,y2,x3,y3,a,b,c,p,s;//float x1,y1,x2,y2,x3,y3,a,b,c,p,s;
scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3);//scanf("%f%f%f%f%f%f",&x1,&y1,&x2,&y2,&x3,&y3);
a=sqrt((x1-x2)(x1-x2)+(y1-y2)(y1-y2));
b=sqrt((x1-x3)(x1-x3)+(y1-y3)(y1-y3));
c=sqrt((x2-x3)(x2-x3)+(y2-y3)(y2-y3));
p=(a+b+c)/2;
s=sqrt(p*(p-a)(p-b)(p-c));
printf("%.2lf",s);//printf("%.2f",s);
return 0;
}

//1035 等差数列末项计算
#include <stdio.h>
int main(){
int a1,a2,n;
scanf("%d%d%d",&a1,&a2,&n);
printf("%d",a1+(n-1)*(a2-a1));
return 0;
}

//1036 AB问题
#include <stdio.h>
int main(){
long long a,b;
scanf("%lld%lld",&a,&b);
printf("%lld",a
b);
return 0;
}

//1037 计算2的幂
#include <stdio.h>
int main(){
int n,ans=1,i;
scanf("%d",&n);
for(i=1;i<=n;i++)
ans*=2;
printf("%d",ans);
return 0;
}
//1037 计算2的幂
//方法二:正好用到位运算,试试该题,
//样例通过,提交AC 2018-4-12
#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
printf("%d",1<<n);
return 0;
}

//1038 苹果和虫子
//提交,未通过,搜索网络https://zhidao.baidu.com/question/240793441435694964.html
//少考虑了m会比n大的情况。假如只有n=5个苹果,不可能出现虫子吃掉m=6个苹果的情况吧。
//修改,提交AC
#include <stdio.h>
int main(){
int n,x,y,ans;
scanf("%d%d%d",&n,&x,&y);
if(y%x==0)ans=n-y/x;
else ans=n-y/x-1;
if(ans<0)
printf(“0”);
else
printf("%d",ans);
return 0;
}

猜你喜欢

转载自blog.csdn.net/tianli315/article/details/86572504