break语句

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
#include<iostream>
using namespace std;

int main()
{
//1.出现在swich语句中
cout << "请选择副本难度" << endl;
cout << "1.普通难度" << endl;
cout << "2.中等难度" << endl;
cout << "3.困难难度" << endl;
system("pause");
int select = 0;//创建选择变量的结果
cin >> select;//等待用户输入
switch (select)
{
case 1:
cout << "您选择的是简单难度" << endl;
break;//退出swich语句
case 2:
cout << "您选择的是中等难度" << endl;
break;//退出swich语句

case 3:
cout << "您选择的是困难难度" << endl;
break;//退出swich语句

default:
break;
}
system("pause");
return 0;

}

continue语句

输出0-100的奇数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using namespace std;
int main()
{
//contiue 输出0-100的奇数
for (int i = 0; i <= 100; i++)
{
if (i % 2 == 0)
{
continue;
}
cout << i << endl;
}
system("pause");
return 0;
}

数组定义方式

定义方式1

image-20220111215324734

定义方式2

image-20220111214751256

定义方式3

image-20220111215209779


数组名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
cout << "整个数组占用的内存空间为:" << sizeof(arr)<<endl;
cout << "每个元素所占内存空间为:" << sizeof(arr[0])<<endl;
cout << "数组中有多少个元素:" << sizeof(arr) / sizeof(arr[0]) << endl;
//一个int整型占4个字节


//可以通过数组名查看数组首地址
cout << "数组首地址为:" << arr << endl;
//地址一般为16进制,若想将其变成十进制,则改成(int)arr
cout << "数组中第一个元素的地址为:" << (int)&arr[0] << endl;
cout << "数组中第二个元素的地址为:" << (int)&arr[1] << endl;

//数组名是个常量,不可以进行赋值

system("pause");
return 0;


}

image-20220111221809400


Array 和 Vector

Vector

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
#include <iostream> //Vector
#include <iomanip>
#include <array>
#include <vector>
using namespace std;
void showVector(vector<int> v)
{
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
}

int main()
{
vector<int> a;
cout << "At the beginning, a.size() = " << a.size() << endl;

for (int i = 1; i <= 10; i++)
{
a.push_back(i+1); //增加一个单位长度并将其中的值赋值为i+1
cout << "After i = " << i << " , a.size() = " << a.size() << endl;
}

showVector(a);

for (int i = 1; i <= 5; i++)
{
a.pop_back(); //减少一个单位长度
cout << "After i = " << i << " , a.size() = " << a.size() << endl;
}

showVector(a);

a.resize(10); //直接将数组长度变为10
cout << "After a.resize(10): " << endl;
showVector(a);

a.resize(3);
cout << "After a.resize(3): " << endl;
showVector(a);
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
int main()
{
vector<int> a(3,2); //将数组a中的3个元素赋值为2
vector<int> b(4,5);
showVector(a);
showVector(b);
b = a;
cout << "After b = a :" << endl;
showVector(a);
showVector(b);
return 0;
}
int main7()
{
vector<int> a;//长度为0
vector<int> b(3);//长度为3,默认值都为0
vector<int> c(4,5);//长度为4,初始值都为5
vector<int> d = {1,2,3,4,5};//长度为初始值数量5
cout << "a.size() = " << a.size() << endl;
cout << "b.size() = " << b.size() << endl;
cout << "c.size() = " << c.size() << endl;
cout << "d.size() = " << d.size() << endl;
vector<int>a(b);//将b的值copy到a中
return 0;
}

Array

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//array: <array> , 定长数组 
//常用操作:整体赋值,size(),fill(),swap()

void showMatrix(int mat[][3], int rowNum)
{
for(int i = 0; i < rowNum; i++)
{
for(int j = 0; j < 3; j++)
{
cout << mat[i][j] << " ";
}
cout << endl;
}
}

void showMatrix(array< array<int,3> , 2 > mat)
{
for(int i = 0; i < mat.size(); i++)
{
for(int j = 0; j < mat[i].size(); j++)
{
cout << mat[i][j] << " ";
}
cout << endl;
}
}


int main6()
{
int mat[2][3] = {1,2,3,4,5,6};
array< array<int,3> , 2 > a = {11,12,13,14,15,16};
//array< array< array<int,4> ,3> , 2 > b;

//showMatrix(mat, 2);
showMatrix(a);

/*
cout << "a.size() = " << a.size() << endl;
cout << "a[0].size() = " << a[0].size() << endl;
cout << "a[1].size() = " << a[1].size() << endl;
cout << a[0][0] << " " << a[0][1] << " " << a[0][2] << endl;
cout << a[1][0] << " " << a[1][1] << " " << a[1][2] << endl;
*/


return 0;
}

void showArray(int arr[], int len)
{
cout << "------------" << endl;
for(int i = 0; i < len; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}

void showArray(array<int,3> arr)
{
cout << "------------" << endl;
for(int i = 0; i < 3; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}

int main5()
{
//array<int,3> a = {1,2,3};
//array<int,3> b = {4,5,6};
const int N = 3;
array<int,N> a = {1,2,3}, b = {4,5,6};

showArray(a);
showArray(b);

//a.swap(b);
b.swap(a);
cout << "After swapping:" << endl;
showArray(a);
showArray(b);

return 0;
}
int main4()
{
array<int,3> a = {0};
cout << "a.size() = " << a.size() << endl;

showArray(a);

a.fill(2);

showArray(a);

return 0;
}

int main3()
{
int a[3] = {1,2,3};
int b[3];
//b = a;//error: invalid array assignment

for(int i = 0; i < 3; i++)
{
a[i] = 2;
}

array<int,3> c = {4,5,6};
array<int,3> d;

showArray(c);
showArray(d);

d = c;
cout << "After d = c:" << endl;
showArray(c);
showArray(d);

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
#include<iostream>
using namespace std;
int main()
{
//打印一个数组
int arr[5]{ 1,4,2,5,3 };
cout << "逆置前数组为:";
for (int i = 0; i < 5; i++)
{
cout << arr[i];
}
int start = 0; //数组第一个元素下标
int end = sizeof(arr) / sizeof(arr[0]) - 1;
while (start < end)
{

//实现逆置
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
//下标更新
start++;
end--;

}
//打印逆置后数组
cout <<endl<< "逆置后数组为:";
for (int i = 0; i < 5; i++)
{
cout << arr[i];
}

system("pause");
return 0;
}

冒泡排序

image-20220112133447593

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
#include<iostream>
using namespace std;
int main()
{
//利用冒泡排序实现升序序列
int arr[9]{ 4,2,8,5,7,1,3,9,0 };
cout << "排序前为:"<<endl;
for (int i = 0; i < 9; i++)
{
cout << arr[i] << " ";
}
cout << endl;
//开始冒泡排序
//外层循环(总轮数为n-1,n为元素个数)
for (int i = 0; i < 9-1; i++)
{
//内循环(每轮排序的对比次数为n-轮数-1)
for (int j = 0; j < 9 - i - 1; j++)
{
//实现交换
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j + 1] = temp;

}
}

}
cout << "排序后数组为:";
for (int i = 0; i < 9; i++)
{
cout << arr[i];
}
cout << endl;

system("pause");
return 0;
}

image-20220112135312575


二维数组

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
#include<iostream>
using namespace std;
int main()
{
//方式一
int arr[2][3]
{
{1,2,3},{4,5,6}
};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr[i][j]<<" ";
}
cout << endl;
}
//方式二
int arr1[2][3]{ 1,2,3,4,5,6 };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}

system("pause");
return 0;
}

打印学生总成绩

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std;
#include<string>
int main()
{
string names[3]={ "王五","李四","张三" };//中间=可不加
int scores[3][3]= //中间=可不加
{
{100,100,100},{90,80,70},{60,80,90}
};
for (int i = 0; i < 3; i++)
{
int sum = 0;
for (int j = 0; j < 3; j++)
{
sum += scores[i][j];
}
cout << names[i] << "的成绩为" << sum << endl;
}
system("pause");
return 0;
}