

Microsoft Visual Studio(简称VS)是开发工具包系列产品。VS是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具、代码管控工具、集成开发环境(IDE)等等。所写的目标代码适用于微软支持的所有平台。
开卷语 开卷语 开卷语
『时空无垠地延长』 — T i m e a n d s p a c e e x t e n d e n d l e s s l y — \color{#2CA9E1}\mathsf{『时空无垠地延长』}\\\scriptsize\mathsf{— \ Time \ and \ space \ extend \ endlessly \ —} 『时空无垠地延长』— Time and space extend endlessly —
『维度被悄悄埋葬』 — D i m e n s i o n s a r e q u i e t l y b u r i e d — \color{#2CA9E1}\mathsf{『维度被悄悄埋葬』}\\\scriptsize\mathsf{—\ Dimensions \ are \ quietly \ buried—} 『维度被悄悄埋葬』— Dimensions are quietly buried—
『歌者在星空的边缘吟唱』 — T h e s i n g e r c h a n t e d a t t h e e d g e o f t h e s t a r r y s k y — \color{#2CA9E1}\mathsf{『歌者在星空的边缘吟唱』}\\\scriptsize\mathsf{— \ The \ singer \ chanted \ at \ the \ edge \ of \ the \ starry \ sky—} 『歌者在星空的边缘吟唱』— The singer chanted at the edge of the starry sky—
『永恒的毁灭乐章』 — E t e r n a l m u s i c o f d e s t r u c t i o n — \color{#2CA9E1}\mathsf{『永恒的毁灭乐章』}\\\scriptsize\mathsf{— \ Eternal \ music \ of \ destruction—} 『永恒的毁灭乐章』— Eternal music of destruction—
『在文明挣扎万年的地方』 — I n a p l a c e w h e r e c i v i l i z a t i o n h a s s t r u g g l e d f o r t h o u s a n d s o f y e a r s — \color{#2CA9E1}\mathsf{『在文明挣扎万年的地方』}\\\scriptsize\mathsf{—\ In \ a \ place \ where \ civilization \ has \ struggled \ for \ thousands \ of \ years—} 『在文明挣扎万年的地方』— In a place where civilization has struggled for thousands of years—
『三颗恒星布撒不恒定的光』 — T h r e e s t a r s c a s t n o n − c o n s t a n t l i g h t — \color{#2CA9E1}\mathsf{『三颗恒星布撒不恒定的光』}\\\scriptsize\mathsf{—\ Three \ stars \ cast \ non-constant \ light—} 『三颗恒星布撒不恒定的光』— Three stars cast non−constant light—
『乐土般的天堂』 — P a r a d i s e — \color{#2CA9E1}\mathsf{『乐土般的天堂』}\\\scriptsize\mathsf{—\ Paradise—} 『乐土般的天堂』— Paradise—
『卑微的虫子企图染指神之权杖』 — T h e l o w l y b u g s a t t e m p t t o i n f e c t t h e s c e p t e r o f G o d — \color{#2CA9E1}\mathsf{『卑微的虫子企图染指神之权杖』}\\\scriptsize\mathsf{—\ The \ lowly \ bugs \ attempt \ to \ infect \ the \ scepter \ of \ God—} 『卑微的虫子企图染指神之权杖』— The lowly bugs attempt to infect the scepter of God—
『天真的孩子手握烛棒』 — N a ı ¨ v e c h i l d r e n h o l d c a n d l e s t i c k s i n t h e i r h a n d s — \color{#2CA9E1}\mathsf{『天真的孩子手握烛棒』}\\\scriptsize\mathsf{—\ Naïve \ children \ hold \ candlesticks \ in \ their\ hands—} 『天真的孩子手握烛棒』— Naı¨ve children hold candlesticks in their hands—
『黑暗森林中的穿行』 — W a l k t h r o u g h t h e d a r k f o r e s t — \color{#2CA9E1}\mathsf{『黑暗森林中的穿行』}\\\scriptsize\mathsf{—\ Walk \ through \ the \ dark \ forest—} 『黑暗森林中的穿行』— Walk through the dark forest—
『满眼皆是骸骨与伤』 — E y e s f u l l o f s k e l e t o n s a n d w o u n d s — \color{#2CA9E1}\mathsf{『满眼皆是骸骨与伤』}\\\scriptsize\mathsf{—\ Eyes \ full \ of \ skeletons \ and \ wounds—} 『满眼皆是骸骨与伤』— Eyes full of skeletons and wounds—
『熄灭的火光蔓延着恐惧与凄凉』 — T h e e x t i n g u i s h e d f l a m e s p r e a d f e a r a n d d e s o l a t i o n — \color{#2CA9E1}\mathsf{『熄灭的火光蔓延着恐惧与凄凉』}\\\scriptsize\mathsf{—\ The \ extinguished \ flame \ spread \ fear \ and \ desolation—} 『熄灭的火光蔓延着恐惧与凄凉』— The extinguished flame spread fear and desolation—
『童话的世界被绘进画里』 — T h e w o r l d o f f a i r y t a l e s i s p a i n t e d i n t h e p a i n t i n g — \color{#E4671E}\mathsf{『童话的世界被绘进画里』}\\\scriptsize\mathsf{—\ The \ world \ of \ fairy \ tales \ is \ painted \ in \ the \ painting—} 『童话的世界被绘进画里』— The world of fairy tales is painted in the painting—
『被迫远走的游子在时间之外流浪』 — T h e w a n d e r e r , f o r c e d t o g o a w a y , w a n d e r s o u t s i d e o f t i m e — \color{#E4671E}\mathsf{『被迫远走的游子在时间之外流浪』}\\\scriptsize\mathsf{—\ The \ wanderer\ , \ forced \ to \ go \ away\ , \ wanders \ outside \ of \ time—} 『被迫远走的游子在时间之外流浪』— The wanderer , forced to go away , wanders outside of time—
『在无尽的星河浩渺里』 — I n t h e v a s t n e s s o f t h e e n d l e s s g a l a x y — \color{#E4671E}\mathsf{『在无尽的星河浩渺里』}\\\scriptsize\mathsf{—\ In \ the \ vastness \ of \ the \ endless \ galaxy—} 『在无尽的星河浩渺里』— In the vastness of the endless galaxy—
『孤独迷航』 — L o n e l y T r e k — \color{#E4671E}\mathsf{『孤独迷航』}\\\scriptsize\mathsf{—\ Lonely \ Trek—} 『孤独迷航』— Lonely Trek—
『人类的墓碑』 — M a n ′ s t o m b s t o n e — \color{#E4671E}\mathsf{『人类的墓碑』}\\\scriptsize\mathsf{—\ Man's \ tombstone—} 『人类的墓碑』— Man′s tombstone—
『被印拓在宇宙最低的地方』 — I t i s i n t h e l o w e s t p l a c e i n t h e u n i v e r s e — \color{#E4671E}\mathsf{『被印拓在宇宙最低的地方』}\\\scriptsize\mathsf{—\ It \ is \ in \ the \ lowest \ place \ in\ the \ universe—} 『被印拓在宇宙最低的地方』— It is in the lowest place in the universe—
『永远尘封的文明』 — A c i v i l i z a t i o n t h a t w i l l a l w a y s b e d u s t y — \color{#E4671E}\mathsf{『永远尘封的文明』}\\\scriptsize\mathsf{—\ A \ civilization \ that \ will \ always \ be \ dusty—} 『永远尘封的文明』— A civilization that will always be dusty—
『陪葬着二维的太阳』 — B u r i e d w i t h t h e t w o − d i m e n s i o n a l s u n — \color{#E4671E}\mathsf{『陪葬着二维的太阳』}\\\scriptsize\mathsf{—\ Buried \ with \ the \ two-\ dimensional \ sun—} 『陪葬着二维的太阳』— Buried with the two− dimensional sun—
『正在死去的宇宙』 — T h e u n i v e r s e t h a t i s d y i n g — \color{#E4671E}\mathsf{『正在死去的宇宙』}\\\scriptsize\mathsf{—\ The \ universe \ that \ is \ dying—} 『正在死去的宇宙』— The universe that is dying—
『归零重生的希望』 — T h e h o p e o f z e r o r e b i r t h — \color{#E4671E}\mathsf{『归零重生的希望』}\\\scriptsize\mathsf{—\ The \ hope \ of \ zero \ rebirth—} 『归零重生的希望』— The hope of zero rebirth—
『散逸的死线曲率的逃亡』 — T h e e s c a p e o f t h e l o o s e d e a d − l i n e c u r v a t u r e — \color{#E4671E}\mathsf{『散逸的死线曲率的逃亡』}\\\scriptsize\mathsf{—\ The \ escape \ of \ the \ loose \ dead-line \ curvature—} 『散逸的死线曲率的逃亡』— The escape of the loose dead−line curvature—
『宇宙之外的宇宙人性却悄然绽放』 — T h e h u m a n i t y o f t h e u n i v e r s e o u t s i d e t h e u n i v e r s e h a s q u i e t l y b l o s s o m e d — \color{#E4671E}\mathsf{『宇宙之外的宇宙人性却悄然绽放』}\\\scriptsize\mathsf{—\ The \ humanity \ of \ the \ universe \ outside \ the \ universe \ has \ quietly \ blossomed—} 『宇宙之外的宇宙人性却悄然绽放』— The humanity of the universe outside the universe has quietly blossomed—
『当时间消融了一切印记』 — W h e n t i m e d i s s o l v e s a l l i m p r i n t s — \color{#E4671E}\mathsf{『当时间消融了一切印记』}\\\scriptsize\mathsf{—\ When \ time \ dissolves \ all \ imprints—} 『当时间消融了一切印记』— When time dissolves all imprints—
『爱情却横跨了亿万年的离殇』 — L o v e s p a n s h u n d r e d s o f m i l l i o n s o f y e a r s o f d e a t h — \color{#E4671E}\mathsf{『爱情却横跨了亿万年的离殇』}\\\scriptsize\mathsf{—\ Love \ spans \ hundreds \ of \ millions \ of \ years \ of \ death—} 『爱情却横跨了亿万年的离殇』— Love spans hundreds of millions of years of death—
——《三体》 \normalsize{\color{red}{\ \ \ \\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ——《三体》}} ——《三体》
c++是一种计算机高级程序设计语言, 由C语言扩展升级而产生 , 最早于1979年由本贾尼·斯特劳斯特卢普在AT&T贝尔工作室研发。
C++既可以进行C语言的过程化程序设计,又可以进行以抽象数据类型为特点的基于对象的程序设计,还可以进行以继承和多态为特点的面向对象的程序设计。C++擅长面向对象程序设计的同时,还可以进行基于过程的程序设计。
C++拥有计算机运行的实用性特征,同时还致力于提高大规模程序的编程质量与程序设计语言的问题描述能力。
详细见:https://baike.so.com/doc/5372557-5608494.html

#include //头文件
using namespace std;//命名空间
int main() {//主函数
//CODE……
return 0;//结束
}
头文件是c++程序对其他程序的引用。include是预处理命令,它并不是c++中的语句,所以没有语句分隔符:‘;’,使用时用#开头,iostream是输入输出的标准头文件(是i o stream三个词组成)
using namespace std是指明采用的命名空间的指令( C 语言省略 C语言省略 C语言省略),表示使用命名空间std(标准)中的内容。命名空间是为了解决多人同时编写大型程序时名字产生冲突的问题。
int main()是主函数的起始说明,所有c++程序必须有而且只有一个main()函数,运行时,第一条运行的语句是main()函数。
int是一种数据类型——整型(后面会讲,这里只是提一下),在标准c++中规定main()函数必须声明为int(C可声明为void)
return为返回语句,一般是函数最后一条可执行语句,main()函数使用return时,数据0表示程序顺利结束。
c++中,语句和语句之间用;隔开,称为分隔符,但预处理命令、函数、{}后面不加分隔符,结构体除外。
//是单行注释符,从所在行的末尾内容都是注释内容。注释内容不会被程序执行。
例:
//我要三连!!!
/* */是多行注释符。
例:
/*我要三连!!!
你给了吗?
*/
用高级语言编写的程序称为源程序,c++源程序以.cpp为后缀。

在c++中,输入(cin)输出(cout)是用"流"的方式实现的。在定义流对象时,系统会在内存中开辟一段缓冲区,用来暂存输入输出的数据。
cout语句一般格式为:
cout<<项目1<<项目2<<……<<项目n;
cout语句的作用是将流插入运算符"<<"右侧项目的内容插入输出流中,c++系统再将输出流的内容输出到显示器上。
样例:
#include
using namespace std;
int main() {
cout<<"点个赞吧!";
return 0;
}
输出:
点个赞吧!
有的人会问:那要怎么换行呢?
我们需要用到endl。
endl:end line的缩写,换行并清空缓冲区
样例:
#include
using namespace std;
int main() {
cout<<"点个赞吧!"<<endl<<"求求了。";
return 0;
}
输出:
点个赞吧!
求求了。
上机练习:http://ybt.ssoier.cn:8088/problem_show.php?pid=2060
定义变量必须先定义后使用。定义变量时,给变量开辟对应大小的存储单元来存放数据。
定义变量格式:
数据类型 变量1,变量2,……变量n;
变量名只能由字母,数字,下划线组成,而且不能用关键字做变量名。
例如:a_max是合法的,5x是非法的,cout也是非法的。
| 数据类型 | 意思 |
|---|---|
| int | 整型 |
| long | 长整型 |
| long long | 超长整型 |
| char | 字符型 |
| string(需要头文件
<
s
t
r
i
n
g
>
| 字符串 |
| bool | 布尔型 |
| void | 无类型 |
| short | 短型 |
| fioat | 单精度浮点数 |
| double | 双精度浮点数 |
int a,b,c;
就定义了3个整型变量,可以把0、1、2、3这样的整数赋值给a、b、c,但是把1145.14、22.33、88.48这样的浮点数赋值给它们那就会自动取整。截取小数部分。
例如:
#include
using namespace std;
int main() {
int a,b,c;
a=999;//把999赋值给a
b=100;//把100赋值给b
c=a*b;//把a*b赋值给c
cout<<a<<endl<<b<<endl<<c;
return 0;
}
运行结果:
990
100
99000
| 数学符号 | c++运算符号 |
|---|---|
| + | + |
| - | - |
| × | * |
| ÷ | / |
i=i+1;
该语句的含义是先把i的值加1,再赋值给i。还可以写成i++,++叫自加运算符或自增运算符。
变量+1有4种方法:
1:变量名=变量名+1
2:变量名+=1
3:变量名++
4:++变量名
单独使用自增时n++和++n一样
但在赋值语句中,结果就不同了。
例如:
#include
using namespace std;
int main() {
int a=100,b=100;
int x=a++;
int y=++b;
cout<<x<<" "<<y;
return 0;
}
运行结果:
100 101
过程图:
| a | b | x | y |
|---|---|---|---|
| 100 | 100 | ||
| 101 | 101 | 100 | 101 |
解释:
语句x=a++表示将a的值赋值给x后,a再加1
语句y=++b表示将b加1,再把b的值赋值给y
因为自增运算符的结合方向为自左向右。
n*=2;//相当于n=n*2
在赋值符‘=’之前加上‘*’运算符,构成复合运算符
n+=i 相当于 n=n+i
n-=i 相当于 n=n-i
n/=i 相当于 n=n/i(i≠0)
n%=i 相当于 n=n%i(i≠0)
……
上机练习:http://ybt.ssoier.cn:8088/problem_show.php?pid=2063
swap()交换函数暂不讲
#include
using namespace std;
int main() {
int a,b,t;
a=10;
b=20;
cout<<"交换前"<<endl<<"a="<<a<<" "<<"b="<<b<<endl;
t=a;
a=b;
b=t;
cout<<"交换后"<<endl<<"a="<<a<<" "<<"b="<<b;
return 0
}
运行结果:
交换前
a=10 b=20
交换后
a=20 b=10
a=b-a;
b-=a;
a+=b;
a=a*b;
b/=a;
a/=b;
引入例题:
输入两个正整数a,b(在int范围内)输出他们的和。
输入:
12 3
输出:
15
输入由cin来实现,如cin>>a就是输入一个数并赋值 给a;
cin语句的格式一般为:
cin>>变量1>>变量2>>……>>变量n;
那么此题的代码为:
#include
using namespace std;
int main() {
int a,b;
cin>>a>>b;
cout<<a+b;
return 0;
}
其中:
cin>>a>>b;
相当于:
cin>>a;
cin>>b;
上机练习:http://ybt.ssoier.cn:8088/problem_show.php?pid=2062
http://ybt.ssoier.cn:8088/problem_show.php?pid=1002
在前面我讲过fioat和double都是数据类型,都能存放小数。(引用部分转载https://www.runoob.com/w3cnote/float-and-double-different.html?)
float和double的区别:
float 单精度浮点数在机内占 4 个字节,用 32 位二进制描述。
double 双精度浮点数在机内占 8 个字节,用 64 位二进制描述。
浮点数在机内用指数型式表示,分解为:数符,尾数,指数符,指数四部分。
数符占 1 位二进制,表示数的正负。
指数符占 1 位二进制,表示指数的正负。
尾数表示浮点数有效数字,0.xxxxxxx, 但不存开头的 0 和点。
指数存指数的有效数字。
指数占多少位,尾数占多少位,由计算机系统决定。
可能是数符加尾数占 24 位,指数符加指数占 8 位 – float。
数符加尾数占 48 位,指数符加指数占 16 位 – double。
知道了这四部分的占位,按二进制估计大小范围,再换算为十进制,就是你想知道的数值范围。
对编程人员来说,double 和 float 的区别是 double 精度高,有效数字 16 位,float 精度 7 位。但 double 消耗内存是 float 的两倍,double 的运算速度比 float 慢得多,C 语言中数学函数名称 double 和 float 不同,不要写错,能用单精度时不要用双精度(以省内存,加快运算速度)。
类型 比特数 有效数字 数值范围
float 32 6-7 -3.410(-38)~3.410(38)
double 64 15-16 -1.710(-308)~1.710(308)
long double 128 18-19 -1.210(-4932)~1.210(4932)
简单来说,Float 为单精度,内存中占 4 个字节,有效数位是 7 位(因为有正负,所以不是8位),在我的电脑且 VC++6.0 平台中默认显示是6位有效数字;double为 双精度,占 8 个字节,有效数位是 16 位,但在我的电脑且 VC++6.0 平台中默认显示同样是 6 位有效数字
例子:在 C 和 C++ 中,如下赋值语句:
float a=0.1;
编译器报错:warning C4305: ‘initializing’ : truncation from 'const double ’ to 'float ’
原因: 在 C/C++ 中(也不知道是不是就在 VC++ 中这样),上述语句等号右边 0.1,我们以为它是个 float,但是编译器却把它认为是个 double(因为小数默认是 double),所以要报这个 warning,一般改成 0.1f 就没事了。
本人通常的做法,经常使用 double,而不喜欢使用 float。
C 语言和 C# 语言中,对于浮点类型的数据采用单精度类型 float 和双精度类型 double 来存储,float 数据占用 32bit, double 数据占用 64bit,我们在声明一个变量 float f= 2.25f 的时候,是如何分配内存的呢?如果胡乱分配,那世界岂不是乱套了么,其实不论是 float 还是 double 在存储方式上都是遵从 IEEE 的规范 的,float 遵从的是 IEEE R32.24 ,而 double 遵从的是 R64.53。
无论是单精度还是双精度在存储中都分为三个部分:
1.符号位(Sign):0 代表正,1 代表为负。
2.指数位(Exponent):用于存储科学计数法中的指数数据,并且采用移位存储。
3.尾数部分(Mantissa):尾数部分。
先看代码:
#include
using namespace std;
int main() {
int ge,shi,n;
cin>>n;
shi=n/10;
ge=n%10;
cout<<g*10+shi;
return 0;
}
这是一个讲个位和十位交换的代码
6、7行是不是看不懂呀!
6:shi=n/10;的意思是把n除以十的商赋值给shi
7:ge=n%10;的意思是把n除以十的余数赋值给ge
在数学中‘%’是百分号如100%相当于1
c++中‘%’是取余运算符,也叫模运算符,例如:
15%2的值是1
14%2的值是0
38%10的值是8
(自己解释不清楚,引用部分转载一下:https://baike.so.com/doc/7103239-7326232.html)
ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言。它是现今最通用的单字节编码系统,并等同于国际标准ISO/IEC 646。
请注意,ASCII是American Standard Code for Information Interchange缩写,而不是ASC2,有很多人在这个地方产生误解。
重要字符ASICC码表(不是全部,可以以此类推):
| 字符 | ASICC值 |
|---|---|
| 空格 | 32 |
| 0 | 48 |
| 9 | 57 |
| A | 65 |
| Z | 90 |
| a | 97 |
| z | 122 |
字符型是以ASCII码存储的,它的存储形式就与整数的存储形式类似。
字符型质保函一个字符,用一对单引号括起来,如:‘+’,‘a’,‘7’,‘#’,'@'等,用char定义。
题目:输入一个小写字母,输出他相对的大写字母;
讲解:字符A的ASCII码值是65,a是97,两者相差32。
代码:
#include
using namespace std;
int main() {
char n;
cin>>n;
n-=32;//相当于n=n-32
cout<<n;
return 0;
}
输出:a
输出:A
上机练习:http://ybt.ssoier.cn:8088/problem_show.php?pid=1004
在日常生活中,有许多判断,如成绩大于等于60时为及格,一个数是2的倍数时为偶数……
像n>=60,n%2==0,称为关系运算符,如下:
>//大于
>=//大于等于
==//等于
<=//小于等于
<//小于
!=//不等于
关系表达式的值是一个逻辑值,即’真’或’假’。
如果条件成立,值为’真’,相对,值为’假’。
c++中,'真’表示非0,'假’表示0。
if语句的格式(1):
if(表达式) 语句;
if语句的格式(2):
if(表达式){
语句1;
语句2;
……;
语句n;
}
例题:
输入一个人的智商(IQ),如果>140,输出“天才”。
代码:
#include
using namespace std;
int main() {
int IQ;
cin>>IQ;
if(IQ>140) cout<<"天才";
return 0;
}
运行结果:
①
输入:141
输出:天才
②
输出:1
(无输出)
if语句格式(3):
if(表达式1)
语句1;
else
语句2;
if:如果
else:否则
解释:
如果满足表达式1,执行语句1
如果不满足表达式1,执行语句2。
先看代码:
#include
#include //string类型头文件
using namespace std;
int main() {
string a="Minecraft__Himyyds";
cout<<a;
return 0;
}
输出:
Minecraft__Himyyds
解析:
字符串是夹在双引号里的一串字符,个数可以是0、1或多个。
如:“”,“1”,“114514”,“yyds”,"Minecraft__Him"等都是字符串,使用时必须引入头文件:
#include
const的格式:
const 数据类型 变量名=值;
在定义变量时,如果加上关键字:const,则变量的值就无法改变,否则编译错误,这种变量称为常量,在c++中,常量又称只读变量。
比如:
#include
#include
using namespace std;
int main() {
const string IOKIOI="IOKIOI!";
string a;
a=IOKIOI;
cout<<a;
return 0;
}
输出:
IOKIOI!
bool是一种数据类型,它只有两个值:false(0(假)),true(1(真))。
例1:
#include
using namespace std;
int main() {
bool a=true;
cout<<a;
return 0;
}
运行结果:
1
例2:
#include
using namespace std;
int main() {
bool a=false;
cout<<a;
return 0;
}
运行结果:
0
题目:
输入一个数,如果是3和5的公倍数,输出YES,否则输出NO
代码:
#include
using namespace std;
int main() {
int n;
cin>>n;
if(n%3==0&&n%5==0){
cout<<"YES";
}
else{
cout<<"NO";
}
return 0;
}
讲解:
这个问题有2个条件:能被3整除,能被5整除,即n%3=0,n%5=0都要满足,这是要用到表示“而且”的逻辑运算符&&(逻辑与),代码为:
n%3==0&&n%5==0;
逻辑运算符有3个:
| 逻辑运算符 | 名字 |
|---|---|
| && | 逻辑与 |
| 两根竖线(Markdown显示不出来) | 逻辑或 |
| ! | 逻辑非 |
非的级别最高,或的级别最低
即:
!→&&→||
代码:
#include
using namespace std;
int main() {
int a,b,t;
cin>>a>>b;
if(a>b){//交换
t=a;
a=b;
b=t;
}
cout<<a<<" "<<b;
return 0;
}
输入:
10 20
输出:
10 20
代码:
#include
using namespace std;
int main() {
int a,b,t;
cin>>a>>b;
if(a<b){//交换
t=a;
a=b;
b=t;
}
cout<<a<<" "<<b;
return 0;
}
输入:
10 20
输出:
20 10
求最小值:
#include
using namespace std;
int main() {
int a,b,c,min=100000000;//先赋初值,这样和下面a比较时条件一定是成立的。
cin>>a>>b>>c;
if(min>a) min=a;
if(min>b) min=b;
if(min>c) min=c;
cout<<min;
return 0;
}
求最大值:
#include
using namespace std;
int main() {
int a,b,c,max=-100000000;//先赋初值,这样和下面a比较时条件一定是成立的。
cin>>a>>b>>c;
if(max<a) max=a;
if(max<b) max=b;
if(max<c) max=c;
cout<<max;
return 0;
}
rand()函数的返回值是一个≥0&&≤RAND_MAX的随机整数。
RAND_MAX是一个符号常量,符号常量就是用标识符来代替常量(不懂正常),它的值与操作系统、编译器等有关,Windows下的Dev-C++中的值为32767,Linux下的g++中的值为2147483674。
在使用rand()函数前,要包含:
#include
头文件。
如果需要每次运行的结果不同,则需要srand()来设置随机种子:
srand(time(0));
还要包含:
#include
头文件(好烦啊!)
重点:
要生成一个[a,b]的随机数,要用公式:
rand()%(b-a+1)+a;
比如:
#include
#include
#include
using namespace std;
int main() {
srand(time(0));
cout<<rand()%5+1;
return 0;
}
可以生成一个[1,5]的随机数。
s w i t c h switch switch语句格式:
switch(表达式){
case 常量表达式1:语句1;break;
case 常量表达式2:语句2;break;
……
case 常量表达式n:语句n;break;
default:语句n+1;break;
switch:条件选择;开关
case:情况
break:终止
default:默认;系统默认值
解释:
先计算switch表达式的值,当表达式的值与某一个case子串中的常量表达式的值相匹配时,执行case子串中的内嵌语句,并顺序执行之后的所有语句,直到遇到break,如果所有case子串中的常量表达式值都不与switch表达式的值相匹配,执行default子串的内嵌语句。
例题:
输出10遍“有关必回”,一行一个
讲解:
这题按照原来的思路的话,代码是:
#include
using namespace std;
int main() {
cout<<"有关必回"<<endl;
cout<<"有关必回"<<endl;
cout<<"有关必回"<<endl;
cout<<"有关必回"<<endl;
cout<<"有关必回"<<endl;
cout<<"有关必回"<<endl;
cout<<"有关必回"<<endl;
cout<<"有关必回"<<endl;
cout<<"有关必回"<<endl;
cout<<"有关必回"<<endl;
return 0;
}
输出:
有关必回
有关必回
有关必回
有关必回
有关必回
有关必回
有关必回
有关必回
有关必回
有关必回
那也太烦了!
这时我们可以用到for语句。
常用格式:
for((数据类型)循环变量=初值;循环条件;循环变量增值){//数据类型可以在这里定义,也可在前面定义
语句1;
语句2;
……
语句n;
}
过程:
1:执行循环变量赋初值语句
2:判断循环条件,如果成立,执行,否则,结束
3:执行循环变量增值语句
4:转回2继续执行
5:循环结束,执行下一条语句
更改代码:
#include
using namespace std;
int main() {
for(int i=1;i<=10;i++){
cout<<"有关必回"<<endl;
}
return 0;
}
输出:
有关必回
有关必回
有关必回
有关必回
有关必回
有关必回
有关必回
有关必回
有关必回
有关必回
例题:输出1+2+3……+98+99+100的和
代码:
#include
using namespace std;
int main() {
int i,sum=0;;//求和sum清零
for(i=1;i<=100;i++){//从1~100循环
sum+=i;//累加,相当于sum=sum+i;
}
cout<<sum;//输出sum的值
return 0;
}
运行结果:
5050
例题:
输出
2
1
2^1
21~
2
64
2^{64}
264次,并输出它他们的和。
代码:
#include
using namespace std;
int main() {
long long sun=0,n=1;//从NOIP2010起,c++long long允许使用
int i;
for(i=1;i<=64;i++){
n*=2;//相当于n=n*2
sum+=n;//相当于sum=sum+n
cout<<i<<" "<<n<<endl;
}
cout<<sum;
return 0;
}
运行结果:
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
……(省略亿点点)
62 4611686018427387904
63 -9223372036854775808
64 0
-2
解释:
为什么会这样呢?因为数据实在太大了,超出了long long的范围,造成了数据溢出,但是在编译运行时并不报错。
long long 的范围是 − 9223772036854775808 -9223772036854775808 −9223772036854775808~ 9223772036854775807 9223772036854775807 9223772036854775807就是 − 2 63 -2^{63} −263到 2 63 − 1 2^{63}-1 263−1,到后面学了高精度才能解决。
int的范围是: − 2 31 -2^{31} −231~ 2 31 − 1 2^{31}-1 231−1
b r e a k break break是结束整个循环, c o n t i n u e continue continue是只结束本次循环。
long的范围和int是一样的,即 − 2 31 -2^{31} −231~ 2 31 − 1 2^{31}-1 231−1,但不是long long的缩写!!!
上册就到此为止了 , 再见! 上册就到此为止了,再见! 上册就到此为止了,再见!
这个单元内容有点少,不理解请在评论区提问 这个单元内容有点少,不理解请在评论区提问 这个单元内容有点少,不理解请在评论区提问
题目:
使用while循环输出1~100这些数字
讲解:
while:当……的时候
while循环的特点是先判断表达式,然后执行语句,形式一般为:
while(表达式){
语句1;
语句2;
……
语句n;
}
那么此题的代码为:
#include
using namespace std;
int main() {
int i=1;
while(i<=100){
cout<<i<<" ";
i++;
}
return 0;
}
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
while(1)中的条件是1,它的值永远为真,如果循环中没有break语句来结束循环,就会造成死循环。
一些永远成立的表达式也会成为死循环。
如:
1>0
true
!false
举个栗子:
#include
using namespace std;
int main() {
while(1) {
cout<<"点赞,关注,收藏!"<<endl;
}
return 0;
}
这个代码会永无止境地输出:点赞,关注,收藏!
do-while语句格式:
do{
语句1;
语句2;
……
语句n;
}while(表达式);//分号别忘了!!!
语句特点:
先执行循环里的语句,在判断条件是否成立,成立的话继续执行,不成立的话结束循环!
题目:输出一行五个+。
这题很简单,只要用for循环就可以了。
代码如下:
#include
using namespace std;
int main() {
int i;
for(i=1;i<=5;i++){
cout<<"*";
}
return 0;
}
运行结果:
+++++
但如果我把题目改成:每次输出n,每行五个+,那该怎么做呢?
这时我们要用到多重循环。
代码如下:
#include
using namespace std;
int main() {
int i,j,n;
cin>>n;
for(i=1;i<=n;i++){
for(j=1;j<=5;j++){
cout<<"+";
}
cout<<endl;
}
return 0;
}
输出:2
输出:
+++++
+++++
过程:
| 变量i | 变量j | 循环体执行情况 |
|---|---|---|
| 1 | 1 | 输出+ |
| 1 | 2 | 输出+ |
| 1 | 3 | 输出+ |
| 1 | 4 | 输出+ |
| 1 | 5 | 输出+ |
| 1 | 6 | 退出内循环,换行 |
| 2 | 1 | 输出+ |
| 2 | 2 | 输出+ |
| 2 | 3 | 输出+ |
| 2 | 4 | 输出+ |
| 2 | 5 | 输出+ |
| 2 | 6 | 退出内循环,换行 |
| 4 | 退出内循环 | 退出内循环 |
我们来看一下,这段代码是不是双重循环:
for(i=1;i<=n;i++)
cout<<"+";
for(j=1;j<=5;j++)
cout<<"+";
cout<<endl;
这两个循环不是并列关系,不是嵌套关系,所以不是双重循环。
双重循环输出图形
双重循环输出图形
双重循环输出图形
例题:
使用双重循环,输出:
*
***
*****
*******
*********
代码:
#include
#include //使用setw函数,需要用到iomanip头文件
using namespace std;
int main() {
int i,j;
for(i=1;i<=5;i++){//控制外循环行数
cout<<setw(6-i)<<'';//setw下面讲,制第一个星号的位置
for(j=1;j<=i*2-1;j++){//内循环控制每行的个数
cout<<'*';
}
cout<<endl;
return 0;
}
输出:
*
***
*****
*******
*********
s
e
t
w
(
)
函数
setw()函数
setw()函数
例题:
在屏幕上输出
11
+
451
11+451
11+451的竖式计算。
代码:
#include
#include //使用setw函数,需要用到iomanip头文件
using namespace std;
int main() {
int a,b,s;
a=11;
b=451;
s=a+b;//s=561
cout<<setw(10)<<a<<endl;
cout<<setw(4)<<'+'<<setw(6)<<b<<endl;
cout<<"-----------"<<endl;
cout<<setw(10)<<s<<endl;
return 0;
}
输出:
11
+ 451
-----------
561
讲解:
使用setw函数前,须有头文件#include
使用时默认为右对齐。
使如果输出数据所需的宽度比设置的宽小,默认用空格填充。
如果输出数据所需的宽度比设置的域宽大,输出数并不会被截断,系统会输出所有位。
例如setw(10),在输出时分配了10个字符的宽度,而变量a的长度是2,则在前面补8个空格。
例题:输出九九乘法表
#include
#include //用到setw()函数
using namespace std;
int main() {
int i,j;
for(i=1;i<=9;i++){//外循环,从1~9循环输出后一个乘数
for(j=1;j<=i;j++){//内循环,从1~i循环输出前一个乘数
cout<<j<<'*'<<i<<'='<<setw(2)<<i*j<<" ";
}
cout<<endl;
}
return 0;
}
运行结果:
1×1= 1
1×2= 2 2×2= 4
1×3= 3 2×3= 6 3×3= 9
1×4= 4 2×4= 8 3×4=12 4×4=16
1×5= 5 2×5=10 3×5=15 4×5=20 5×5=25
1×6= 6 2×6=12 3×6=18 4×6=24 5×6=30 6×6=36
1×7= 7 2×7=14 3×7=21 4×7=28 5×7=35 6×7=42 7×7=49
1×8= 8 2×8=16 3×8=24 4×8=32 5×8=40 6×8=48 7×8=56 8×8=64
1×9= 9 2×9=18 3×9=27 4×9=36 5×9=45 6×9=54 7×9=63 8×9=72 9×9=81
例题:
输入五个同学的成绩,再输入一个学号,输出他的成绩。
输入五个同学的成绩,再输入一个学号,输出他的成绩。
输入五个同学的成绩,再输入一个学号,输出他的成绩。
思路:
可以用
a
1
a_1
a1~
a
5
a_5
a5分别保存
1
1
1 ~
5
5
5号同学的成绩。
代码:
#include
using namespace std;
int main() {
int a1,a2,a3,a4,a5,n;
cin>>a1>>a2>>a3>>a4>>a5;
cin>>n;
switch(n) {
case 1:cout<<a1;break;
case 2:cout<<a2;break;
case 3:cout<<a3;break;
case 4:cout<<a4;break;
case 5:cout<<a5;break;
default:cout<<"学号不存在!";
}
return 0;
}
运行结果:
输入:
11 45 14 19 19
1
输出:
11
如果需要储存45位同学的成绩,那么就需要45个变量来保存成绩。而且输出时需要判断46种情况。有没有简便的方法呢?有的c++提供了数组
定义一维数组的一般形式:
数据类型 数组名[常量表达式];
如int a[10];就定义了一个数组a,其包含a[0],a[1],a[2]……,a[9]10个数组元素。
数组名的命名规则和变量名相同,遵循标识符命名规则,在数组定义的用[]括起来的常量表达式的值表示元素的个数,下标从0开始,上例中的最后一个元素是a[9],而不是a[10],数组往往和循环结合使用。
改进代码:
#include
using namespace std;
int main() {
int a[6],n,i;//用a[i]保存i号同学的成绩,a[0]暂不用
for(i=1;i<=5;i++) {//输入5位同学的成绩
cin>>a[i];
}
cin>>n;//输入学号
if(n>=1&&n<=5){
cout<<a[n];//输出n号同学的成绩
}
else{
cout<<"学号不存在!";
}
return 0;
}
运行结果:
输入:
11 45 14 19 19
1
输出:
11
创作整理不易,每个字都是博主的心血,如果对您有所帮助,还望多点赞、收藏☚
欢迎在评论区留下您宝贵的建议😉😉😉