解题思路:
- 首先先判断不能买的条件,因为偶数和偶数组合得到的是偶数,所以奇数是不能买的,最少购买是6或8或 12个,所以在0到14区间非6非8非12不能买
- 因为要用最少的袋子,所以我们要尽可能的使用8个袋子去装
- 所以我们创建count来表示需要的8个橙子袋子数目,用e来表示对8取余的余数
- 当e为0时,全部用8个橙子的袋子,直接输出count,当e为2时,count中需要有两个袋子各拿出来2个和 余数凑成6,所以总袋子数会多一个,即count+1,余数为4时同理,余数为6时也是。
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b=8;
int c=6;
int count;
if(a%2!=0||a<6||(a>8&&a<12)){
System.out.println("-1");
return;
}
int e;
count=a/8;
e=a%8;
if(e==0){
System.out.println(count);
return;
}else if(e==2||e==4||e==6){
System.out.println(count+1);
return;
}
}
}