首页 > 试题广场 >

买橘子

[编程题]买橘子
  • 热度指数:11561 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}小明去附近的水果店买橙子。水果商贩只提供整袋购买,有每袋 6 个和每袋 8 个的包装(包装不可拆分)。可是小明只想购买恰好 n 个橙子,并且尽量少的袋数方便携带,请你帮他计算一下最少需要购买的袋数。如果不能购买恰好 n 个橙子,小明将不会购买,此时输出 -1

输入描述:
\hspace{15pt}在一行上输入一个整数 n \left(1 \leqq n \leqq 10^5\right),表示小明想要购买的橙子数量。


输出描述:
\hspace{15pt}如果不能通过购买整袋橙子凑出 n 个橙子,则输出 -1;否则,输出一个整数表示最少需要购买的袋数。
示例1

输入

20

输出

3
示例2

输入

7

输出

-1

备注:
本题已于下方时间节点更新,请注意题解时效性:
1. 2025-06-25 优化题面文本与格式,修正为单组输入。
2. 2025-08-07 优化题面文本与格式,扩大数据范围。
3. 2025-11-19 优化题面文本与格式;补充一组样例。新增若干组测试数据。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int ans = Integer.MAX_VALUE;
        boolean flag = false;
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= n; j++) {
                if (6 * i + 8 * j == n) {
                    ans = Math.min(ans, i + j);
                    flag = true;
                }
            }
        }
        if (flag) {
            System.out.println(ans);
        } else {
            System.out.println(-1);
        }
    }
}

发表于 2025-11-15 01:35:28 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
     System.out.println(minBags(sc.nextInt()));
    }
    public static int minBags(int n ){
        int Max = Integer.MAX_VALUE;
        for( int y=0 ; y<=n/8 ;y++)
        {
           int modile = n-8*y;
           if(modile>=0&&modile%6==0){
            int x =modile/6;
            Max=Math.min(Max,x+y);
            }
        }
        return Max == Integer.MAX_VALUE ? -1:Max;
    }
}//
发表于 2025-09-04 11:39:00 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(); // 目标橙子数量

        // 标签用于找到结果后直接结束
        res: {
            // 全用8个装的情况
            if (n % 8 == 0) {
                System.out.println(n / 8);
                break res;
            }

            // 从多到少尝试8个装的袋数
            for (int e = n / 8; e >= 0; e--) {
                int left = n - e * 8; // 剩余橙子数

                // 剩余数量能被6整除
                if (left >= 0 && left % 6 == 0) {
                    System.out.println(e + left / 6); // 总袋数
                    break res;
                }
            }

            // 无法恰好购买
            System.out.println(-1);
        }
        sc.close();
    }
}

发表于 2025-08-26 19:51:19 回复(0)
Scanner in = newScanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别  doro买橘子
        while(in.hasNextInt()) { // 注意 while 处理多个 case
            inta = in.nextInt();
            int b,c = a / 8;
            if(a % 2== 1){System.out.println(-1);return;}
            b = a - a%8*8;
            while(0< b&&b < 6){  //抓住8和6之间的差值就好了
              b+=2;
              c--;
              if(c==0){System.out.println(-1);break;}
            }
            System.out.println(a/8+1);
        }
发表于 2025-07-30 20:22:36 回复(0)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        if (n % 2 != 0) {

            System.out.print("-1");

            return;

        }
        int count=0;
        while(n>24) {
            
            count++;
            n-=8;
            //System.out.println(n);
        }
        
        if(n==16) {
            
            System.out.println(count+2);
            
        }else {
            
            System.out.println(count+3);
        }
        
        

    }

}
发表于 2020-06-06 20:44:36 回复(0)

问题信息

上传者:小小
难度:
5条回答 5371浏览

热门推荐

通过挑战的用户

查看代码
买橘子