共计 758 个字符,预计需要花费 2 分钟才能阅读完成。
1、题目
现有如下二维数组,请找出其中最大的数。
要求:
- 1、不在子函数中输入。
- 2、不能批改指定的子函数 int GetMax(int* p, int m, int n)
示例:
输出:1 2 9
4 9 8
输入:max=9
2、残缺代码
2.1 C 语言版本
#include <stdio.h> | |
#define N 10 | |
int GetMax(int* p, int m, int n) { | |
int ret = -0x3f3f3f3f; | |
for (int i = 0; i < m; ++i) | |
for (int j = 0; j < n; ++j) | |
ret = max(ret, (p + i * N)[j]); | |
return ret; | |
} | |
int max(a, b) {if (a >= b) | |
return a; else | |
return b; | |
} | |
int main() {int a[N][N]; | |
for (int i = 0; i < 2; ++i) | |
for (int j = 0; j < 3; ++j) | |
scanf("%d", &a[i][j]); | |
printf("max=%d", GetMax(a[0], 2, 3)); | |
return 0; | |
} |
2.2 C++ 版本
#include <iostream> | |
#define N 10 | |
using namespace std; | |
int GetMax(int *p, int m, int n) | |
{ | |
int ret = -0x3f3f3f3f; | |
for(int i = 0; i < m; ++i) | |
for(int j = 0; j < n; ++j) | |
ret = max(ret, (p + i * N)[j]); | |
return ret; | |
} | |
int max(a, b) {if (a >= b) | |
return a; else | |
return b; | |
} | |
int main() | |
{int a[N][N]; | |
for(int i = 0; i < 2; ++i) | |
for(int j = 0; j < 3; ++j) | |
cin >> a[i][j]; | |
cout << GetMax(a[0], 2, 3) << endl; | |
return 0; | |
} |
3、截图
正文完