Skip to content

Commit 5962ac6

Browse files
author
alexc
committed
完成题目。
1 parent d9fcb00 commit 5962ac6

30 files changed

+1318
-6
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@
3232
*.app
3333

3434
build/
35-
CMakeLists.txt
3635
.vscode
3736
cmake-build-debug
37+
.idea

.vscode/settings.json

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,70 @@
11
{
22
"files.associations": {
33
"iostream": "cpp",
4-
"ostream": "cpp"
4+
"ostream": "cpp",
5+
"string": "cpp",
6+
"iosfwd": "cpp",
7+
"__bit_reference": "cpp",
8+
"__config": "cpp",
9+
"__debug": "cpp",
10+
"__errc": "cpp",
11+
"__functional_base": "cpp",
12+
"__hash_table": "cpp",
13+
"__locale": "cpp",
14+
"__mutex_base": "cpp",
15+
"__node_handle": "cpp",
16+
"__nullptr": "cpp",
17+
"__split_buffer": "cpp",
18+
"__string": "cpp",
19+
"__threading_support": "cpp",
20+
"__tree": "cpp",
21+
"__tuple": "cpp",
22+
"algorithm": "cpp",
23+
"array": "cpp",
24+
"atomic": "cpp",
25+
"bit": "cpp",
26+
"bitset": "cpp",
27+
"cctype": "cpp",
28+
"chrono": "cpp",
29+
"clocale": "cpp",
30+
"cmath": "cpp",
31+
"complex": "cpp",
32+
"cstdarg": "cpp",
33+
"cstddef": "cpp",
34+
"cstdint": "cpp",
35+
"cstdio": "cpp",
36+
"cstdlib": "cpp",
37+
"cstring": "cpp",
38+
"ctime": "cpp",
39+
"cwchar": "cpp",
40+
"cwctype": "cpp",
41+
"exception": "cpp",
42+
"functional": "cpp",
43+
"initializer_list": "cpp",
44+
"iomanip": "cpp",
45+
"ios": "cpp",
46+
"istream": "cpp",
47+
"iterator": "cpp",
48+
"limits": "cpp",
49+
"list": "cpp",
50+
"locale": "cpp",
51+
"map": "cpp",
52+
"memory": "cpp",
53+
"mutex": "cpp",
54+
"new": "cpp",
55+
"optional": "cpp",
56+
"ratio": "cpp",
57+
"sstream": "cpp",
58+
"stdexcept": "cpp",
59+
"streambuf": "cpp",
60+
"string_view": "cpp",
61+
"system_error": "cpp",
62+
"tuple": "cpp",
63+
"type_traits": "cpp",
64+
"typeinfo": "cpp",
65+
"unordered_map": "cpp",
66+
"utility": "cpp",
67+
"vector": "cpp",
68+
"fstream": "cpp"
569
}
670
}

.vscode/tasks.json

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
"build/${fileBasenameNoExtension}.out",
1414
"--debug"
1515
],
16-
"group": {
17-
"kind": "build",
18-
"isDefault": true
19-
},
16+
"group": "build",
2017
"presentation": {
2118
"echo": true,
2219
"reveal": "silent",
@@ -25,6 +22,28 @@
2522
"showReuseMessage": true,
2623
"clear": false
2724
}
25+
},
26+
{
27+
"type": "cppbuild",
28+
"label": "C/C++: clang++ 生成活动文件",
29+
"command": "/usr/bin/clang++",
30+
"args": [
31+
"-g",
32+
"${file}",
33+
"-o",
34+
"${fileDirname}/${fileBasenameNoExtension}"
35+
],
36+
"options": {
37+
"cwd": "${fileDirname}"
38+
},
39+
"problemMatcher": [
40+
"$gcc"
41+
],
42+
"group": {
43+
"kind": "build",
44+
"isDefault": true
45+
},
46+
"detail": "调试器生成的任务。"
2847
}
2948
]
3049
}

beads.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
ID: geekr1
3+
TASK: beads
4+
LANG: C++
5+
*/
6+
#include <iostream>
7+
#include <string>
8+
9+
using namespace std;
10+
11+
char neckless[350];
12+
13+
int main()
14+
{
15+
int size;
16+
cin >> size;
17+
cout << "size=" << size << endl;
18+
19+
for (int i = 0; i <= size; i++)
20+
{
21+
char c = getchar();
22+
if(c == ' ')
23+
continue;
24+
25+
neckless[i] = c;
26+
cout << neckless[i] << endl;
27+
}
28+
29+
return 0;
30+
}

matrix_add.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// 矩阵加法
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
int main()
7+
{
8+
int a[3][4], b[3][4];
9+
for (int i = 0; i < 3;i++ ) {
10+
for (int j = 0; j < 4;j++) {
11+
cin >> a[i][j];
12+
}
13+
}
14+
15+
for (int i = 0; i < 3; i++) {
16+
for (int j = 0; j < 4; j++) {
17+
cin >> b[i][j];
18+
}
19+
}
20+
21+
for (int i = 0; i < 3; i++) {
22+
for (int j = 0; j < 4; j++) {
23+
cout << a[i][j] + b[i][j] << " ";
24+
}
25+
cout << endl;
26+
}
27+
28+
return 0;
29+
}

