`
gogoyoyo
  • 浏览: 36773 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

判断一个二维数组有无鞍点

    博客分类:
  • Java
J# 
阅读更多
public class PutMatrix {
	int index;
	
	boolean hasSaddlepoint(int a[][]){
		boolean hasSaddlepoint = false;
		boolean flag = true;
		for (int i = 0; i < a.length; i++) {
			flag = true;
			index = rowmax(a[i]);
			for (int j = 0; j < a[0].length; j++) {
				if(i==j) 	continue;
				else if(a[i][index]>a[j][index])
					{
					flag = false;
					break;
					}
			}
			
			if(flag == true) {
				hasSaddlepoint = flag;
				System.out.println("Find a saddle point, and the point is ("+i+","+index +"),"+"the value is "+a[i][index]);}
			else System.out.println("This row has no saddle point!");
			
			
		}
		return hasSaddlepoint;
	}
	
	int rowmax(int b[]){
		int index=0;
		int max = b[0];
		for (int j = 1; j < b.length; j++) {
			if(b[j]>max)
				{
				max = b[j];
				index = j;
				}
		}
		System.out.println(max+"   "+index);
		return index;
	}
	public static void main(String[] args) {
		int a[][] = {{1,2,6,7},{3,5,8,13},{4,9,12,14},{10,11,15,16}};
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				System.out.printf("%12d",a[i][j]);
			}
			System.out.println("\n");
		}
		
		PutMatrix pm = new PutMatrix();
		System.out.println(pm.hasSaddlepoint(a));
         
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics