引言
针对初学者,逐日训练几个题,快速上手C语言。第十天。(连续更新中)
接纳在线OJ的形式
什么是在线OJ?
在线判题系统(英语:Online Judge,缩写OJ)是一种在编程竞赛中用来测试参赛程序的在线系统,也可以用于平时的训练。
具体内容可以看一下这篇博客:关于C/C++语言的初学者在哪刷题,怎么刷题-CSDN博客https://blog.csdn.net/2401_88433210/article/details/146056171?spm=1011.2415.3001.10575&sharefrom=mp_manage_link
建议从第一天开始做,前面讲了的知识点后面只是会提一下。有不懂的写再评论区里
今天的题全是一些二维数组的应用
1.班级成绩输入输出
点进去直接做题:班级成绩输入输出_牛客题霸_牛客网 (nowcoder.com)https://www.nowcoder.com/practice/60d96b08e1cb42e38629d54e37eac008?tpId=107&&tqId=33382&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking参考代码:
- #include<stdio.h>
- int main()
- {
- double arr[5][5];
- for(int i=0;i<5;++i)
- {
- scanf("%lf %lf %lf %lf %lf",&arr[i][0],&arr[i][1],&arr[i][2],&arr[i][3],&arr[i][4]);
- }
- for(int i=0;i<5;++i)
- {
- double sum=0;
- for(int j=0;j<5;++j)
- {
- printf("%.1f ",arr[i][j]);
- sum+=arr[i][j];
- }
- printf("%.1f\n",sum);
- }
- return 0;
- }
复制代码 解析:按照题目要求做就行了
2.矩阵元素定位
点进去直接多题:矩阵元素定位_牛客题霸_牛客网 (nowcoder.com)https://www.nowcoder.com/practice/b8e6a46992fe4e11b2822b20561b6d94?tpId=107&&tqId=33383&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking参考代码:
- #include <stdio.h>
- int main() {
- int n, m;
- scanf("%d %d", &n, &m);
- int val[n + 1][m + 1];
- for (int i = 1; i <= n; i++) {
- for (int j = 1; j <= m; j++) {
- scanf("%d ", &val[i][j]);
- }
- }
- int x, y;
- scanf("%d %d", &x, &y);
- printf("%d", val[x][y]);
- return 0;
- }
复制代码 解析:按照题目要求做就行了,简朴题
3.序列重组矩阵
点进去直接做题:序列重组矩阵_牛客题霸_牛客网 (nowcoder.com)https://www.nowcoder.com/practice/7548f8f5e44c42fa84fb2323d899a966?tpId=107&&tqId=33384&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking
参考代码:
- #include <stdio.h>
-
- int main()
- {
- int n = 0;
- int m = 0;
- int arr[100] = { 0 };
- scanf("%d %d", &n, &m);
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- scanf("%d", &arr[i]);
- printf("%d ", arr[i]);
- }
- printf("\n");
- }
- return 0;
- }
复制代码 解析:按照题目意思就行,简朴的
4.最高身高
点进去直接做题:最高身高_牛客题霸_牛客网 (nowcoder.com)https://www.nowcoder.com/practice/258fe0c567ac493f9b7bc9d3669d158d?tpId=107&&tqId=33385&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking参考代码:
- #include <stdio.h>
-
- int main()
- {
- int n = 0;
- int m = 0;
- int arr[10][10] = { 0 };
- scanf("%d %d", &n, &m);
- int max = 0;
- int x = 0;
- int y = 0;
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- scanf("%d", &arr[i][j]);
- if (arr[i][j] > max)
- {
- x = i + 1;
- y = j + 1;
- max = arr[i][j];
- }
- }
- }
- printf("%d %d\n", x, y);
-
- return 0;
- }
复制代码 解析:
核心代码就if语句里面的东西,很简朴。
5.矩阵相等判断
点进去直接做题:矩阵相等判断_牛客题霸_牛客网 (nowcoder.com)https://www.nowcoder.com/practice/2f058b1e89ae43f7ab650c0fd0b71fa8?tpId=107&&tqId=33386&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking参考代码:
- #include <stdio.h>
-
- int main()
- {
- int n = 0;
- int m = 0;
- int arr1[10][10] = { 0 };
- int arr2[10][10] = { 0 };
- //输入
- scanf("%d %d", &n, &m);
- int i = 0;
- for (i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- scanf("%d", &arr1[i][j]);
- }
- }
- for (i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- scanf("%d", &arr2[i][j]);
- //判断
- if (arr1[i][j] != arr2[i][j])
- {
- printf("No\n");
- return 0;
- }
- }
- }
- printf("Yes\n");
- return 0;
- }
复制代码 解析:题目让你干啥,你就干啥。
6.上三角矩阵判断
点进去直接做题:
上三角矩阵判断_牛客题霸_牛客网 (nowcoder.com)https://www.nowcoder.com/practice/f5a29bacfc514e5a935723857e1245e4?tpId=107&&tqId=33387&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking参考代码:
- //方法一:先将元素进行录入,后进行判别下三角的情况
- int main()
- {
- int n = 0;
- scanf("%d",&n);
- int i,j;
- int arr[10][10] = {0};
- for(i=0; i<n;i++)
- {
- for(j=0;j<n;j++)
- {
- scanf("%d",&arr[i][j]);
- }
- }
- for(i=0; i<n;i++)
- {
- for(j=0;j<i;j++)
- {
- if(arr[i][j] != 0)
- {
- printf("NO\n");
- return 0;
- }
- }
- }
- printf("YES\n");
- return 0;
- }
- */
-
- //方法二:一边录入元素,一边进行判断
- int main()
- {
- int n = 0;
- scanf("%d",&n);
- int i,j;
- int arr[10][10] = {0};
- for(i=0; i<n;i++)
- {
- for(j=0;j<n;j++)
- {
- scanf("%d",&arr[i][j]);
- if(i>j)
- {
- if(arr[i][j] != 0)
- {
- //下三角有不是0的就不满足
- printf("NO");
- return 0;
- }
- }
- }
- }
- printf("YES\n");
- return 0;
- }
复制代码 解析:找规律,按照题意来做就行。
7.矩阵互换
点进去直接做题:矩阵互换_牛客题霸_牛客网 (nowcoder.com)https://www.nowcoder.com/practice/ec44d4ff8c794b2f9205bdddbde96817?tpId=107&&tqId=33389&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking参考代码:
- #include <stdio.h>
-
- int main()
- {
- int n = 0;
- int m = 0;
- int arr[10][10] = { 0 };
- scanf("%d %d", &n, &m);
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- scanf("%d", &arr[i][j]);
- }
- }
- int k = 0;
- scanf("%d", &k);
- char t = 0;
- int a = 0;
- int b = 0;
-
- //进行k次变化
- for (int i = 0; i < k; i++)
- {
- scanf(" %c %d %d", &t, &a, &b);
- if (t == 'r')
- {
- //交换a和b行
- for (int j = 0; j < m; j++)
- {
- int tmp = arr[a - 1][j];
- arr[a - 1][j] = arr[b - 1][j];
- arr[b - 1][j] = tmp;
- }
- }
- else if (t == 'c')
- {
- //交换a和b列
- for (int j = 0; j < n; j++)
- {
- int tmp = arr[j][a - 1];
- arr[j][a - 1] = arr[j][b - 1];
- arr[j][b - 1] = tmp;
- }
- }
- }
-
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- printf("%d ", arr[i][j]);
- }
- printf("\n");
- }
-
- return 0;
- }
复制代码 解析:
1. 假如是%c读取字符,肯定要清理前面输入时留下的空缺字符
2. 注意二维数组的下标
3.按照题意模拟就行。
8.矩阵转置
点进去直接做题:矩阵转置_牛客题霸_牛客网 (nowcoder.com)https://www.nowcoder.com/practice/351b3d03e410496ab5a407b7ca3fd841?tpId=107&&tqId=33388&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking参考代码:
- #include <stdio.h>
- int main() {
- int m, n, b[20][20];
- scanf("%d %d", &n, &m);
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- scanf("%d", &b[i][j]);
- }
- }
- for (int i = 0; i < m; i++)
- {
- for (int j = 0; j < n; j++) {
- printf("%d ", b[j][i]);
- }
- printf("\n");
- }
- return 0;
- }
复制代码 解析:按照题目标意思做就行。
9.杨辉三角
点进去直接做题:杨辉三角_牛客题霸_牛客网 (nowcoder.com)https://www.nowcoder.com/practice/e671c6a913d448318a49be87850adbcc?tpId=107&&tqId=33390&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking参考代码:
- #include <stdio.h>
-
- int main()
- {
- int n = 0;
- int arr[30][30] = { 0 };
- scanf("%d", &n);
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j <= i; j++)
- {
- if (0 == j || i == j)
- {
- arr[i][j] = 1;
- }
- else
- {
- arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
- }
- printf("%5d", arr[i][j]);
- }
- printf("\n");
- }
-
- return 0;
- }
复制代码 解析:找规律题,模拟就行,注意题目要求。
10.井字棋
点进去直接做题:井字棋_牛客题霸_牛客网 (nowcoder.com)https://www.nowcoder.com/practice/0375c1d206ef48a3894f72aa07b2fdde?tpId=107&&tqId=33391&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking参考代码:
- #include <stdio.h>
-
- int main()
- {
- char board[3][3] = { 0 };
- char flag = 0;
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- scanf(" %c", &board[i][j]);
- }
- }
- //判断输赢
- for (int i = 0; i < 3; i++)
- {
- //行
- if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][2] != 'O')
- {
- flag = board[i][2];
- break;
- }
- //列
- if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[2][i] != 'O')
- {
- flag = board[2][i];
- break;
- }
- }
- //对角线
- if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] != 'O')
- {
- flag = board[2][2];
- }
- if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] != 'O')
- {
- flag = board[2][0];
- }
-
- if ('K' == flag)
- {
- printf("KiKi wins!\n");
- }
- else if ('B' == flag)
- {
- printf("BoBo wins!\n");
- }
- else
- {
- printf("No winner!\n");
- }
-
- return 0;
- }
复制代码 解析:
把所有赢的环境罗列出来然后排除就行。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |