您的位置:首页 > 编程语言 > C语言/C++

【CCF-CSP 201709-1】打酱油

2018-03-11 22:12 302 查看
问题描述
  小明带着N元钱去买酱油。酱油10块钱一瓶,商家进行促销,每买3瓶送1瓶,或者每买5瓶送2瓶。请问小明最多可以得到多少瓶酱油。
输入格式
  输入的第一行包含一个整数N,表示小明可用于买酱油的钱数。N是10的整数倍,N不超过300。
输出格式
  输出一个整数,表示小明最多可以得到多少瓶酱油。
样例输入
40
样例输出
5
样例说明
  把40元分成30元和10元,分别买3瓶和1瓶,其中3瓶送1瓶,共得到5瓶。
样例输入
80
样例输出
11
样例说明
  把80元分成30元和50元,分别买3瓶和5瓶,其中3瓶送1瓶,5瓶送2瓶,共得到11瓶。
#include <iostream>
#include <fstream>
using namespace std;

void input_data();
int excute();

int N;

void input_data(){
cin>>N;
}

int excute(){
// 3 --> 1
// 5 --> 2
// limit function: 50*num_5 + 30*num_3 <= N; N <= 300; --> 5*num_5 + 3*num_3 <= 30
// target function = (5*num_5 + 2*num_5) + (3*num_3 + 1*num_3)
int num_5;  // 5*num_5 + 3*num_3 <= 30  -->  num_5 <= 6, when num_3=0
int num_3;  // 5*num_5 + 3*num_3 <= 30 --> num_3 <= 10, when num_5=0
int max_buy = 0;
int temp_buy_num, money_used, buy_one;
int limit_5 = 6;
int limit_3 = 10;

for(num_5 = 0; num_5 <= limit_5; num_5++){
for(num_3 = 0; num_3 <= limit_3; num_3++){
money_used = 50*num_5 + 30*num_3;
buy_one = 0;
if(money_used <= N){
// buy one
if((N - money_used) >= 10){
buy_one = (N - money_used)/10;
}
temp_buy_num = (7*num_5 + 4*num_3) + buy_one;
if(max_buy < temp_buy_num){
max_buy = temp_buy_num;
// cout<<"num_5 = "<<num_5<<"; num_3 = "<<num_3<<"; money_used = "<<money_used<<"; buy_one = "<<buy_one<<"; max_buy = "<<max_buy<<endl;
}
}
}
}

return max_buy;
}

int main(){

input_data();
cout<<excute()<<endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  CCF CSP C++ 打酱油