第一题
1.题目描述
2.解决思路交换两个数组的数字其实和交换两个对象的数字原理是一样的,都采用了一个中间变量作为桥梁。其余的工作无非是找出最大的那一行。
3.代码详情:
1 #include
2 using namespace std;3
4 intmain() {5 int arr[3][5];6 inti, j;7 cout << "输入一个三行五列的矩阵" <>arr[i][j];11 }12 }//输入一个三行五列的矩阵
13 int a=0, b=0, c=0;14 for (j = 0; j < 5; j++) {15 a += arr[0][j];16 }17 for (j = 0; j < 5; j++) {18 b += arr[1][j];19 }20 for (j = 0; j < 5; j++) {21 c += arr[2][j];22 }//计算每行元素之和
23 if (b > a && b >c) {24 int arr2[5];//arr2数组用于交换的媒介
25 for (j = 0; j < 5; j++) {26 arr2[j] = arr[0][j];27 arr[0][j] = arr[1][j];28 arr[1][j] =arr2[j];29 }30 }31 if (c > b && c >a) {32 int arr2[5];//arr2数组用于交换的媒介
33 for (j = 0; j < 5; j++) {34 arr2[j] = arr[0][j];35 arr[0][j] = arr[2][j];36 arr[2][j] =arr2[j];37 }38 }39 for (i = 0; i < 3; i++) {40 for (j = 0; j < 5; j++) {41 cout <
第二题
1.题目描述
2.解决思路首先创建一个二维数组然后利用下标访问特定的元素并进行运算。最后根据题目要求输出运算结果即可
3.代码详情:
1 #include
2 using namespace std;3 intmain() {4 int a2d[3][3];5 cout << "输入一个3*3方阵" <>a2d[i][j];10 }11 }12 int (*p2d)[3] =a2d;13 int a = 0, b = 0, c = 0;14 for (int m = 0; m < 3; m++) {15 for (int k = 0; k <= m; k++) {16 a += *(*(p2d + m) +k);17 }18 }19 cout << "下三角形和为:" << a <= m; k--) {22 b += *(*(p2d + m) +k);23 }24 }25 cout << "上三角形和为:" << b <
31 return 0;32 }
第三题
1.题目详情
2.解决思路这是困扰我最久的一个问题。最开始没看懂题目要求用了char来获取字符。后来才明白要用string。我本来以为只需要进行少量的修改即可。但后来发现和char不同的是:当我令空格(ASCII码为32)为0的时候,它并不会被删除,而用char就不会出现这个问题。我的解决方法是定义一个空的string类型对象carray2。当检测到carray里的某个字符是字母的时候,我就把它变成小写存放到carray2里,非字母字符则不动。这样,carray2里的字符就是删除非字母字符以及把字母全部变成小写之后的句子了。最后用题给思路判断是否为回文。
3.代码详情:
1 #include
2 #include
3 using namespace std;4
5 intmain() {6 string carray, carray2;7 int is_palindrome = 1;8 cout << "Please input a string.." < 64 && carray[i] < 123) {13 if (carray[i] > 64 && carray[i] < 91) { carray[i] += 32; carray2 += carray[i]; continue; }14 if (carray[i] > 96 && carray[i] < 123) { carray2 +=carray[i]; }15 }16 }17 int len2 =carray2.length();18 for (int i = 0; i < len2 / 2; i++){19 if (carray2[i] != carray2[len2 - 1 -i]) {20 is_palindrome = 0;21 break;22 }23 }24 if(is_palindrome)25 cout << "The string is a palindrome." <
27 cout << "The string is not a palindrome." <
29 return 0;30 }
第四题
1.题目详情
2.解决思路这题思路是按照题给提示来的。只不过不同的是,当报数来到最后一个人的时候,我直接让下标变成0来回到第一个人而不是用提示的方法。3.代码详情:
1 #include
2 #include
3 #include
4 using namespace std;5 intmain() {6 int n, k = 0, p = 0;7 cout << "Please put in the number of people " <>n;9 srand(time(0));10 unsigned int m = rand() % 100 + 1;11 cout << "The random is " << m <vi(n,1);13 for (int i = 0;; i++) {//i为下标
14 if (i ==n) {15 i = 0;16 }17 if (vi[i] != 0) {18 k++;//k表示编号为i+1的人所报的数字
19 }20 if (k ==m) {21 vi[i] = 0;22 for (int j = 0; j < n; j++) {23 if (vi[j] == 1) {24 p++;//p用来计vector里面1的个数
25 }26 }27 int l = 0;//l用来判断最后者编号
28 for (; l < n; l++) {29 if (vi[l]) { break; }30 }31 if (p == 1) {32 cout << "The last one's number is " << l + 1 <
41 return 0;42 }
总结
这次遇到了不少困难,求助过同学,上过百度、CSDN……总之想尽各种办法来克服困难。这次我学到了不少额外知识,比如了解了什么是伪随机、补充了许多vector的函数、加强了string类型的用法、了解了string和char的区别。收获颇丰。
第一题
1.题目描述
2.解决思路交换两个数组的数字其实和交换两个对象的数字原理是一样的,都采用了一个中间变量作为桥梁。其余的工作无非是找出最大的那一行。
3.代码详情
#include
using namespace std;
int main() {
int arr[3][5];
int i, j;
cout << "输入一个三行五列的矩阵" << endl;
for (i = 0; i < 3; i++) {
for (j = 0; j < 5; j++) {
cin >> arr[i][j];
}
}//输入一个三行五列的矩阵
int a=0, b=0, c=0;
for (j = 0; j < 5; j++) {
a += arr[0][j];
}
for (j = 0; j < 5; j++) {
b += arr[1][j];
}
for (j = 0; j < 5; j++) {
c += arr[2][j];
}//计算每行元素之和
if (b > a && b > c) {
int arr2[5];//arr2数组用于交换的媒介
for (j = 0; j < 5; j++) {
arr2[j] = arr[0][j];
arr[0][j] = arr[1][j];
arr[1][j] = arr2[j];
}
}
if (c > b && c > a) {
int arr2[5];//arr2数组用于交换的媒介
for (j = 0; j < 5; j++) {
arr2[j] = arr[0][j];
arr[0][j] = arr[2][j];
arr[2][j] = arr2[j];
}
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 5; j++) {
cout << arr[i][j];
}
cout << "\n";
}
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
第二题
1.题目描述
2.解决思路首先创建一个二维数组然后利用下标访问特定的元素并进行运算。最后根据题目要求输出运算结果即可
3.代码详情
#include
using namespace std;
int main() {
int a2d[3][3];
cout << "输入一个3*3方阵" << endl;
int i, j;
for (i = 0; i < 3 ; i++) {
for (j = 0; j < 3; j++) {
cin >> a2d[i][j];
}
}
int (*p2d)[3] = a2d;
int a = 0, b = 0, c = 0;
for (int m = 0; m < 3; m++) {
for (int k = 0; k <= m; k++) {
a += *(*(p2d + m) + k);
}
}
cout << "下三角形和为:" << a << endl;
for (int m = 0; m < 3; m++) {
for (int k = 2; k >= m; k--) {
b += *(*(p2d + m) + k);
}
}
cout << "上三角形和为:" << b << endl;
for (int i = 0; i < 3; i++) {
c += *(*(p2d + i) + i);
}
cout << "主对角线和为:" << c << endl;
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
第三题
1.题目详情
2.解决思路这是困扰我最久的一个问题。最开始没看懂题目要求用了char来获取字符。后来才明白要用string。我本来以为只需要进行少量的修改即可。但后来发现和char不同的是:当我令空格(ASCII码为32)为0的时候,它并不会被删除,而用char就不会出现这个问题。我的解决方法是定义一个空的string类型对象carray2。当检测到carray里的某个字符是字母的时候,我就把它变成小写存放到carray2里,非字母字符则不动。这样,carray2里的字符就是删除非字母字符以及把字母全部变成小写之后的句子了。最后用题给思路判断是否为回文。
3.代码详情
#include
#include
using namespace std;
int main() {
string carray, carray2;
int is_palindrome = 1;
cout << "Please input a string.." << endl;
getline(cin, carray);
int len = carray.length();
for (int i = 0; i < len; i++) {
if (carray[i] > 64 && carray[i] < 123) {
if (carray[i] > 64 && carray[i] < 91) { carray[i] += 32; carray2 += carray[i]; continue; }
if (carray[i] > 96 && carray[i] < 123) { carray2 += carray[i]; }
}
}
int len2 = carray2.length();
for (int i = 0; i < len2 / 2; i++){
if (carray2[i] != carray2[len2 - 1 - i]) {
is_palindrome = 0;
break;
}
}
if (is_palindrome)
cout << "The string is a palindrome." << endl;
else
cout << "The string is not a palindrome." << endl;
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
第四题
1.题目详情
2.解决思路这题思路是按照题给提示来的。只不过不同的是,当报数来到最后一个人的时候,我直接让下标变成0来回到第一个人而不是用提示的方法。3.代码详情
#include
#include
#include
using namespace std;
int main() {
int n, k = 0, p = 0;
cout << "Please put in the number of people " << endl;
cin >> n;
srand(time(0));
unsigned int m = rand() % 100 + 1;
cout << "The random is " << m << endl;
vectorvi(n,1);
for (int i = 0;; i++) {//i为下标
if (i == n) {
i = 0;
}
if (vi[i] != 0) {
k++;//k表示编号为i+1的人所报的数字
}
if (k == m) {
vi[i] = 0;
for (int j = 0; j < n; j++) {
if (vi[j] == 1) {
p++;//p用来计vector里面1的个数
}
}
int l = 0;//l用来判断最后者编号
for (; l < n; l++) {
if (vi[l]) { break; }
}
if (p == 1) {
cout << "The last one's number is " << l + 1 << endl;
break;
}
p = 0;
k = 0;
}
if (p == 1) { break; }
}
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
总结
这次上机遇到了不少困难,求助过同学,上过百度、CSDN……总之想尽各种办法来克服困难。这次作业我学到了不少额外知识,比如了解了什么是伪随机、补充了许多vector的函数、加强了string类型的用法、了解了string和char的区别。收获颇丰。