1.新建代码模板

本次使用C++语言,新建一个代码模板,方便后续使用。在小熊猫工具-代码模板-C++中新建一个代码模板,内容如下:

1
2
3
4
5
#include<bits/stdc++.h>
using namespace std;
int main(){
return 0;
}

如果是蓝桥杯,这里有一些小建议建议你加上:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<bits/stdc++.h>//万能头文件
#define endl '\n'//如果改了cin/cout速读速写,这里建议你改为'\n',不然没用。
#define int long long//为了防止数据溢出,这里建议你将int改为long long
#define INF 0x3f3f3f3f3f3f3f3f//int类型的正无穷
using namespace std;
void solve(){
//这里写你的代码
}
signed main(){//signed避免int被改为long long报错,也是int类的
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);//上面三行修改cin/cout速读速写
int t=1;
//cin>>t;
while(t--){
solve();//蓝桥杯一般是多个测试用例,这里建议你写一个solve函数
}
return 0;
}

2.题解基础(一)C++语言

第一章C++语言入门

1000:入门测试题目

查看本题

【题目描述】
求两个整数的和。
【输入】
一行,两个用空格隔开的整数。
【输出】
两个整数的和。
【输入样例】
2 3
【输出样例】
5

1
2
3
4
5
6
7
#include <stdio.h>
int main(){
int x,y;
scanf("%d %d",&x,&y);
printf("%d",x+y);
return 0;
}

2060:【例1.1】计算机输出

打印个helloworld,应该都会吧?

2061:【例1.2】梯形面积

查看本题

题目详见2061:【例1.2】梯形面积

1
2
3
4
5
6
7
8
9
#include <stdio.h>
int main(){
float area;
int h;
h=2*150/15;
area=(15+25)*h/2;
printf("%.2f",area);
return 0;
}

2062:【例1.3】电影票

查看本题

【题目描述】
已知一位小朋友的电影票价是10元,计算x位小朋友的总票价是多少?
【输入】
输入x。
【输出】
人数和电影票总价,中间用一个空格隔开。
【输入样例】
2
【输出样例】
2 20

1
2
3
4
5
6
#include <stdio.h>
int main(){
int x;
scanf("%d",&x);
printf("%d %d",x,x*10);
}

2063:【例1.4】牛吃牧草

查看本题

【题目描述】
有一个牧场,牧场上的牧草每天都在匀速生长,这片牧场可供15头牛吃20天,或可供20头牛吃10天,那么,这片牧场每天新生的草量可供几头牛吃1天?

1
2
3
4
5
6
#include <iostream>
using namespace std;
int main() {
cout << (15 * 20 - 20 * 10) / (20 - 10);
return 0;
}

1001:Hello,World!

查看本题

编写一个能够输出“Hello,World!”的程序,这个程序常常作为一个初学者接触一门新的编程语言所写的第一个程序,也经常用来测试开发、编译环境是否能够正常工作。
提示:“Hello,World!”中间没空格。
【输入】

【输出】
Hello,World!
【输入样例】
(无)
【输出样例】
Hello,World!

1
2
3
4
5
6
#include<bits/stdc++.h>
using namespace std;
int main(){
cout<<"Hello,World!";
return 0;
}

1002:输出第二个整数

查看本题

【题目描述】
输入三个整数,整数之间由一个空格分隔,整数是32位有符号整数。把第二个输入的整数输出。
【输入】
只有一行,共三个整数,整数之间由一个空格分隔。整数是32位有符号整数。
【输出】
只有一行,一个整数,即输入的第二个整数。
【输入样例】
123 456 789
【输出样例】
456

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;
int main() {
int x,y,z;
cin >> x >> y >> z;
cout << y;
return 0;
}

1003:对齐输出

查看本题

【题目描述】
读入三个整数,按每个整数占8个字符的宽度,右对齐输出它们,按照格式要求依次输出三个整数,之间以一个空格分开。
【输入】
只有一行,包含三个整数,整数之间以一个空格分开。
【输出】
只有一行,按照格式要求依次输出三个整数,之间以一个空格分开。
【输入样例】
123456789 0 -1
【输出样例】
123456789 0 -1

1
2
3
4
5
6
#include<stdio.h>
int main(void){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("%8d %8d %8d",a,b,c);
}

1004:字符三角形

查看本题

【题目描述】
给定一个字符,用它构造一个底边长5个字符,高3个字符的等腰字符三角形。
【输入】
输入只有一行,包含一个字符。
【输出】
该字符构成的等腰三角形,底边长5个字符,高3个字符。
【输入样例】

