java题1_Java题目1

本文介绍了使用C++进行数组操作和矩阵计算的实现方法,包括交换矩阵行、计算矩阵下三角形和上三角形的和以及判断字符串是否为回文。通过示例代码展示了如何处理二维数组、利用指针访问元素、字符串处理和条件判断等编程技巧。此外,还探讨了在处理字符串时string与char类型的差异,并在最后的报数游戏中应用了随机数和循环控制。
摘要由CSDN通过智能技术生成

第一题

1.题目描述

8b4740044aaf4463ac6f139649791603.png2.解决思路交换两个数组的数字其实和交换两个对象的数字原理是一样的,都采用了一个中间变量作为桥梁。其余的工作无非是找出最大的那一行。

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.题目描述

914b0e8937e2e98811c0f644dbc0191b.png2.解决思路首先创建一个二维数组然后利用下标访问特定的元素并进行运算。最后根据题目要求输出运算结果即可

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.题目详情

a26f5112433e920b263b7449e7f7e167.png

f12bbb11e903aa674ce4eba5e14ad264.png2.解决思路这是困扰我最久的一个问题。最开始没看懂题目要求用了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.题目详情

a65f9c146501d711703291b2249c5dd2.png2.解决思路这题思路是按照题给提示来的。只不过不同的是,当报数来到最后一个人的时候,我直接让下标变成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.题目描述

8b4740044aaf4463ac6f139649791603.png2.解决思路交换两个数组的数字其实和交换两个对象的数字原理是一样的,都采用了一个中间变量作为桥梁。其余的工作无非是找出最大的那一行。

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.题目描述

914b0e8937e2e98811c0f644dbc0191b.png2.解决思路首先创建一个二维数组然后利用下标访问特定的元素并进行运算。最后根据题目要求输出运算结果即可

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.题目详情

a26f5112433e920b263b7449e7f7e167.png

f12bbb11e903aa674ce4eba5e14ad264.png2.解决思路这是困扰我最久的一个问题。最开始没看懂题目要求用了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.题目详情

a65f9c146501d711703291b2249c5dd2.png2.解决思路这题思路是按照题给提示来的。只不过不同的是,当报数来到最后一个人的时候,我直接让下标变成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的区别。收获颇丰。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值