华为OJ(购物单)
2015-08-18 18:11
225 查看
述 | 王强今天很开心,公司发给N元的年终奖。王强决定把年终奖用于购物,他把想买的物品分为两类:主件与附件,附件是从属于某个主件的,下表就是一些主件与附件的例子:
| ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
知识点 | 字符串 | ||||||||||
运行时间限制 | 0M | ||||||||||
内存限制 | 0 | ||||||||||
输入 | 输入的第 1 行,为两个正整数,用一个空格隔开:N m (其中 N ( <32000 )表示总钱数, m ( <60 )为希望购买物品的个数。) 从第 2 行到第 m+1 行,第 j 行给出了编号为 j-1 的物品的基本数据,每行有 3 个非负整数 v p q (其中 v 表示该物品的价格( v<10000 ), p 表示该物品的重要度( 1 ~ 5 ), q 表示该物品是主件还是附件。如果 q=0 ,表示该物品为主件,如果 q>0 ,表示该物品为附件, q 是所属主件的编号) | ||||||||||
输出 | 输出文件只有一个正整数,为不超过总钱数的物品的价格与重要度乘积的总和的最大值( <200000 )。 | ||||||||||
样例输入 | 1000 5 800 2 0 400 5 1 300 5 1 400 3 0 500 2 0 | ||||||||||
样例输出 | 2200 |
#include<iostream> using namespace std; struct goods { int price; int w; int type; }; const int Max=20; goods a[Max]; int count_value(int,int,int); int number; int main() { int n; cin>>n; cin>>number; for(int i=0;i<number;i++) { cin>>a[i].price; cin>>a[i].w; cin>>a[i].type; } cout<<count_value(0,n,1); //system("pause"); return 0; } int count_value(int index,int money,int f) { int t1=0,t2=0; if(index>=number) return 0; if(a[index].type==0) { if(money>=a[index].price) { t1=count_value(index+1,money-a[index].price,1)+a[index].price*a[index].w; t2=count_value(index+1,money,0); return t1>t2?t1:t2; } else return count_value(index+1,money,0); } else if((a[index].type!=0)&&(f==1)) { if(money>=a[index].price) t1=count_value(index+1,money-a[index].price,f)+a[index].price*a[index].w; t2=count_value(index+1,money,f); return t1>t2?t1:t2; } else return count_value(index+1,money,0); }
相关文章推荐
- 用一枚实名戒指来弥补我对你的爱
- Codeforces C The Game of Efil (暴力枚举状态)
- android内置存储器memory和第三方外部存储disk管理
- 折返(Reentrancy)VS线程安全(Thread safety)
- POJ 2393 Yogurt factory
- 全半角空格导致的Sql Server Analysis Services处理错误(转载)
- Handler机制的初步理解
- java中的向上转型和向下转型
- 出现Unable to locate appropriate constructor on class解决方案
- Objective-C NSDate的使用
- golang自动导入postgresql脚本
- failed to open gcomm backend connection: 13: error while trying to listen 'tcp:/
- PHP之——中文乱码解决方案
- Objective-C 常见的结构体
- 零代码搭建RESTful server
- 【Ajax技术】解决XHR与中文乱码问题
- ActiveMQ消息中间件Producer和Consumer
- Objective-C 集合对象的内存管理
- linux /etc/hosts文件作用
- 平衡二叉树的实现原理