题目描述
给定一个 n 行 m 列的二维矩阵和一个目标数 t,二维矩阵中对于每一行从左到右不下降(右边的数大于等于左边的数),对于每一列从上到下不下降(下边的数大于等于上边的数)。现要在数组中找到目标数 t,并输出其所在位置的行数和列数。
注意:本题数据已更新,t 已改为在最后输入,所有不符合要求的提交记录均已被删除。
输入
第一行两个整数 n,m。(1≤n,m≤3000)
接下来一个二维矩阵,矩阵内所有数均小于 200000。
最后一行一个整数 t。(1≤t≤200000)
输出
输出两个用空格隔开的数表示位置(从一开始计数),答案有唯一解。
样例输入
3 4
1 2 3 4
5 6 15 20
7 10 20 25
15
样例输出
2 3
右上角
#include<iostream>
#include <cstdio>
using namespace std;
int n, m, t, num[3005][3005];
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d", &num[i][j]);
}
}
scanf("%d", &t);
int x = 1, y = m;
while (x <= n && y > 0) {
int t1 = num[x][y];
if (t1 == t) {
printf("%d %d\n", x, y);
return 0;
}
if (t1 > t) {
y--;
}
else {
x++;
}
}
printf("not found\n");
return 0;
}
左下角
#include<iostream>
#include <cstdio>
using namespace std;
int n, m, t, num[3005][3005];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d", &num[i][j]);
}
}
scanf("%d", &t);
int x = n, y = 1;
while (y <= m && x > 0) {
int t1 = num[x][y];
if (t1 == t) {
printf("%d% d\n", x, y);
return 0;
}
if (t1 > t) {
x--;
}
else {
y++;
}
}
printf("not found\n");
return 0;
}