小易去附近的商店买苹果,奸诈的商贩使用了捆绑交易,只提供6个每袋和8个每袋的包装(包装不可拆分)。 可是小易现在只想购买恰好n个苹果,小易想购买尽量少的袋数方便携带。如果不能购买恰好n个苹果,小易将不会购买。
输入描述:
输入一个整数n,表示小易想购买n(1 ≤ n ≤ 100)个苹果
输出描述:
输出一个整数表示最少需要购买的袋数,如果不能买恰好n个苹果则输出-1
示例1
输入
20
输出
3
代码:
链接:https://www.nowcoder.com/questionTerminal/61cfbb2e62104bc8aa3da5d44d38a6ef
来源:牛客网
#include <stdio.h>
#include <iostream>
#include "math.h"
using namespace std;
int main(){
int n;
cin>>n;
int count = 0;
while(n>=8){
n -= 8;
count++;
}
if(n == 6 || (n == 4 && count >=1) || (n == 2 && count >=2))
count++;
else
if(n == 0)
count = count;
else
count = -1;
cout<<count<<endl;
return 0;
}