您的位置 首页 编程语言

An array is defined to be inertial if the following conditions hold

1. 美国测试题

1.题目:

 

An array is defined to be inertial if the following conditions hold:
a. it contains at least one odd value
b. the maximum value in the array is even
c. every odd value is greater than every even value that is not the maximum value. So {11, 4, 20, 9, 2, 8} is inertial because
a. it contains at least one odd value
b. the maximum value in the array is 20 which is even
c. the two odd values (11 and 9) are greater than all the
even values that are not equal to 20 (the maximum), i.e., (4, 2, 8}.
However, {12, 11, 4, 9, 2, 3, 10} is not inertial because it fails condition (c), i.e., 10 (which is even) is greater 9 (which is odd) but 10 is not the maximum value in the array.
Write a function called isIntertial that accepts an integer array and returns 1 if the array is inertial; otherwise it returns 0.
If you are programming in Java or C#, the function signature is
int isInertial(int[ ] a
If you are programming in C or C++, the function signature is
int isInertial(int a[ ], int len) where len is the number of elements in the array

 

2.测试用例:

 

3. 代码:

public class Inertial {
    public static void main(String[] args) {
//        int a[] ={1};
//        int a[] ={2};
//        int a[] ={1,2,3,4};
//        int a[] ={1,1,1,1,1,1,2};
//        int a[] ={2,12,4,6,8,11};
//        int a[] ={2,12,12,4,6,8,11};
        int a[] ={-2,-4,-6,-8,-11};
//        int a[] ={2,3,5,7};
//            int a[] ={2,4,6,8,10};
        System.out.println(isInertial(a));

    }

    public static int isInertial(int[] a){
        //1
        int length = a.length;

        int max =a[0];

        if(length <=1){
            return 0;
        }

        ArrayList<Integer> oddarray = new ArrayList();
        ArrayList<Integer> evenarray = new ArrayList();


        for (int i=0; i<length; i++){
//            System.out.println("a[i]"+a[i]);
            if (isOdd(a[i])){ // 是否满足 至少有一个奇数

                oddarray.add(a[i]);

            }else {
                evenarray.add(a[i]);
            }

            if (a[i] > max){ // 最大数是否是偶数
                max = a[i];
            }

        }

        if ( oddarray.size()==0){ //a
            return 0;
        }
        if(!isEven(max)){//b
            return 0;
        }

        System.out.println("max"+max);
//        System.out.println("min"+min);
        //c
       if(!evenarray.isEmpty()){
           int minOdd = oddarray.get(0);



           for (int s : oddarray) { // zhaochu  oddarray 最小
               if (minOdd > s){
                   minOdd = s;
               }

           }

           for (int t: evenarray){
               if(t ==max){

               }else if( t > minOdd){
                   return 0;
               }

           }


       }

        return 1;
    }

    public static boolean isOdd(int a){
        if(a %2 !=0){

        return true;
        }else {
            return false;
        }
    }
    public static boolean isEven(int a){
        if(a%2 ==0){
            return true;
        }else {
            return false;
        }

    }

}

猫叔总结了 适合新手操作的副业 《淘宝虚拟产品月入2万的 6个 细分类目》的电子书 仅供参考

如果你对虚拟产品比较感兴趣,可以点击:

淘宝卖什么虚拟产品赚钱(月入2万+)

hadoopall

关于花猫大叔短视频创业 作者: hadoopall

热门文章

发表评论

电子邮件地址不会被公开。