【输出样例】



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
int main (void){
char a ;
int i ,j ;
scanf("%c",&a);
for(i=1;i<=3;i++){
for(j=1;j<=3-i;j++){
printf(" ");
}
for(j=1;j<i*2;j++){
printf("%c",a);
}
printf("\n");
}
return 0;
}

1005:地球人口承载力估计

查看本题

【题目描述】
假设地球上的新生资源按恒定速度增长。照此测算,地球上现有资源加上新生资源可供x亿人生活a年,或供y亿人生活b年。为了能够实现可持续发展,避免资源枯竭,地球最多能够养活多少亿人?
【输入】
一行,包括四个正整数x,a,y,b,两个整数之间用单个空格隔开。x>y,a< b,ax< by,各整数均不大于10000
【输出】
一个实数z,表示地球最多养活z亿人,舍入到小数点后两位。
【输入样例】
110 90 90 210
【输出样例】
75.00

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <algorithm>
using namespace std;
int x,a,b,y;
int main()
{
cin>>x>>a>>y>>b;
printf("%.2lf",(b*y-a*x)*1.0/(1.0*(b-a)));
return 0;
}

第二章 顺序结构程序设计

第一节 运算符和表达式

2064:【例2.1】交换值

查看本题

题目描述】
输入两个正整数a和b,试交换a、b的值(使a的值等于b,b的值等于a)。
【输入】
输入两个正整数a和b。
【输出】
输出a与b交换值后的结果。
【输入样例】
2 3
【输出样例】
3 2

1
2
3
4
5
6
7
8
9
#include <bits/stdc++.h>
using namespace std;

int main(void){
int a,b,t;
cin >> a >> b;
t=a;a=b;b=t;
cout << a << " " << b;
}

2065:【例2.2】整数的和

查看本题

【题目描述】
求3个整数的和。
输入a、b、c这3个整数,求它们的和。
【输入】
3个整数。
【输出】
三个数的和。
【输入样例】
1 2 3
【输出样例】
6

1
2
3
4
5
6
7
8
#include <bits/stdc++.h>
using namespace std;

int main(void){
int a,b,c;
cin>>a>>b>>c;
cout<<a+b+c;
}

2066:【例2.3】买图书

查看本题

【题目描述】
已知小明有n元,他买了一本书,这本书原价为m元,现在打8折出售。求小明还剩多少钱(保留2位小数)。
【输入】
输入n,m。
【输出】
小明还剩多少钱(保留2位小数)。
【输入样例】
100 100
【输出样例】
20.00

1006:A+B问题(同1000做法)

这里不再赘述,详情看1000

1007:计算(a+b)×c的值

查看本题

【题目描述】
给定3个整数a、b、c,计算表达式(a+b)×c的值。
【输入】
输入仅一行,包括三个整数a、b、c, 数与数之间以一个空格分开。(-10,000< a,b,c< 10,000)
【输出】
输出一行,即表达式的值。
【输入样例】
2 3 5
【输出样例】
25

1
2
3
4
5
6
7
8
#include <bits/stdc++.h>
using namespace std;

int main(void){
int a,b,c;
cin>>a>>b>>c;
cout<<(a+b)*c;
}

1008:计算(a+b)/c的值

*改为/即可。

1009:带余除法

查看本题

【题目描述】
给定被除数和除数,求整数商及余数。此题中请使用默认的整除和取余运算,无需对结果进行任何特殊处理。
【输入】
一行,包含两个整数,依次为被除数和除数(除数非零),中间用一个空格隔开。
【输出】
一行,包含两个整数,依次为整数商和余数,中间用一个空格隔开。
【输入样例】
10 3
【输出样例】
3 1

1
2
3
4
5
6
7
8
#include <bits/stdc++.h>
using namespace std;

int main(void){
int a,b;
cin>>a>>b;
cout<<a/b<<" "<<a%b;
}

1010:计算分数的浮点数值

查看本题

【题目描述】
两个整数a和b分别作为分子和分母,既分数ab,求它的浮点数值(双精度浮点数,保留小数点后9位)。
【输入】
输入仅一行,包括两个整数a和b。
【输出】
输出也仅一行,分数ab的浮点数值(双精度浮点数,保留小数点后9位)。
【输入样例】
5 7
【输出样例】
0.714285714

1
2
3
4
5
6
7
8
#include <bits/stdc++.h>
using namespace std;

int main(void){
double a,b;
cin>>a>>b;
printf("%.9lf",a/b);
}

第二节 常量和变量

2067:【例2.5】圆

查看本题

【题目描述】
输入半径r,输出圆的直径、周长、面积,数与数之间以一个空格分开,每个数保留小数点后4位。
【输入】
输入半径。
【输出】
如题述。数与数之间以一个空格分开。
【输入样例】
1.0
【输出样例】
2.0000 6.2832 3.1416

1
2
3
4
5
6
7
8
#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926
int main(void){
double r;
cin>>r;
printf("%.4lf %.4lf %.4lf",2*r,2*pi*r,pi*r*r);
}

2068:【例2.6】鸡兔同笼

查看本题

题目详见2068:【例2.6】鸡兔同笼

1
2
3
4
5
6
7
8
9
10
#include <bits/stdc++.h>
using namespace std;

int main(void){
long long ck,rb,f,h;
cin>>h>>f;
rb=(f-2*h)/2;
ck=h-rb;
cout<<ck<<" "<<rb;
}

1011:甲流疫情死亡率

查看本题

【题目描述】
甲流并不可怕,在中国,它的死亡率并不是很高。请根据截止2009年12月22日各省报告的甲流确诊数和死亡数,计算甲流在各省的死亡率。
【输入】
输入仅一行,有两个整数,第一个为确诊数,第二个为死亡数。
【输出】
输出仅一行,甲流死亡率,以百分数形式输出,精确到小数点后3位。
【输入样例】
10433 60
【输出样例】
0.575%

1
2
3
4
5
6
7
8
#include <bits/stdc++.h>
using namespace std;

int main(void){
double a,b;
cin>>a>>b;
printf("%.3lf%%",b*100/a);
}

1012:计算多项式的值

查看本题

【题目描述】
对于多项式f(x)=ax3+bx2+cx+d和给定的a,b,c,d,x,计算f(x)的值,保留到小数点后7位。
【输入】
输入仅一行,包含5个实数,分别是x,及参数a、b、c、d的值,每个数都是绝对值不超过100的双精度浮点数。数与数之间以一个空格分开。
【输出】
输出一个实数,即f(x)的值,保留到小数点后位。
【输入样例】
2.31 1.2 2 2 3
【输出样例】
33.0838692

1
2
3
4
5
6
7
8
9
#include <bits/stdc++.h>
using namespace std;

int main(void){
double x,a,b,c,d,ans;
cin>>x>>a>>b>>c>>d;
ans=a*x*x*x+b*x*x+c*x+d;
printf("%.7lf",ans);
}

1013:温度表达转化

查看本题

题目详见1013:温度表达转化

1
2
3
4
5
6
7
8
9
#include <bits/stdc++.h>
using namespace std;

int main(void){
double C,F;
cin>>F;
C=5*(F-32)/9;
printf("%.5lf",C);
}

1014:与圆相关的计算

查看本题

题目详见1014:与圆相关的计算

1
2
3
4
5
6
7
8
#include <bits/stdc++.h>
using namespace std;

int main(void){
double r,pi=3.14159;
cin>>r;
printf("%.4lf %.4lf %.4lf",2*r,2*pi*r,pi*r*r);
}

1015:计算并联电阻的阻值

查看本题

题目详见1015:计算并联电阻的阻值

1
2
3
4
5
6
7
8
#include <bits/stdc++.h>
using namespace std;

int main(void){
double r1,r2;
cin>>r1>>r2;
printf("%.2lf",1/((1/r1)+(1/r2)));
}

第三节 标准数据类型

1414:【17NOIP普及组】成绩

查看本题

题目详见1414:【17NOIP普及组】成绩

1
2
3
4
5
6
7
8
#include <bits/stdc++.h>
using namespace std;

int main(void){
int a,b,c;
cin>>a>>b>>c;
cout<<a*0.2+b*0.3+c*0.5;
}

1016:整型数据类型存储空间大小

查看本题

【题目描述】
分别定义int,short类型的变量各一个,并依次输出它们的存储空间大小(单位:字节)。

1
2
3
4
5
6
7
8
#include <bits/stdc++.h>
using namespace std;

int main(void){
int a;
short b;
cout<<sizeof(a)<<" "<<sizeof(b);
}

1017:浮点型数据类型存储空间大小

将1016题代码中的intshort类型改为floatdouble类型,输出它们的存储空间大小(单位:字节)即可。

1018:其他数据类型存储空间大小

将1016题代码中的intshort类型改为boolchar类型,输出它们的存储空间大小(单位:字节)即可。

1019:浮点数向零舍入

查看本题

【题目描述】
输入一个单精度浮点数,将其向零舍入到整数。说明:向零舍入的含义是,正数向下舍入,负数向上舍入。提示:可以使用强制类型转换来实现。
【输入】
一个单精度浮点数。
【输出】
一个整数,即向零舍入到整数的结果。
【输入样例】
2.3
【输出样例】
2

1
2
3
4
5
6
7
8
#include <bits/stdc++.h>
using namespace std;

