XJOI一级五段题解(g++,即C++),也可视作C++算法竞赛教程
目录
-
- [Problem 9305](https://xjoi.net/problem/9305) 加法检测器
-
- 一 题目内容
- 二 新知识点
- 三 思路
- 四 AC代码
- [Problem 3537](https://xjoi.net/problem/3537) 判断闰年
-
- 一 题目内容
- 二 新知识点
- 三 思路
- 四 AC代码
- [Problem 3903](https://xjoi.net/problem/3903) 水仙花数
-
- 一 题目内容
- 二 新知识点
- 三 思路
- 四 AC代码
- [Problem 9304](https://xjoi.net/problem/9304) maoge的数学测试
-
- 二 新知识点
-
- 2.1 在一行内写下一句if(if-else if-else,if-else if,if-else)语句
- 三 思路
- 四 AC代码
- [Problem 1111](https://xjoi.net/problem/1111) 三数中最大和最小
-
- 一 题目内容
- 二 新知识点
- 三 思路
- 四 AC代码
- [Problem 9873](https://xjoi.net/problem/9873) 从大到小
-
- 一 题目内容
- 二 新知识点
- 三 思路
- 四 AC代码
Problem 9305 加法检测器
一 题目内容
【题目描述】
maoge在教小Maoge加法,他想计算出小Maoge加法的准确度如何,但是因为题目太多了,他算不出来,所以找到了你。
【数据格式】
输入一行三个数a,b,c,分别表示两个加数和小Maoge算出来的答案(a,b,c在0~10000之间)。
输出一行一个字符串。
如果小Maoge算的答案和正确答案的差值在正负10(不包括10)以内,就输出“Good”。
如果在10~20(包括10不包括20)之间,就输出“not bad”
否则输出“QwQ”。
样例输入:
12 35 37
样例输出:
not bad
二 新知识点
本题没有新知识点。
三 思路
依题意处理并判断即可。
四 AC代码
#includeusing namespace std;int main(){ int a,b,c; cin>>a>>b>>c; if(abs(a+b-c)<10){ cout<<"Good"; }else if(abs(a+b-c)>=10&&abs(a+b-c)<20){ cout<<"not bad"; }else{ cout<<"QwQ"; } return 0;}
Problem 3537 判断闰年
一 题目内容
时间:0.2s 空间:32M
题目描述:
判断一个整数 n n n是否是闰年
输入格式:
输入一个整数 n n n
输出格式:
是闰年输出"yes" 否则输出"no"
样例输入1:
2000 2000 2000
样例输出1:
y e s yes yes
样例输入2:
1900 1900 1900
样例输出2:
n o no no
约定:
1 ≤ n ≤ 2018 1 \leq n \leq 2018 1≤n≤2018
二 新知识点
本题没有新知识点。
三 思路
闰年的判断法则是:四年一闰,一百年不闰,四百年才闰。
四 AC代码
#includeusing namespace std;int main(){ int a; cin>>a; if(a%4!=0){ cout<<"no"; }else if(a%4==0&&a%100!=0){ cout<<"yes"; }else if(a%4==0&&a%400!=0){ cout<<"no"; }else{ cout<<"yes"; } return 0;}
Problem 3903 水仙花数
一 题目内容
题目描述:
给定一个整数 n n n,判断其是否为水仙花数。
三个数 a a a(百位)、 b b b(十位)、 c c c(个位),若满足 a 3 + b 3 + c 3 = a b c a^3+b^3+c^3=abc a3+b3+c3=abc ( a b c abc abc为该数本身) ,则称 n n n为水仙花数。
输入格式:
一行一个整数 n n n
输出格式:
若是,输出“YES”,若否,输出“NO”
样例输入:
153 153 153
样例输出:
Y E S YES YES
时间限制:
1000 1000 1000ms
空间限制:
65536 65536 65536KB
二 新知识点
本题没有新知识点。
三 思路
还记得 Problem 3570 求个位 与 Problem 3571 求十位数 吗?(题解链接在这里)用 n / 10 n/10 n/10可分离出个位,用 n / 10 % 10 n/10\%10 n/10%10可以分离出十位。至于百位,由于 n n n是三位数,所以直接用 n / 100 n/100 n/100即可分离出百位。但要注意的是, a 3 a^3 a3不能直接写作“a^3”,而是要写作 a ∗ a ∗ a a*a*a a∗a∗a。
四 AC代码
#includeusing namespace std;int main(){ int n,a,b,c; cin>>n; //-------------------------分离出个位、十位、百位------------------------- c=n%10; b=n/10%10; a=n/100; if(a*a*a+b*b*b+c*c*c==n){ cout<<"YES"; }else{ cout<<"NO"; } return 0;}
Problem 9304 maoge的数学测试
题目描述:
Maoge在他的数学测试中遇到了一个他认为很难的问题。这是一个关于分段函数的问题。
这个函数描述如下:
y = { x x < 2 x 2 + 1 2 ≤ x < 6 x + 1 6 ≤ x < 10 1 x + 1 10 ≤ x y=\begin{cases} x & x < 2 \\ x^2+1 & 2 \leq x < 6 \\ \sqrt{x+1} & 6 \leq x < 10 \\ \frac{1}{x+1} & 10 \leq x \end{cases} y=⎩⎪⎪⎪⎨⎪⎪⎪⎧xx2+1x+1x+11x<22≤x<66≤x<1010≤x
给定一个 x x x, 请计算相应的 y y y值.
输入:
输入一个整数 x x x( 0 ≤ x ≤ 20 0 \leq x \leq 20 0≤x≤20).
输出:
一个浮点数 y y y的值,代表函数的计算答案,保留两位小数.
样例输入:
3 3 3
样例输出:
10.00 10.00 10.00
二 新知识点
2.1 在一行内写下一句if(if-else if-else,if-else if,if-else)语句
有些时候,我们会在写if语句时抱怨:if语句大大增加了代码的行数,让调试代码变得好费劲!这时,如果if语句内只有一行代码,就可以这么写:
if(条件)语句
这与
if(条件){ 语句}
效果相同。
当然,if-else if-else,if-else if,if-else语句也可以在一行内写下,如下所示:
//if-else if-else语句if(条件1)语句1else if(条件2)语句2else if(条件3)语句3else if(条件4)语句4.........else if(条件n-1)语句n-1else 语句n//if-else if语句if(条件1)语句1else if(条件2)语句2else if(条件3)语句3else if(条件4)语句4.........else if(条件n)语句n//if-else 语句if(条件)语句1else 语句2
它们分别与下列语句效果相同:
//if-else if-else语句if(条件1){语句1}else if(条件2){语句2}else if(条件3){语句3}else if(条件4){语句4}...{.........}else if(条件n-1){语句n-1}else{语句n}//if-else if语句if(条件1){语句1}else if(条件2){语句2}else if(条件3){语句3}else if(条件4){语句4}...{.........}else if(条件n){语句n}//if-else语句if(条件){语句1}else{语句2}
需要注意的是,只能在每条语句都只有一行的情况下这样写。
三 思路
根据题意直接判断并输出即可。
四 AC代码
#includeusing namespace std;int main(){ int x; cin>>x; if(x<2)cout<<x<<".00"; if(x>=2&&x<6)cout<<x*x+1<<".00"; if(x>=6&&x<10)printf("%.2lf",sqrt(x+1)); if(x>=10)printf("%.2lf",1.0/(x+1)); return 0;}
Problem 1111 三数中最大和最小
一 题目内容
时间:1s 空间:256M
题目描述:
输入三个数,输出最大数,最小数
输入格式:
输入一行,包含三个整数 a , b , c a,b,c a,b,c
输出格式:
输出两行,第一行输出最大数,第二行输出最小数。具体格式见样例输出。
样例输入:
1 1 1 2 2 2 3 3 3
样例输出:
T h e The The m a x i m u m maximum maximum n u m b e r number number i s is is : : : 3 3 3
T h e The The m i n i m u m minimum minimum n u m b e r number number i s is is : : : 1 1 1
约定:
1 ≤ a , b , c ≤ 1000000 1 \leq a,b,c \leq 1000000 1≤a,b,c≤1000000
二 新知识点
本题没有新知识点。
三 思路
先读入数,再依次求最大值和最小值,最后按格式输出。
四 AC代码
#includeusing namespace std;int main(){ int a,b,c,max=-1,min=99999999; //由于a,b,c不超过1000000,所以把min设成99999999是可以正常更新min的值的 cin>>a>>b>>c; cout<<"The maximum number is : "; if(a>max)max=a; if(b>max)max=b; if(c>max)max=c; cout<<max<<endl<<"The minimum number is : "; if(a<min)min=a; if(b<min)min=b; if(c<min)min=c; cout<<min;}
Problem 9873 从大到小
一 题目内容
时间:1s 空间:128M
题目描述:
将输入的3个数,从大到小输出。
输入格式:
仅一行,包含3个整数 a , b , c a,b,c a,b,c,以空格间隔。
输出格式:
仅一行,包含3个整数 a , b , c a,b,c a,b,c,以空格间隔。
样例输入1:
1 1 1 2 2 2 3 3 3
样例输出1:
3 3 3 2 2 2 1 1 1
约定:
0 ≤ a , b , c ≤ 1000 0 \leq a,b,c \leq 1000 0≤a,b,c≤1000,且 a , b , c a,b,c a,b,c互不相等。
二 新知识点
本题没有新知识点。
三 思路
先找出最大数和最小数,再依据“ a , b , c a,b,c a,b,c互不相等”找出中间数,最后依次输出即可。
四 AC代码
#includeusing namespace std;int main(){ int a,b,c,max=-1,min=99999999,o; //o是中间数(说实话,这个变量名是我乱取的) cin>>a>>b>>c; if(a>max)max=a; if(b>max)max=b; if(c>max)max=c; if(a<min)min=a; if(b<min)min=b; if(c<min)min=c; if(a!=max&&a!=min)o=a; if(b!=max&&b!=min)o=b; if(c!=max&&c!=min)o=c; cout<<max<<' '<<o<<' '<<min;}