题解 | 买橘子

买橘子

https://www.nowcoder.com/practice/73e0552b78474a9086781e47f4e01d73

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 满足6x+8y=n
        // 尽量少的袋数 8袋的优先
        // n/8 =?
        // n/6 = ?
        int n = -1;
        int m = -1;

        int apples = in.nextInt();
    //    if (apples < 8 && apples != 6 ) {
    //         System.out.println(-1);
    //         return;
    //     }
        if (apples % 6 == 0) {
            n = apples / 6;
        }   else {
            n = apples / 6 + 1;
        }
        if (apples % 8 == 0) {
            m = apples / 8;
        }   else {
            m = apples / 8 + 1;
        }



        in.nextLine();
        int min = 999999;
        //6袋装的数量
        // System.out.println(m +" " +n);
        for (int i = 0; i <= n; i++) {

            //8袋装数量
            for (int j = 0 ; j <= m; j++) {
    //  System.out.println(i +" " +j);
                if (6 * i + 8 * j == apples) {
                    min = Math.min(min, i + j );
                }
            }
        }
        if (min == 999999) {
            System.out.println(-1);
        } else {
            System.out.println(min);
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务