int main(void){
float a;
cin>>a;
cout<<int(a);
}

1020:打印ASCII码

查看本题

题目详见1020:打印ASCII码

1
2
3
4
5
6
7
8
#include <bits/stdc++.h>
using namespace std;

int main(void){
char a;
scanf("%c",&a);
printf("%d",a);
}

1021:打印字符

查看本题

题目详见1021:打印字符

1
2
3
4
5
6
7
8
#include <bits/stdc++.h>
using namespace std;

int main(void){
int a;
scanf("%d",&a);
printf("%c",a);
}

1022:整型与布尔型的转换

查看本题

题目详见1022:整型与布尔型的转换

1
2
3
4
5
6
7
8
#include <bits/stdc++.h>
using namespace std;

int main(void){
int a;
scanf("%d",&a);
printf("%d",bool(a));
}


1
2
3
4
5
6
7
8
9
10
#include <bits/stdc++.h>
using namespace std;

int main(void){
int a;
bool b;
cin>>a;
b=a;
cout<<b;
}

1023:Hello,World!的大小

查看本题

题目详见1023:Hello,World!的大小

1
2
3
4
5
6
#include <bits/stdc++.h>
using namespace std;

int main(void){
cout<<sizeof("Hello, World!");
}

第四节 数据输入输出

1024:保留3位小数的浮点数

查看本题

题目详见1024:保留3位小数的浮点数

1
2
3
4
5
6
7
8
#include <bits/stdc++.h>
using namespace std;

int main(void){
double a;
cin>>a;
printf("%.3lf",a);
}

1025:保留12位小数的浮点数

查看本题

题目详见1025:保留12位小数的浮点数

1
2
3
4
5
6
7
8
#include <bits/stdc++.h>
using namespace std;

int main(void){
double a;
cin>>a;
printf("%.12lf",a);
}

1026:空格分隔输出

查看本题

题目详见1026:空格分隔输出

1
2
3
4
5
6
7
8
9
10
11
#include <bits/stdc++.h>
using namespace std;

int main(void){
char a;
int b;
float c;
double d;
cin>>a>>b>>c>>d;
printf("%c %d %.6lf %.6lf",a,b,c,d);
}

1027:输出浮点数

查看本题

题目详见1027:输出浮点数

1
2
3
4
5
6
7
8
#include <bits/stdc++.h>
using namespace std;

int main(void){
double a;
cin>>a;
printf("%f\n%.5f\n%e\n%g",a,a,a,a);
}

1028:字符菱形

不太会打印题哇

查看本题

题目详见1028:字符菱形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
using namespace std;
int main()
{
char c;
cin>>c;
int n = 5;//菱形对角线长度。若要输出任意对角线长度的菱形,则输入该变量。
for(int i = 0; i < n; ++i)//一共有n行
{
int sn = abs(i - n / 2);//该行前面空格数
int en = n - 2 * sn;//该行字符个数
for(int j = 0; j < sn; ++j)
cout<<' ';
for(int j = 0; j < en; ++j)
cout<<c;
cout<<endl;
}
return 0;
}

第五节 顺序结构实例

2070:【例2.13】数字对调

查看本题

题目详见2070:【例2.13】数字对调
第一种方法没有考虑0不能作为首位的情况,所以会提交失败

1
2
3
4
5
6
7
8
#include <bits/stdc++.h>
using namespace std;

int main(void){
int a;
cin>>a;
cout<<a%10<<a/10%10<<a/100%10;
}

1
2
3
4
5
6
7
8
9
10
11
12
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n, a, b, c;
cin >> n;
a = n%10;//个位
b = n/10%10;//十位
c = n/100;//百位
cout << a*100+b*10+c;//如果a是0,那么输出的数字没有百位
return 0;
}

1031:反向输出一个三位数

查看本题

【题目描述】
将一个三位数反向输出,例如输入358,反向输出853。
【输入】
一个三位数n。
【输出】
反向输出n。
【输入样例】
100
【输出样例】
001

1
2
3
4
5
6
7
8
9
10
11
#include <bits/stdc++.h>
using namespace std;

int main(void){
int n,g,s,b;
cin>>n;
g=n%10;
s=n/10%10;
b=n/100%10;
cout << g << s << b;
}

1035:等差数列末项计算

查看本题

【题目描述】
给出一个等差数列的前两项a1,a2,求第n项是多少。
【输入】
一行,包含三个整数a1,a2,n。−100≤a1,a2≤100,0

1
2
3
4
5
6
7
8
#include <bits/stdc++.h>
using namespace std;

int main(void){
int a1,a2,n;
cin>>a1>>a2>>n;
cout<<a1+(n-1)*(a2-a1);
}