写一个函数传入一个任意整形数组 判断数组元素是否为连续数字如{1,2,3,4}或{2,1,4,3},而{1,2,4,3}则不是,是返回TRUE,不是返回FALSE

解决方案 »

  1.   

    import java.util.*;
    public class a{
    public static void main(String... args)
    {
    int[] s={1,3,0,4};
    System.out.println(lx(s));

    }
    public static boolean lx(int[] arr)
    {
    boolean f=false;
    Arrays.sort(arr);
    int m=0;
    for(int i=arr[0];i<=arr.length;i++)
    {
    if(i!=arr[m++])
    {
    f=false;
    return false;
    }
    }
    return true;
    }
    }这个第一次可以不比较...无所谓...功能实现就行了...
      

  2.   

    public static boolean isLianxu(int [] sorce){
          for(int i=0;i<sorce.length()-1;i++){
               if(sorce[i+1]!=(sorce[i]+1)
                   return false;
          }
          return true;
    }
    思路:只要第i+1个数字不是第i歌数字加1,则返回false,循环结束如果都是,那么就返回true
      

  3.   

    思路确实很多...取出最小的,然后加1,判断,不等则FALSE,否则TRUE
      

  4.   

    public class Test {
    public static boolean function(int[] a){
    boolean b=true;
    int i=0;
    while(b){
    if(i+1<a.length){
    if(a[i]==a[i+1]+1){
    b=true;
    }
    else {
    b= false;
    }
    i++;
    }
    return true;

    }return false;
    }
    public static boolean function1(int []a){
    for(int i=0;i<a.length-1;i++){
    int num=0;
    if(a[i]>a[i+1]){
    num=a[i];
    a[i]=a[i+1];
    a[i=1]=num;
    }
    }for(int i=0;i<a.length;i++){
    System.out.println(a[i]);}
    if(function(a)){
    return true;
    }else{
    return false;
    }
    }
    }
    新手多指教:
    function 是用来判断给的数组是否是连续的
    function1 是用来判断给的数组能否排列成连续的
      

  5.   

    题目的意思可能是要判断前一个数字和后一个数字是否为连续的数组,如果是这样
    public static boolean isLianxu(int [] sorce){
    for(int i=0;i<sorce.length()-1;i++){
    if((sorce[i+1]+1=(sorce[i]+1))||(sorce[i+1]-1=(sorce[i]+1))
    return true;
    }
    return false;
    }
      

  6.   

    {2,1,4,3}这哪里连续了
    那就是一个for循环
    for(int i=0;i<a.length;i++)
    {
    if((a[i]-a[i+1]=1)||(a[i+1]-a[i]=1)||(a[2i+1]-a[2i]=1)||(a[2i+1]-a[2i]=-1))
    return true;
    }
    return false;
      

  7.   

    思路:题目false的条件是后一个数不是前一个数加1或减1(arg[i+1]+1 || arg[i+1]-1 <> arg[i])判断arg[i+1]+1或者arg[i+1]-1是否等于arg[i],是则记录是+还是-,然后继续判断下一个是否连续。否则跳过,用上面的方法判断arg[i+2]。直至结束。
    好学生,只解题,无代码。
      

  8.   

    我觉得应该是“经旋转后是否连续了”,姑且看做循环队列吧
    如题目:
    {1,2,3,4}原本就有序 略
    {2,1,4,3}表面看无序的,事实上经过一定程度的循环后一定是连续的,如左循环两位{4,3,2,1}那么什么情况才能旋转事实上此处有一个趋势。
    由于一定严格要求“连续”,则刚开始的数组的“趋势”和以后任意相邻两数字的“趋势”是一样的(要么++,要么--),一种情况除外就是 ,已经--到本数组最小,或者已经++到本数组
    最大了,就要从相反的方向重新以相反的趋势发展。
    基于以上思路,为了逻辑清晰,我们可以先遍历一遍 求出最大,最小值(或下标),然后在遍历记下数组发展“趋势”,,一旦不一样返回false,但是遍历到最小(最大)处要除外的。
    这里还有一点就是,既然是循环连续的那么max和min的值只有两种可能:差1;差length-1代
    码如下public static boolean isL(int[] a)
    {
    int i;
    int max=0,min=0;
    for(i=1;i<a.length;++i)
    {
    if(a[i]>a[max]) max=i;
    if(a[i]<a[min]) min=i;
    }
    if((Math.abs(max-min)!=1)&&(Math.abs(max-min)!=a.length-1))return false;
    int status=a[0]-a[1];
    for(i=0;i<a.length-1;++i)
    {

    if(Math.abs(status)!=1)return false;
    if((a[i]-a[i+1]!=status))   //不是依次递增或递减时
    {

    if(((i+1)!=min)&&((i+1)!=max))return false;

        }

    }
    return true;
    只测试了几个用例  还望指点
      

  9.   


    package doudou;public class Test {
    public static boolean checkDown(int[] num, int start, int end) {
    for (int i = start; i < end - 1; i++) {
    if (num[i] > num[i + 1]) {
    continue;
    } else {
    return false;
    }
    }
    return true;
    } public static boolean checkUp(int[] num, int start, int end) {
    for (int i = start; i < end - 1; i++) {
    if (num[i] < num[i + 1]) {
    continue;
    } else {
    return false;
    }
    }
    return true;
    } public static boolean loopDown(int[] num, int len) {
    int times = num.length / len;
    int mod = num.length % len;
    boolean flag = false;
    int i = 0;
    if (times == 1) {
    return checkDown(num, 0, len);
    }
    if (mod != 0
    && checkDown(num, num.length - 1 - mod, num.length - 1) == false) {
    flag = loopDown(num, len + 1);
    } else {
    for (i = 0; i < times; i++) {
    if (checkDown(num, i * len, (i + 1) * len) == false) {
    break;
    }
    }
    if (i == times) {
    flag = true;
    } else {
    flag = loopDown(num, len + 1);
    }
    }
    return flag;
    } public static boolean loopUp(int[] num, int len) {
    int times = num.length / len;
    int mod = num.length % len;
    boolean flag = false;
    int i = 0;
    if (times == 1) {
    return checkUp(num, 0, len);
    }
    if (mod != 0
    && checkUp(num, num.length - 1 - mod, num.length - 1) == false) {
    flag = loopUp(num, len + 1);
    } else {
    for (i = 0; i < times; i++) {
    if (checkUp(num, i * len, (i + 1) * len) == false) {
    break;
    }
    }
    if (i == times) {
    flag = true;
    } else {
    flag = loopUp(num, len + 1);
    }
    }
    return flag;
    } public static boolean loop(int[] num, int len) {
    if (loopDown(num, 2) || loopUp(num, 2)) {
    return true;
    } else {
    return false;
    }
    } public static void printArr(int[] num) {
    System.out.print("{");
    for (int i = 0; i < num.length - 1; i++) {
    System.out.print(num[i] + ",");
    }
    System.out.print(num[num.length - 1] + "}");
    } public static void main(String[] args) {
    int[] a = { 4, 3, 2, 6, 5, 3, 2, 1 };
    int[] b = { 1, 4, 7, 2, 3, 5, 9 };
    if (loop(a, 2)) {
    printArr(a);
    System.out.println("连续");
    } else {
    printArr(a);
    System.out.println("不连续");
    }
    if (loop(b, 2)) {
    printArr(b);
    System.out.println("连续");
    } else {
    printArr(b);
    System.out.println("不连续");
    }
    }
    }不知道是不是楼主想要的效果,楼主可以找几个用例测试一下。
      

  10.   

    贴下我的代码。。跟19楼的想法有点像。。考虑了下旋转。。
    class LianXu
    {
    static int[] sz = new int[]{1,2,4,3}; public static void main(String[] args){
    System.out.println(isLianxu());
    } public static boolean isLianxu(){
    int index = 0;
    for (int i = 1;i<sz.length ;i++ )
    if (sz[index]>sz[i])
    index = i;
    int direction = 0;
    if (index==0){
    if (sz[index+1]==sz[index]+1){
    direction = 1; index++;
    }
    else if (sz[sz.length-1]==sz[index]+1){
    direction = -1; index = sz.length-1;
    }
    else{
    return false;
    }
    }
    if (index==sz.length-1){
    if (sz[index-1]==sz[index]+1){
    direction = -1; index--;
    }
    else if (sz[0]==sz[index]+1){
    direction = 1; index = 0;
    }
    else{
    return false;
    }
    }
    int count = sz.length-2;
    int next = index; while(count>0){
    next += direction;
    if (next < 0) {next = sz.length-1;}
    else if (next>sz.length-1){ next = 0;}

    if (sz[index] == sz[next]){
    index = next;
    }
    else{
    return false;
    }
    count--;
    }
    return true;
    }
    }
      

  11.   

    囧。。才发现是贴了刚才没做好的。。这次是做好的了。。求验证
    class LianXu
    {
    static int[] sz = new int[]{1,2,3,4}; public static void main(String[] args){
    System.out.println(isLianxu());
    } public static boolean isLianxu(){
    int max = 0,min = 0,direction = 0,index = 0;
    for (int i = 1;i<sz.length ;i++ ){
    if (sz[min]>sz[i]) min = i;
    if (sz[max]<sz[i])  max = i;
    }
    index = min;
    if (max-min==1 || max-min==-(sz.length-1)){
    direction = -1; 
    }
    else if (max-min==-1 || max-min==sz.length-1){
    direction = 1;
    }
    else {return false;}

    int count = sz.length-1;
    int next = index; while(count>0){
    next += direction;
    if (next < 0) {next = sz.length-1;}
    else if (next>sz.length-1){ next = 0;}

    if (sz[index]+1 == sz[next]){
    index = next;
    }
    else{
    return false;
    }
    count--;
    }
    return true;
    }
    }
      

  12.   


    public class IsSequential {
    public static void main(String[] args) {
    int[] a={1,3,2,3};
    check(a);
    }


    public static void check(int[] numbers)
    {
    int numCount;
    numCount = numbers.length;
    System.out.println(numCount);
    if (numCount < 2)
    {
    System.out.println("此数组是连续的");
    }
    else if (numCount == 2)
    {
    if (Math.abs(numbers[0]-numbers[1]) == 1)
    System.out.println("此数组是连续的");
    else
    System.out.println("此数组是不连续的");
    }
    else
    {
    for (int i=1;i<numCount-1;i++)
    {
    if (Math.abs(numbers[i]-numbers[i-1])!=1 && Math.abs(numbers[i]-numbers[i+1])!=1 )
    {
    System.out.println("此数组是不连续的");
    break;
    }
    else
    if (i == numCount-2 ) System.out.println("此数组是连续的");
    }

    }
    }
    }难不成是这样的?
      

  13.   


    public class Test {
    public static boolean checkDown(int[] num, int start, int end) {
    for (int i = start; i < end - 1; i++) {
    if (num[i] > num[i + 1]) {
    continue;
    } else {
    return false;
    }
    }
    return true;
    } public static boolean checkUp(int[] num, int start, int end) {
    for (int i = start; i < end - 1; i++) {
    if (num[i] < num[i + 1]) {
    continue;
    } else {
    return false;
    }
    }
    return true;
    } public static boolean loopDown(int[] num, int len) {
    int times = num.length / len;
    int mod = num.length % len;
    boolean flag = false;
    int i = 0;
    if (times == 1) {
    return checkDown(num, 0, len);
    }
    if (mod != 0
    && checkDown(num, num.length - 1 - mod, num.length) == false) {//刚刚的写错了,应该是checkDown(num, num.length - 1 - mod, num.length)
    flag = loopDown(num, len + 1);
    } else {
    for (i = 0; i < times; i++) {
    if (checkDown(num, i * len, (i + 1) * len) == false) {
    break;
    }
    }
    if (i == times) {
    flag = true;
    } else {
    flag = loopDown(num, len + 1);
    }
    }
    return flag;
    } public static boolean loopUp(int[] num, int len) {
    int times = num.length / len;
    int mod = num.length % len;
    boolean flag = false;
    int i = 0;
    if (times == 1) {
    return checkUp(num, 0, len);
    }
    if (mod != 0 && checkUp(num, num.length - 1 - mod, num.length) == false) {//刚刚的写错了,应该是checkUp(num, num.length - 1 - mod, num.length)
    flag = loopUp(num, len + 1);
    } else {
    for (i = 0; i < times; i++) {
    if (checkUp(num, i * len, (i + 1) * len) == false) {
    break;
    }
    }
    if (i == times) {
    flag = true;
    } else {
    flag = loopUp(num, len + 1);
    }
    }
    return flag;
    } public static boolean loop(int[] num, int len) {
    if (loopDown(num, 2) || loopUp(num, 2)) {
    return true;
    } else {
    return false;
    }
    } public static void printArr(int[] num) {
    System.out.print("{");
    for (int i = 0; i < num.length - 1; i++) {
    System.out.print(num[i] + ",");
    }
    System.out.print(num[num.length - 1] + "}");
    } public static void main(String[] args) {
    int[] a = { 4, 3, 6, 5, 3, 2, 1 };
    int[] b = { 1, 4, 7, 2, 3, 5, 9, 5 };
    if (loop(a, 2)) {
    printArr(a);
    System.out.println("连续");
    } else {
    printArr(a);
    System.out.println("不连续");
    }
    if (loop(b, 2)) {
    printArr(b);
    System.out.println("连续");
    } else {
    printArr(b);
    System.out.println("不连续");
    }
    }
    }
    /*结果
    {4,3,6,5,3,2,1}连续
    {1,4,7,2,3,5,9,5}不连续
    */不管是不是楼主要,我只是纠正一下我上面的小失误!
    这样的解法,我是这样理解的:把数组先按两个一组分成若干组,余下的作为一组,依次判断是不是都是递增或递减的。若两个一组没有得到每个小组是都是递增或递减的,则按三个一组继续这样做,三个不行就四个一组.....直到整个数组一起作为一组还没有得到是递增或递减的,那么就不连续。如果中途得到了就是连续的。
    举几个例子:{1,2,  3,4, 5}连续的,{2,1   5,4,  3}连续的,{1,2,3,5,6,7}连续的
    {1,4,7,2,3,5,9,5}不连续
    解释一下原因:
    1,2递增 3,4递增 5单独一个数一组随便算作是递增还是递减 ,所以连续。
    2,1递减 5,4递减 3单独一个数一组随便算作是递增还是递减 ,所以连续。
    1,2递增 3,5递增 6,7递增,所以连续。也可以这样理解1,2,3递增5,6,7递增,所以连续。
    最后一个不管这么分都不连续。
    好了,到此告一段落!
      

  14.   

      public static boolean checkseq(int []t){
     
      if((t != null)&&(t.length == 0))
      {
      return false;
      }
      else if(t.length == 1)
      {
      return true;  
      }
     
      int steplength , temp , count=0;
      steplength = t[1] - t[0];
      
      for(int i=2;i<t.length;i++)
      {
     temp = t[i] - t[i-1];
     if((steplength != temp)&&(count++ == 1)) 
     {
                     return false;
     }
      }
      
      return true;
      
      }
      

  15.   


    忘记这个数组时循环连续了,增加一段首尾步长的判断^_^   public static boolean checkseq(int []t){
     
      if((t != null)&&(t.length == 0))
      {
      return false;
      }
      else if(t.length == 1)
      {
      return true;  
      }
     
      int steplength , temp , count=0;
      steplength = t[1] - t[0];
      
      if((t[0] - t[t.length-1])!=steplength)
      {
      count++;
      }
      
      for(int i=2;i<t.length;i++)
      {
     temp = t[i] - t[i-1];
     if((steplength != temp)&&(count++ == 1)) 
     {
                     return false;
     }
      }
      
      return true;
      
      }
      

  16.   

    [code = java]
      /**写一个函数传入一个任意整形数组 
     * 判断数组元素是否为连续数字如{1,2,3,4}
     * 或{2,1,4,3},而{1,2,4,3}则不是,
     * 是返回TRUE,不是返回FALSE
     * @author hlw
     *
     */
    public class ContinuousCheck {
    public static boolean isContinue(int[] args){
    if(args.length<2) return true;
    boolean breaked = false;
    int step = args[0]<args[1]?1:-1;
    for(int i = 0;i<args.length-1;i++){
    if(args[i]+step != args[i+1]) {
    if(breaked) return false;
    else {
    breaked = true;
    }
    }
    }
    if(breaked&&args[args.length-1]+step != args[0]) return false;
    return true;
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
    System.out.println(ContinuousCheck.isContinue(new int[]{1, 2, 3, 4}));
    System.out.println(ContinuousCheck.isContinue(new int[]{4, 3, 2, 1}));
    System.out.println(ContinuousCheck.isContinue(new int[]{2, 1, 4, 3}));
    System.out.println(ContinuousCheck.isContinue(new int[]{1, 2, 4, 3}));
    }
    }
    [/code]
      

  17.   

    上面没考虑到断点在第二个的情况,
    int step = args[0]<args[1]?1:-1;
    改为
    int step = args[0]<args[1]&&args[0]+1 == args[1]?1:-1;