matrix_snake.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// http://oj.ryipedu.com/problem.php?id=1245
2+
// 打印蛇形矩阵 (样例显示其实是一个回形矩阵、环形矩阵)
3+
#include <iostream>
4+
#include <iomanip>
5+
6+
using namespace std;
7+
8+
const int N = 100;
9+
int a[N][N];
10+
11+
int main()
12+
{
13+
int n; // 矩阵的维度
14+
cin >> n;
15+
16+
int index, // 每个格子的编号
17+
i, // 行号
18+
j; // 列号
19+
i = j = 0;
20+
index = 1;
21+
22+
// 模拟回形矩阵的生成过程
23+
while (index <= n*n)
24+
{
25+
// 从左到右生成一行
26+
while(a[i][j] == 0 && j < n)
27+
a[i][j++] = index++;
28+
i++; // 下移一行
29+
j--; // 回退一格
30+
31+
// 从上到下生成一列
32+
while (a[i][j] == 0 && i < n)
33+
a[i++][j] = index++;
34+
i--;
35+
j--;
36+
37+
// 从右到左生成一行
38+
while(a[i][j] == 0 && j >= 0)
39+
a[i][j--] = index++;
40+
i--;
41+
j++;
42+
43+
// 从下到上生成一列
44+
while(a[i][j] == 0 && i >= 0)
45+
a[i--][j] = index++;
46+
i++;
47+
j++;
48+
}
49+
50+
// 输出矩阵
51+
for (i = 0; i < n; i++) {
52+
for (j = 0; j < n;j++){
53+
cout <<setw(3)<< a[i][j] << " ";
54+
}
55+
cout << endl;
56+
}
57+
58+
return 0;
59+
}

matrix_top_right_down.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// 折角矩阵。从左到右、再从上到下。
2+
#include <iostream>
3+
#include <iomanip>
4+
5+
using namespace std;
6+
7+
int main()
8+
{
9+
int n, a[20][20];
10+
cin >> n;
11+
12+
int v = 1;
13+
// 逐行输出。每行分成左右两部分
14+
for (int i = 0; i < n; i++)
15+
{
16+
// 输出左半部分
17+
int j = 0;
18+
for (; j < (n - i); j++)
19+
{
20+
a[i][j] = v++;
21+
cout << setw(4) << a[i][j];
22+
}
23+
24+
// 输出右半部分
25+
for (; j < n; j++)
26+
{
27+
// 前一行相同列的元素+1
28+
a[i][j] = a[i - 1][j] + 1;
29+
cout << setw(4) << a[i][j];
30+
}
31+
32+
// 换行
33+
cout << endl;
34+
// 调整 v 的取值
35+
v = v + n - i - 1;
36+
}
37+
38+
return 0;
39+
}

maxtrix_sort.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <iostream>
2+
#include <math.h>
3+
4+
using namespace std;
5+
6+
int main()
7+
{
8+
int m, n; // m 是行,n 是列
9+
cin >> m >> n;
10+
11+
// 二维数组存放的身高数据
12+
int h[10][10];
13+
for (int i = 0; i < m; i++)
14+
{
15+
for (int j = 0; j < n; j++)
16+
{
17+
cin >> h[i][j];
18+
}
19+
}
20+
21+
// 所有同学的身高一维数组,最多就是 10*10=100个同学
22+
int a[100];
23+
// 一维数组的下标从0开始
24+
int a_index = 0;
25+
// 所有身高的和,用来计算平均身高
26+
int total_height = 0;
27+
28+
// 遍历输入的二维数组
29+
// 注意:两重循环是先遍历列、再遍历行,即 n、m 对调,并且 j、i 对调
30+
for (int i = 0; i < n; i++)
31+
{
32+
int column_max = 0; // 列最大身高
33+
for (int j = 0; j < m; j++)
34+
{
35+
int current = h[j][i];
36+
37+
a[a_index++] = current; // 转成一维数组,注意 a_index++ 表示赋值后下标往后移动
38+
total_height += current; // 累加所有人的身高
39+
40+
if (current > column_max)
41+
{
42+
column_max = current;
43+
}
44+
}
45+
// 1. 输出每一列的最大值
46+
cout << column_max << endl;
47+
}
48+
49+
// 2. 对所有身高进行排序,简单冒泡
50+
for (int i = 0; i < m * n; i++)
51+
{
52+
for (int j = 0; j < m * n; j++)
53+
{
54+
// 从高到低
55+
if(a[j] < a[i]) {
56+
// 交换 a[i] a[j]
57+
int ai = a[i];
58+
a[i] = a[j];
59+
a[j] = ai;
60+
}
61+
}
62+
}
63+
// 从高到低,在一行内输出所有身高
64+
for (int i = 0; i < m * n; i++){
65+
cout << a[i] << " ";
66+
}
67+
cout << endl;
68+
69+
// 3.求平均身高并输出
70+
// round 是四舍五入向上取整的标准库函数
71+
// 乘1.0是为了把整数变成浮点数,这样除以同学的人数时,才有小数点
72+
int average = round((total_height * 1.0) / (m * n));
73+
cout << average << endl;
74+
75+
// 4. 输出不小于平均身高的同学人数
76+
int count = 0;
77+
for (int i = 0; i < m * n; i++)
78+
{
79+
if(a[i] >= average){
80+
count++;
81+
}
82+
}
83+
cout << count << endl;
84+
85+
return 0;
86+
}

0 commit comments

Comments
 (0)