开头

放假一周了,前两天我开始入门C艹,到现在学习了三天。IDE为了方便选择了VScode(之前js脚本和html代码都是用这个写的),
看的是 黑马程序员 的课,目前个人认为讲的挺细致

凭借五年级时学习python的老底,在第三天我学到了 47 数组-二维数组定义方式 这一课。

晒一下目前的成果
已经做成的题目

代码展示

接下来随便发几段写的练手题(欢迎指正)

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
// (穷举大法好)
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "请输入A、B、C三只小猪的体重: ";
cin >> a >> b >> c;
// 如果a=b=c,那么返回全部相等
if (a == b && b == c) {
cout << "三只小猪体重完全相等" << endl;
}
// 如果不,就看是不是a>b=c
else if (a > b && b == c) {
cout << "A最重" << endl;
}
// 不是就看a<b=c,是就返回b、c并列最重
else if (a < b && b == c) {
cout << "B和C并列最重" << endl;
}
// 不是就看a=b>c,ab最重
else if (a == b && b > c) {
cout << "A和B并列最重" << endl;
}
// a<b>c,b最重
else if (a < b && b > c) {
cout << "B最重" << endl;
}
// a>b>c,a最重
else if (a > b && b > c) {
cout << "A最重" << endl;
}
// a<b<c,c最重
else if (a < b && b < c) {
cout << "C最重" << endl;
}
// a=b<c,c最重
else if (a == b && b < c) {
cout << "C最重" << endl;
}
// a>b<c,此时判断a是否大于c
else if (a > b && b < c) {
if (a > c) {
cout << "A最重" << endl;
}
else if (a == c) {
cout << "A和C并列最重" << endl;
}
else {
cout << "C最重" << endl;
}
}
system("pause");
return 0;

// ——————————————————————————————————————————
// ——————————————————————————————————————————
//运用三目运算符,不考虑相同重量:
// int main() {
// cout << "分别输入三只小猪的体重:\n";
// cin >> A >> B >> C;
// result = (A > B) ? ((A > C) ? 'A' : 'C') : ((B < C) ? 'C' : 'B');
// cout << result << "小猪最重!" << 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
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
126
#include <iostream>
using namespace std;

int main() {

while (true) {

cin.clear();
cout << "请按下回车以进入计算器界面..." << endl;
cin.ignore(10000, '\n');

int choice = 0;
float bmi = 0.0f;
float result = 0.0f;

//用户菜单
cout << "===== 可达鸭战神の计算器 =====" << endl;
cout << "1. 普通四则运算(加减乘除)" << endl;
cout << "2. 取模运算(求余数)" << endl;
cout << "3. 健康BMI计算" << endl;
cout << "4. 退出计算器" << endl;
cout << "---------------------" << endl;
cout << "请选择功能(请输入 1、2 3或4):" << endl;
cin >> choice;

//判断是否退出
if (choice == 4) {
cout << "感谢您使用可达鸭战神の计算器,再见!";
break;
}

//普通四则运算
else if (choice == 1){
int num1;
int num2;

cout << "请输入第一个整数:" << endl;
cin >> num1;
cout << "请输入第二个整数:" << endl;
cin >> num2;

//四则运算结果展示
cout << "计算结果:" << endl;
cout << "加法结果为:" << num1 + num2 << endl;
cout << "减法结果为:" << num1 - num2 << endl;
cout << "乘法结果为:" << num1 * num2 << endl;
result = (float)num1 / (float)num2;
// 除数为0处理
if (num2 == 0){
cout << "警告:除数不能为0,除法运算已跳过!" << endl;
}
else {
cout << "除法结果为:" << result << endl;

}
}

//取模运算
else if (choice == 2) {
int num1;
int num2;

cout << "请输入第一个整数:" << endl;
cin >> num1;
cout << "请输入第二个整数:" << endl;
cin >> num2;

// 除数为0处理
if (num2 == 0) {
cout << "除数不能为0,无法进行取模运算!" << endl;
}

else {
cout << "取模(取余)结果为:" << num1 % num2 << endl;
}
}

//BMI计算
else if (choice == 3) {

float height;
float weight;


// 1.身高录入
cout << "请输入你的身高(单位米):";
cin >> height;
cout << "\n您的身高为:" << height << "米" << endl;


// 2.体重录入
cout << "请输入您的体重(单位为公斤):";
cin >> weight;
cout << "\n您的体重是:" << weight << "公斤" << endl;


// 3.健康状态判断与登记
bmi = weight / (height * height);
cout << "\n您的BMI指数为:" << bmi << "," << endl;
if (bmi < 18.5){
cout << "过轻了!多吃点东西哦~";
}
else if (18.5 <= bmi && bmi < 24.0) {
cout << "健康哦~~继续保持!";
}
else if (24.0 <= bmi && bmi < 28.0){
cout << "有点儿超重咯~一起来锻炼吧!!";
}
else {
cout << "emmm,多锻炼,控制饮食,详细你可以滴~";
}
cout << endl;
}

//特殊选择处理
else{
cout << "\n错误的数字!" << endl;
cout << "请输入1~4之间的数字来使用相应功能!" << endl;
}

//循环启动逻辑
cout << "\n按回车键初始化运行..." << endl;
cin.ignore();
cin.get();
}
}

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
#include <iostream>
using namespace std;
//黑马答案
int main () {
int arr[] = {2,4,6,5,9};
int start = 0;
int temp = 0;
int end = sizeof(arr) / sizeof(arr[0]) - 1;

while (start < end) //当start < end时才需要逆置
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++; //每逆置一组元素,起始下标往前进一位
end--; //结束下标往后退一位
}
for (int i = 0; i < 5; i++)
{
cout << arr[i] << endl;
}
}


// 我的
// int main() {

// int arr[] = {6,1,9,7,2};
// int start = 0; //起始下标
// int temp = 0;
// int end = sizeof(arr) / sizeof(arr[0]) - 1; //结束下标
// int y = sizeof(arr) / sizeof(arr[0]) / 2;

// for (int i = 0; i < y; i++)
// {
// temp = arr[start];
// arr[start] = arr[end];
// arr[end] = temp;
// start++;
// end--;
// }
// cout << "逆置后数组顺序为:\n";

// for (int j = 0; j < (sizeof(arr) / sizeof(arr[0]) ); j++)
// {
// cout << arr[j] << endl;
// }
// }

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

int main() {
// 冒泡排序要点
// 排序总轮数 = 元素个数 - 1 (总0开始计算)
// 对比次数 = 元素个数 - 当前排序行数 - 1
int arr[] = {5,6,4,1,2,9,4,3,6,4,7,8,2,1,0};
int all = sizeof(arr) / sizeof(arr[0]);
bool flag = false; //设置标志位,若一轮排序中没有发生交换,则说明已经有序

for (int i = 0; i < all - 1; i++) //外层循环,总共对比的轮数
{
flag = false; //每一轮开始时都重置标志位
for (int j = 0; j < all - i - 1; j++) //内层循环,每轮对比的次数
{
if (arr[j] > arr[j+1]) { //用代表对比次数的j进行计算,否则一整轮只会对比一组数字
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
flag = true; //发生交换,说明数组已经有序
}
}
if (flag == false) { //如果该轮比较标志位为false则说明没有发生交换行为,已有序,直接跳出循环
break;
}
}
for (int k = 0; k < all; k++)
{
cout << arr[k] << endl;
}
}

听说指针不简单,我倒是有点迫不及待了🙈

其它

明后天找机会看看随便出个什么教程,不然这博客真的太空了,没干货啊
这四个多月,积累还是有一些的(🤔🤔🤔