C++入门(算法) - 习题

🌊用有趣的言语来阐述苦涩难懂的代码世界,让每一个技术都充满风趣!
🔭个人主页:散峰而望
🚀学习方向: C/C++等方向
📌专栏系列:
- 📖《C语言:从基础到进阶》
- 📚《编程工具的下载和使用》
- 🚀《C语言刷题》
- ⚖️《算法竞赛从入门到获奖》
💬人生格言:冀以尘雾之微,补益山海,荧烛末光,增辉岁月。
🎬博主简介



文章目录
- 前言
- C++入门(一)习题
- Hello,World!
- 小飞机
- 第二个整数
- 字符三角形
- C++入门(二)习题
- 整数
- 打印字符
- 倒序
- 整型数据类型存储空间大小
- 买票
- [A + B 问题](https://www.luogu.com.cn/problem/B2007)
- 结语
前言
本篇将对C++入门三篇文章中的习题进行讲解
C++入门(一)习题
Hello,World!
该题目非常简单,直接根据题目的要求输出就行。代码如下:
#include<iostream>
using namespace std;
int main()
{cout << "Hello,World!" << endl;return 0;
}
提示:
注意语法细节:main函数写法、大括号匹配、中英标点符号使用、头文件
同时还要注意挑选相匹配的编译环境,只要是C++就行。

小飞机
本题也是非常简单,按照题目要求来就行,注意一下每个 * 之间的排距。

代码如下:
#include <iostream>
using namespace std;int main()
{cout << " ** " << endl;cout << " ** " << endl;cout << "************" << endl;cout << "************" << endl;cout << " * * " << endl;cout << " * * " << endl;return 0;}
当然像这样也行:
#include <iostream>
using namespace std;int main()
{cout << " **" << endl;cout << " **" << endl;cout << "************" << endl;cout << "************" << endl;cout << " * *" << endl;cout << " * *" << endl;return 0;}
第二个整数
该题比较简单,先输入C++标准 int main() 函数模板,然后定义变量,最后按照题目要求输入即可,代码如下:
#include <iostream>
using namespace std;int main()
{int n1, n2, n3;cin >> n1;cin >> n2;cin >> n3;cout << n2 << endl;
}
当回答正确时,提交页面会出现这样的画面:

因为 cin 可以连续输入,所以还能这样写:
#include <iostream>
using namespace std;int main()
{int n1, n2, n3;cin >> n1 >> n2 >> n3;cout << n2 << endl;
}
又因为只要输入输出得到 n2 即可,故还能这样写:
#include <iostream>
using namespace std;int main()
{int n1, n2, n3;cin >> n1 >> n2;cout << n2 << endl;
}
提示:
这里的 n1 和 n2 两个整数,可以分两次读入,也可以一次读入,只有获取到第二个输入值才能输出结果。
cin 是支持连续读入几个数值的
cout 也是支连续输出几个数值的
字符三角形
同样比较简单,按照题目要求来,因为题目要求字符,所以要用 char 来定义接受。同时也要注意空格数。代码如下:
#include <iostream>
using namespace std;int main()
{char c = 0;cin >> c;cout << " " << c << endl;cout << " " << c << c << c << endl;cout << c << c << c << c << c << endl;return 0;}
C++入门(二)习题
整数
这道题也是比较简单的,根据题目要求就行,不过注意题目要求的输入范围
n(−10 ^ 4 ≤ n ≤ 10 ^ 4 )小于 int 类型
#include <iostream>
using namespace std;int main()
{int a;cin >> a;cout << a;return 0;
}
打印字符
题目要求输入一个整数,然后打印出字符,所以需要改变一下数据类型。先定义为整型,然后输入,再改为字符型输出。代码如下:
#include<iostream>
using namespace std;int main()
{int n = 0;cin >> n;char ch = n;cout << ch << endl;return 0;}
当然有人会想,cin 不是可以直接识别变量类型吗,直接用字符类型来定义不就行吗,代码像这样:
#include<iostream>
using namespace std;int main()
{char ch ;cin >> ch;cout << ch << endl;return 0;}
这种情况是不行的,当你输入整数时,会把第一个整数当作字符输出,演示结果如下:

提示:
“一个整数,即字符的 ASCII 码”,那么就必须使用一个 int 类型的变量来输入
数值。因为C++的cin是根据变量的类型在缓冲区读取数据的。换成char类型是不行的,虽然char类型的变量也是能存储这个ASCII值的。
倒序
题目要求要输入三个整数,同时要大于等于0且小于等于2^31-1,刚好满足 signed int 的取值范围。同时还要注意题目要求的空格,如果直接连着输入,则会连在一起。代码如下:
#include <iostream>
using namespace std;int main(){int a, b, c;//输入cin >> a >> b >> c;//输出cout << c << b << a;return 0; }
演示效果:

所以要在每一个字符输入中间添加 " " 才行,也可以用 ‘ ’ ,只是表示不一样,一个是字符串一个是字符。
#include <iostream>
using namespace std;int main(){int a, b, c;//输入cin >> a >> b >> c;//输出cout << c <<" " << b << " " << a;return 0; }
整型数据类型存储空间大小

这道题也比较简单,按照题目要求来输入就行。代码如下:
#include <iostream>
using namespace std;
int main()
{int n = 0;short s = 0;cout << sizeof(n) << " " << sizeof(s) << endl;return 0;}
提交正确后会出现这个窗口:

此处的 int n = 0; 和 short s = 0; 是对整型变量和短整型变量的初始化定义。因为是在局部范围 内 ,如果不初始化,会发生不可预测行为和运行错误。
买票
非常简单的一道题,先输入C++标准 int main() 函数模板,然后定义变量,让变量乘以100输出就行,按照题目要求就行。代码如下:
#include <iostream>
using namespace std;int main()
{int n = 0;cin >> n;cout << n * 100 << endl;return 0;
}
当然,也可以按照全局变量写。代码如下:
#include <iostream>
using namespace std;int n;
int main()
{cin >> n;cout << n * 100 << endl;return 0;
}
A + B 问题
也是一道非常简单的题,先输入C++标准 int main() 函数模板,然后定义变量,最后让两个变量相加即可。代码如下:
#include <iostream>
using namespace std;int main()
{int a = 0;int b = 0;cin >> a >> b;cout << a + b << endl;return 0;
}
同样也可以使用全局变量:
#include <iostream>
using namespace std;int a, b;
int main()
{cin >> a >> b;cout << a + b << endl;return 0;
}
占时先讲解这些,希望能帮助各位出入C++的冒险者。
结语
同时愿诸君能一起共渡重重浪,终见缛彩遥分地,繁光远缀天。


