首页 > 代码库 > 网易2017 最大奇约数

网易2017 最大奇约数

package com.net163.question;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * Created by Administrator on 2016/11/11 0011.
 */
public class MaxApproximate {
    private int N = 7;

    private int maxApprNumber(int number) {
        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        for (int i = 1; i * i <= number; i++) {
            if (number % i == 0) {
                arrayList.add(number / i);
                arrayList.add(i);
            }
        }
        Collections.sort(arrayList, new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        for(int x:arrayList){
            if (x%2!=0){
                return x;
            }
        }
        return 0;
    }

    @Test
    public void testMaxApprCount() {
        int count = 0;
        for (int i = 1; i <= N; i++) {
            int tmp = maxApprNumber(i);
            count += tmp;
            System.out.println(tmp);
        }
        System.out.println(count);
    }
}

没啥难度直接贴代码

网易2017 最大奇约数