6个苹果、8个苹果

买苹果

http://www.nowcoder.com/questionTerminal/61cfbb2e62104bc8aa3da5d44d38a6ef

6和8的最小公倍数是24,只要24就买3个8,其余几个值单列出来:

import java.util.*;
public class Main {
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<Integer> v = new ArrayList<>(Arrays.asList(0, 6, 8, 12, 14, 16, 20, 22));
        List<Integer> r = new ArrayList<>(Arrays.asList(0, 1, 1, 2, 2, 2, 3, 3));
        int ans = n/24*3;
        if(v.contains(n%24)) System.out.println(ans + r.get(v.indexOf(n%24)));
        else System.out.println(-1);
    }
}
全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务