Going to a nearby store to buy apples, the treacherous vendor used a bundling deal, offering only 6-per-bag and 8-per-bag packs (packs not splittable).
But now I just want to buy exactly n apples, and I want to buy as few bags as possible for easy portability. If you can't buy exactly n apples, you won't buy it.
Enter description:
Enter an integer n, indicating that Xiaoyi wants to buy n (1 ≤ n ≤ 100) apples
Output description:
Output an integer indicating the minimum number of bags to buy, or -1 if you can't buy exactly n apples
Input example 1:
20
Output example 1:
3
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class M1 { public static void main(String[] args) { Scanner scanner =new Scanner(System.in); int n=scanner.nextInt(); ArrayList<Integer> m=new ArrayList<Integer>(); for (int i = 0; i <= n/6; i++) { int s=0; double y=(n-6*i)/8.0; if (y-(int) y==0) { s=(int) (i+y); m.add(s); } } if (m.size()==0) { System.out.println(-1); }else { Collections.sort(m); System.out.println(m.get(0)); } } }