POJ2586(贪心)
来源:互联网 发布:linux 执行权限 编辑:程序博客网 时间:2024/05/02 00:11
题意:
有一个公司,每个月要么亏损d元,要么盈利s元.一年十二个月,每个月的盈利亏损情况
都不知道,唯一知道的就是,每五个月的总和都是亏损的,1-5,2-6,3-7….8-12.要解决的问
题就是这个公司一年最大能盈利多少钱,输出结果,否则就输出”Deficit”
分析:
题目意思看懂了就好做了.我的思路就是每五个月亏损月份的个数都是一样的,那么选择
亏损的月份尽量都要再其他的月份区间,也就是尽可能的选择后面的月份作为亏损月份,这样
获得的利润就最大.这个就是贪心了.
枚举也可以解决问题
代码:
#include <iostream>#include <cmath>#include <algorithm>using namespace std;int main(){ double s,d; int arr[12]; while(cin>>s>>d) { int cns=-1; for(int i=0;i<12;i++) arr[i]=s; //计算需要五个月中需要几个月是亏损的 for(int i=1;i<=5;i++) { if((i*d-(5-i)*s) > 0) { cns=i; break; } } if(cns==-1 || cns ==5) { cout<<"Deficit"<<endl; continue; } //计算八个区间中每个区间内月份亏损的个数 int i,j,cns02,k; for(i=0;i<8;i++) { cns02=0; //统计当前区间月份亏损的个数 for(j=i;j<i+5;j++) { if(arr[j]<0) cns02++; } k=i+4; while(cns != cns02) { if(arr[k]>0) { arr[k]=-d; k--; cns02++; } } } double sum=0; for(int m=0;m<12;m++) sum+=arr[m]; if(sum<0) cout<<"Deficit"<<endl; else cout<<sum<<endl; } return 0;}
阅读全文
0 0
- POJ2586--贪心
- poj2586 贪心
- POJ2586(贪心)
- poj2586贪心
- POJ2586(贪心)
- 贪心poj2586
- poj2586 典型贪心
- poj2586 贪心算法
- 【贪心】POJ2586 Y2K Accounting Bug
- poj2586 Y2K Accounting Bug 贪心
- poj2586 (盈亏计算贪心)
- POJ2586 Y2K Accounting Bug(贪心)
- POJ2586 Y2K Accounting Bug 贪心
- poj1323,poj2586贪心容易题
- poj2586 Y2K Accounting Bug 贪心
- poj2586 Y2K Accounting Bug 贪心
- POJ2586 Y2K Accounting Bug(贪心)
- poj2586 贪心水题一道(一步贪心)
- 单片机实验4
- HttpUtil
- document.ready和onload的区别
- 线程、进程间的通信方式
- 【Errcode:13-Permission denied】mysqlbinlog:File'/var/log/mysql/mysql-bin.000001' not found解决方案
- POJ2586(贪心)
- Spring Security实现动态权限管理
- python3 爬虫环境准备 (二)
- 前端框架之Vue.js学习总结
- 线程高级.md
- python--func1
- sturts2_json插件使用
- Cpp_String深拷贝
- 软件安全性测试