標籤:java
/**
* 6-1
* 編寫程式找出一個二維數組的鞍點
*/
import java.util.Scanner;public class Test{public static void main(String[] args){/*建立二維數組*/int i, j, k, l, m = 0, max, min, line=0, list=0;Scanner read = new Scanner(System.in);System.out.println("請輸入二維數組的行、列");line = read.nextInt();list = read.nextInt();int array[][] = new int[line][list];System.out.println("請輸入二維數組的各個元素值");for (i = 0; i < line; i++)for (j = 0; j < list; j++)array[i][j] = read.nextInt();//int array[][]= new int[][]{{2,3,4,5,6},// {3,4,5,6,8},// {3,1,2,5,9},// {3,4,1,8,7}};//line=4;//list=5;/*尋找鞍點*/System.out.println("開始尋找鞍點^_^");for (i = 0; i < line; i++){max = array[i][0];k = 0;for (j = 1; j < list; j++)if (array[i][j] > max){max = array[i][j];k = j;}min = array[0][k];for (l = 1; l < line; l++)if (array[l][k] < min)min = array[l][k];if (max == min){System.out.println("存在鞍點,在第 " + i + "行,第 " + (j-1) + "列,值為:"+array[i][j-1]);m++;}}if (m == 0)System.out.println("不存在鞍點!");}}
那些年,一起學的Java 6-1