C++语法篇 - D01
2025-02-11 12:03:36
发布于:浙江
1.基本框架
(1)头文件:#include<bits/stdc++.h>//万能头,可以只写这个
(2)命名空间
(3)主函数:有且只有一个
#include<bits/stdc++.h>
using namespace std;
int main(){
return 0;
}
2.数据类型
2.1整型(integer)
(1)int:整型 范围:-21亿~21亿 -2^31 ~ 2^31-1
(2)long long:长长整型 范围:-10^18 ~ 10^18 左右
int a=2100000000
int b=-2100000000
long long c=1000000000000000000
long long d=-1000000000000000000
2.2浮点型
(1)float:单精度浮点型(基本不用)
(2)double: 双精度浮点型(小数就用这个)
float e=0.12345678
double f= 0.1234567890123456
2.3字符型
(1)char:定义单个字符
字符:字母、数字、符号等用 单引号 包起来的 单个符号
char a='A';
(2)ASCII码
'A'=65 'a'=97 '0'=48 ' '=32
char a=65;
cout<<a;
//输出为'A'
2.4 字符串类型 string s;
(1) 头文件 #include<string>
(2) 计算长度 s.size(); s.length();
#include<string>
……
string s;
cin>>s;
b=s.size()//第一种获取长度方式
……
(3)输入字符串
a)cin>>s;(不带空格)
b)getline(cin,s);(带空格)
string s;
//设输入无空格
cin>>s;
//设输入有空格
getline(cin,s);
(4)比较大小:字典序(从大到小)
比较方式:按位一个个比较过去,直到第一个不相同,谁大就谁大
"123" < "2" "121" > "12"
3.输入输出
3.1 输入
(1)单个变量或者字符串: cin >> 变量名;
#include<bits/stdc++.h>
using namespace std;
int main(){
int a;
cin >> a
return 0;
}
(2)数组:循环输入
#include<bits/stdc++.h>
using namespace std;
int a[100];
int main(){
for(int i=0;i<数组长度;i++){
cin>>a[i];
}
return 0;
}
3.2输出
(1) cout<<endl;
(2) printf:格式化输出
a) 小数:printf("%.2f",变量名); 保留两位小数
b) 右对齐-空格:printf("%5d",123);
c) 右对齐-补0:printf("%05d",123);
#include<bits/stdc++.h>
using namespace std;
int main(){
double a;
cin>>a;
printf("%.2f",a);
printf("%5d",123);
printf("%05d",123)
return 0;
}
4.条件分支语句
(1)单分支语句:如果条件成立,执行语句
if(条件){
语句;
}
#include<bits/stdc++.h>
using namespace std;
int main(){
int day = 10;
if(day==0){
cout<<"张斌真帅"<<endl;
}
return 0;
}
(2)双分支语句:如果条件成立,执行语句1;否则语句2
if(条件){
语句1;
}
else{
语句2;
}
#include<bits/stdc++.h>
using namespace std;
int main(){
int day = 10;
if(day==10){
cout<<"张斌真帅";
}
else{
cout<<"zhangbinzhenshuai";
}
return 0;
}
(3)三目运算符:条件?语句1:语句2
如果条件成立,执行语句1;否则语句2
例如:
#include<bits/stdc++.h>
using namespace std;
int main(){
int day = 10;
day == 10?cout<<"老师真帅";cout<<"张斌真帅";
return 0;
}
(4)多分支语句
if(){
}else if(){
}else if(){
}else{
}
5.循环
(1)for循环
for(初始值;条件;变化量){
循环体;
;
}
初始值 -> 条件 -> 循环体 -> 变化量 -> …
#include<bits/stdc++.h>
using namespace std;
int main(){
for(int i=1;i<=10;i++){
cout<<"张斌真帅!!!"<<endl;
}
return 0;
}
(2)while循环
while(条件){
循环语句;
}
#include<bits/stdc++.h>
using namespace std;
int main(){
int day = 10;
while(day==10){
cout<<"张斌真帅"<<endl;
}
return 0;
}
(3)do-while:相比while循环,如果条件不成立,至少执行一次
#include<bits/stdc++.h>
using namespace std;
int main(){
do{
cout<<"张斌真帅"<<endl;
}while(0);
return 0;
}
6.数组
(1)定义:变量类型 数组名[数组空间]; //数组空间=n的最大值+5~10
#include<bits/stdc++.h>
using namespace std;
int main(){
//设n为100
int a[105]
}
(2)遍历范围:0~n-1 或者 1-n => 注意循环范围
7.数位分离
(1)分离各位与求和
#include<bits/stdc++.h>
using namespace std;
int main(){
int n=12345,s=0;
s+=n%10;//个位
n/=10;
}
-
函数(巨烦的啊啊啊!!!)
(1)函数声明 - 省略
(2)函数定义
函数类型 函数名(变量类型 变量1,变量类型 变量名2,…){
函数内容;
}
函数类型 - 返回(int/double/string…) 无返回(void)
(3)函数调用
函数名(变量名1,变量名2,…) => 注意:类型需要一一对应 -
sort
(1)头文件:algorithm
(2)模板:sort(数组名+开始下标,数组名+结束下标+1,排序方式)
(3)升序:从小到大 sort(a,a+n);
(4)降序:从大到小
#include<bits/stdc++.h>
using namespace std;
bool cmp(int x,int y){
return x>y;
}
int main(){
……
sort(a,a+n,cmp);
……
}
- 结构体
struct 结构体类型名{
变量类型 变量名1;
变量类型 变量名2;
变量类型 变量名3;
}结构体类型 结构体变量名;
例如:
#include<bits/stdc++.h>
using namespace std;
struct student{
string name;
int id;
int cls;
int age;
} a[105],b,c;
int main(){
……
return 0;
}
//等同于 student a[105],b,c => 类似于 int a[105],b,c;
- 结构体排序
/*
题目:学生有语文、数学、英语成绩,总分,输入顺序是学号
(1)根据成绩从小到大
(2)如果成绩相同,语文成绩从大到小
(3)如果语文成绩从小到大
*/
#include<bits/stdc++.h>
using namespace std;
struct student{
int yw,sx,yy,sum,id;
}a[305];
bool cmp(student x,student y){
//1.如果成绩不同,成绩从大到小
if(x.sum!=y.sum) return x.sum>y.sum;
//2.如果成绩相同,如果语文成绩不相同,语文成绩从大到小
else if(x.yw!=y.yw) return x.yw>y.yw;
//3.如果语文成绩相同,学号从小到大
else return x.id<y.id;
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].yw>>a[i].sw>>a[i].yy;
a[i].id=i;
a[i].sum=a[i].yw+a[i].sw+a[i].yy;
}
sort(a+1,a+n+1,cmp);
return 0;
}
全部评论 3
顶
2025-03-18 来自 浙江
0d
2025-03-18 来自 浙江
0d
2025-03-18 来自 浙江
0
有帮助,赞一个