Chinaunix首页 | 论坛 | 博客
  • 博客访问: 616125
  • 博文数量: 87
  • 博客积分: 3399
  • 博客等级: 中校
  • 技术积分: 1422
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-17 21:20
文章分类

全部博文(87)

文章存档

2013年(1)

2012年(51)

2011年(33)

2010年(2)

分类: Java

2012-05-04 16:25:00


点击(此处)折叠或打开

  1. public class Test{
  2.     public static boolean isOrdered(Comparable a[]){
  3.         int j=0, n = a.length;
  4.         while(j<n-1 && a[j].compareTo(a[j+1])==0) j++;
  5.         if(n<=1 || j==n-1)
  6.             return true;
  7.         else
  8.             j = a[j].compareTo(a[j+1]);
  9.         for(int i=0; i<n-1; i++)
  10.             if(a[i].compareTo(a[i+1])*j < 0)
  11.                 return false;
  12.         return true;
  13.     }    
  14.     public static void main(String[] args){
  15.         Integer arr[] = {1,2,3,4,5};
  16.         Integer arr1[] = {5,4,3,2,1};
  17.         Integer arr2[] = {1,2,5,4,3};
  18.         
  19.         System.out.println(isOrdered(arr)); //true

  20.         System.out.println(isOrdered(arr1));//true

  21.         System.out.println(isOrdered(arr2));//false

  22.         
  23.     }
  24. }

阅读(970) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~