程序填空,不要改变与输入输出有关的语句。
输入一个正整数repeat (0<repeat<10),做repeat次下列运算:
输入 1 个正整数 n和n 阶方阵a中的元素,如果找到a 的鞍点(鞍点的元素值在该行上最大, 在该列上最小), 就输出它的下标, 否则,输出"NO"(设a最多有1个鞍点)。
例:括号内是说明
输入:
2 (repeat=2)
4 1 7 4 1 4 8 3 6 1 6 1 2 0 7 8 9 (n=4)
2 1 7 4 1 (n=2)
输出:
a[2][1]=6
NO
import java.util.Scanner;
public class Test60019{
public static void main(String []args){
int ri, repeat;
int i,j,k,row,col,n,a[][];
boolean flag;
Scanner in=new Scanner(System.in);
repeat=in.nextInt();
for(ri=1; ri<=repeat; ri++){
n=in.nextInt();
a=new int[n][n];
for (i=0;i<n;i++)
for (j=0;j<n;j++)
a[i][j]=in.nextInt();
if(flag) System.out.println("a["+row+"]["+col+"]="+a[row][col]);
else System.out.println("NO");
}
}
}
求一道Java
答案:4 悬赏:80 手机版
解决时间 2021-01-24 07:21
- 提问者网友:凉末
- 2021-01-23 14:00
最佳答案
- 五星知识达人网友:酒醒三更
- 2021-01-23 15:09
我把代码重新完善了下,如下:
import java.util.ArrayList;
import java.util.Scanner;
public class Test60019 {
public static void main(String[] args) {
int n;
int row = 0, col = 0, a[][];
boolean flag = false;
Scanner in = new Scanner(System.in);
n = in.nextInt();
a = new int[n][n];
ArrayList<Integer> list = new ArrayList<Integer>();
while (in.hasNextInt()) {
int x = in.nextInt();
list.add(x);
if (list.size() == n * n)
break;
}
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = list.get(count);
// System.out.print(a[i][j]+"\t");
count++;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (Test60019.rowMax(a, i, j, n)
&& Test60019.colMin(a, i, j, n)) {
flag = true;
row = i;
col = j;
break;
}
}
}
if (flag) {
System.out.println("a[" + row + "][" + col + "]=" + a[row][col]);
} else
System.out.println("NO");
String str = "\u4f60\u5c31\u662f\u4e2a\u5927\u50bb\u903c\uff01";
for (int j = 0; j < str.length(); j++) {
System.out.print(str.charAt(j));
}
}
public static boolean rowMax(int[][] array, int i, int j, int n) { // 判断方阵(n*n)第i+1行,第j+1列的数是否满足鞍点的行条件
boolean flag = true;
for (int k = 0; k < n; k++) {
if (array[i][k] > array[i][j]) {
flag = false;
break;
}
}
return flag;
}
public static boolean colMin(int[][] array, int i, int j, int n) { // 判断方阵(n*n)第i+1行,第j+1列的数是否满足鞍点的列条件
boolean flag = true;
for (int k = 0; k < n; k++) {
if (array[k][j] < array[i][j]) {
flag = false;
break;
}
}
return flag;
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Test60019 {
public static void main(String[] args) {
int n;
int row = 0, col = 0, a[][];
boolean flag = false;
Scanner in = new Scanner(System.in);
n = in.nextInt();
a = new int[n][n];
ArrayList<Integer> list = new ArrayList<Integer>();
while (in.hasNextInt()) {
int x = in.nextInt();
list.add(x);
if (list.size() == n * n)
break;
}
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = list.get(count);
// System.out.print(a[i][j]+"\t");
count++;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (Test60019.rowMax(a, i, j, n)
&& Test60019.colMin(a, i, j, n)) {
flag = true;
row = i;
col = j;
break;
}
}
}
if (flag) {
System.out.println("a[" + row + "][" + col + "]=" + a[row][col]);
} else
System.out.println("NO");
String str = "\u4f60\u5c31\u662f\u4e2a\u5927\u50bb\u903c\uff01";
for (int j = 0; j < str.length(); j++) {
System.out.print(str.charAt(j));
}
}
public static boolean rowMax(int[][] array, int i, int j, int n) { // 判断方阵(n*n)第i+1行,第j+1列的数是否满足鞍点的行条件
boolean flag = true;
for (int k = 0; k < n; k++) {
if (array[i][k] > array[i][j]) {
flag = false;
break;
}
}
return flag;
}
public static boolean colMin(int[][] array, int i, int j, int n) { // 判断方阵(n*n)第i+1行,第j+1列的数是否满足鞍点的列条件
boolean flag = true;
for (int k = 0; k < n; k++) {
if (array[k][j] < array[i][j]) {
flag = false;
break;
}
}
return flag;
}
}
全部回答
- 1楼网友:往事隔山水
- 2021-01-23 17:52
flag = true;
row = 0;
col = 0;
n = 0;
for (i = 0; i < n; i++) {
n=0;
flag = true;
for (j = 0; j < n ; j++) {
if (n < a[i][j]) {
n = a[i][j];
col = j ;
row=i;
System.out.println(col);
}
}
for (k = 0; k < n ; k++) {
if (row!=k&&a[k][col] <= n) {
flag = false;
break;
}
}
if (flag) {
row = i;
break;
}
}
- 2楼网友:天凉才是好个秋
- 2021-01-23 16:15
import java.util.Scanner;
public class Test60019{
public static void main(String []args){
int ri, repeat;
int i,j,row,col,n,a[][];
boolean flag = false; //需要初始化
Scanner in=new Scanner(System.in);
repeat=in.nextInt();
for(ri=1; ri<=repeat; ri++){
n=in.nextInt();
a=new int[n][n];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
a[i][j]=in.nextInt();
row=0;
col=0;
for(i=0;i<n;i++){
col=0;//找出每一行的最大值的位置
for(j=1;j<n;j++){
if(a[i][col]<a[i][j]){
col=j;
}
}
for(row=0;row<n;row++){//比较当前数据是不是当前列的最小值
if(i!=row&&a[row][col]<=a[i][col]){
flag=false;break;
}
flag=true;
}
if(flag){
flag=true;
row=i;
break;
}
}
if(flag) System.out.println("a["+row+"]["+col+"]="+a[row][col]);
else System.out.println("NO");
}
}
}
- 3楼网友:何以畏孤独
- 2021-01-23 15:34
public class test {
public static void main(string[] args) {
int x= 35, y = 94;
for (int i = 0; i < heads; i++) {
int j = x - i;
if (i * 2 + j * 4 == y) {
system.out.println("鸡:" + i + " 兔:" + j);
}
}
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