您的位置 首页 编程语言

An array is defined to be odd-heavy if it contains at least one odd element

美国面试题

 

3. An array is defined to be odd-heavy if it contains at least one odd element and every element whose value is odd is greater than every even-valued element. So {11, 4, 9, 2, 8} is odd-heavy because the two odd elements (11 and 9) are greater than all the even elements. And {11, 4, 9, 2, 3, 10} is not odd-heavy because the even element 10 is greater than the odd element 9.
Write a function called isOddHeavy that accepts an integer array and returns 1 if the array is odd-heavy; otherwise it returns 0.
If you are programming in Java or C#, the function signature is int isOddHeavy(int[ ] a)
If you are programming in C or C++, the function signature is int isOddHeavy(int a[ ], int len) where len is the number of elements in the array

 

样例:

 

 

java实现代码:

package com.zzy;

/**
 * 更多请关注: http://huamaodashu.com
 * Created by huamaodashu on 20/07/2018.
 */
public class OddHeavy {
    public static void main(String[] args) {

//        int[]a={2};
//        int[]a={1, 1, 1, 1, 1, 1};
//        int[]a={2, 4, 6, 8, 11};
        int[]a={-2, -4, -6, -8, -11};
        System.out.println(isOddHeavy(a));

    }

    public static int isOddHeavy(int[]a){

        if(a.length==0){
            return 0;
        }else if(a.length==1){
            if (isEven(a[0])==0){
                return 1;
            }else {
                return 0;
            }
        }else {
            int maxEvent=a[0];
            int maxOdd=a[0];
            for (int i = 0; i < a.length; i++) {
                if(isEven(a[i])==1){

                    if(maxEvent<a[i]){
                        maxEvent=a[i];
                    }

                }else if(isEven(a[i])==0){
                    if(maxOdd<a[i]){
                        maxOdd=a[i];
                    }

                }else {
                    return 0;
                }
            }

            if(maxEvent<maxOdd){
                return 1;
            }else if(maxEvent==maxOdd &&isEven(maxOdd)==0){
                return 1;
            }

        }
        return 0;
    }

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


}

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

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

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

hadoopall

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

热门文章

发表评论

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