zoukankan      html  css  js  c++  java
  • POJ2586 Y2K Accounting Bug(贪心)

    题目链接

    题目大意:

    题目相当晦涩难懂啊。

    一年的12个月,每5个月统计一次,如从1~5,2~6,3~7,...,8~12共统计了8次,已知每次都deficit,问这一年有没有盈利的可能。

    一个月surplus只能是s,deficit只能是d,不能是其它的值。

    分析:

    用贪心,先计算1~5,让每个月都盈利,然后从第5个月向前deficit,直到1~5的和为deficit。继续2~6,3~7.。。。,8~12.

    最后统计全年是否盈利。

    AC代码如下:

    #include <iostream>
    #include <queue>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    
    using namespace std;
    
    int main(){
        int s, d, a[12];
    
        while(scanf("%d%d", &s, &d) == 2) {
            memset(a, 0, sizeof(a));
    
            for(int i=0; i<5; i++) a[i] = s;
    
            for(int i=0; i<8; i++) {
                a[i+5-1] = s;
                while(true) {
                    int ans = 0;
                    for(int j=0; j<5; j++) {
                        ans += a[i+j];
                    }
    
                    if(ans > 0) {
                        int j;
                        for(j=i+4; j>=i; j--) if(a[j] > 0) {
                            a[j] = -d; break;
                        }
                    }
                    else break;
                }
            }
    
            int ans = 0;
            for(int i=0; i<12; i++) ans += a[i];
    
            if(ans > 0) printf("%d
    ", ans);
            else printf("Deficit
    ");
        }
    
        return 0;
    }
  • 相关阅读:
    函数
    A × B problem
    求n的阶乘
    自己构建一个vector函数
    int与string的互相转化
    列一列(斐波那契数列)
    找一找
    c++大数计算模板
    JSON--js中 json字符串转对象、对象转字符串
    JSON
  • 原文地址:https://www.cnblogs.com/tanhehe/p/3237751.html
Copyright © 2011-2022 走看看