递归与分治法 | Poj2586
来源:互联网 发布:抢红包开挂软件 编辑:程序博客网 时间:2024/06/07 04:10
题目:http://poj.org/problem?id=2586
Description
All what they remember is that MS Inc. posted a surplus or a deficit each month of 1999 and each month when MS Inc. posted surplus, the amount of surplus was s and each month when MS Inc. posted deficit, the deficit was d. They do not remember which or how many months posted surplus or deficit. MS Inc., unlike other companies, posts their earnings for each consecutive 5 months during a year. ACM knows that each of these 8 postings reported a deficit but they do not know how much. The chief accountant is almost sure that MS Inc. was about to post surplus for the entire year of 1999. Almost but not quite.
Write a program, which decides whether MS Inc. suffered a deficit during 1999, or if a surplus for 1999 was possible, what is the maximum amount of surplus that they can post.
Input
Output
Sample Input
59 237375 743200000 8496942500000 8000000
Sample Output
11628300612Deficit
基本思路:
这个题目本身是一种穷举算法,每个月两种可能性(盈利或赤字),12月一共2的12次方种可能性。于是题目的要求是要尽量盈利,因此我们无需穷举所以的可能性,而只需要用递归的方式,尽可能的让每个月盈利。针对其中某个月,我们首先认为当月盈利;当当月盈利导致整年的收支不符合要求时,我们再认为当月亏损。
代码:
#include <iostream>
#include <math.h>
using namespace std;
int* income;
int s,d;
int Cal5Month(int Month)
{
int sum = 0;
for(int i = Month - 4; i <= Month; i++)
{
sum += income[i];
}
return sum;
}
bool SetIncome(int Month)
{
if(Month == 11)
{
income[Month] = s;
if(Cal5Month(Month) >= 0)
{
income[Month] = d;
if(Cal5Month(Month) >= 0)
{
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
else if(Month >= 4)
{
income[Month] = s;
if(Cal5Month(Month) < 0 && SetIncome(Month+1))
{
return true;
}
else
{
income[Month] = d;
if(Cal5Month(Month) < 0 && SetIncome(Month+1))
{
return true;
}
else
{
return false;
}
}
}
else
{
income[Month] = s;
if(SetIncome(Month+1))
{
return true;
}
else
{
income[Month] = d;
return SetIncome(Month+1);
}
}
}
int main()
{
income = new int[12];
while(cin>>s>>d)
{
d *= -1;
if(SetIncome(0))
{
int sum = 0;
for(int i = 0; i < 12; i++)
{
sum += income[i];
}
if(sum < 0)
{
cout<<"Deficit"<<endl;
}
else
{
cout<<sum<<endl;
}
}
else
{
cout<<"Deficit"<<endl;
}
}
return 0;
}
- 递归与分治法 | Poj2586
- 递归与分治法
- 分治法---话说递归与汉诺塔
- 递归与分治:归并排序法
- 递归与分治:快速排序法
- 分治与递归法:全排列问题
- 递归:分治法与动态规划
- 递归与分治策略
- 递归与分治策略
- 分治与递归
- 递归与分治策略
- 递归与分治策略
- 递归与分治
- 递归与分治2
- 递归与分治
- 递归与分治策略
- 分治与递归分析
- 递归与分治分析
- 给extjs4 desktop 的windows 换肤
- uva 165 Stamps (DFS)
- the comment about movie "men are pigs"
- 解决phpmyadmin空密码无法登陆mysql
- 黑马程序员 java高新技术之注解,泛型,类加载器
- 递归与分治法 | Poj2586
- Mahout源码K均值聚类算法分析(3)
- 编译和链接(一)
- 黑界神兵-Cain
- win32多线程-在MFC程序中使用多线程
- 职业里面的细小方向
- 规划
- jsp图形验证码
- 数学专项matrix:UVa 11149