24h購物| | PChome| 登入
2013-02-23 11:29:09| 人氣1,235| 回應0 | 上一篇 | 下一篇

[UVA] 10576 - Y2K Accounting Bug

推薦 0 收藏 0 轉貼0 訂閱站台


  Problem B: Y2K Accounting Bug 

Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vital data for preparing annual report for MS Inc.

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 and Output 

Input is a sequence of lines, each containing two positive integers s and d. For each line of input, output one line containing either a single integer giving the amount of surplus for the entire year, or output `Deficit' if it is impossible.

Sample input 

59 237
375 743
200000 849694
2500000 8000000

Sample Output 

116
28
300612
Deficit

題意 :
ACM 這家公司失去了每個月是否盈利還是赤字,
每個月不是賺 $s 就是虧 $d,他們會公布連續五個月的盈虧,
因此每年會公布八次,現在知道這八次都是赤字。

問最多可以獲利多少?

解法:
窮舉月份是否為賺或虧,因此需要 2^12 次,然後檢查符合的條件即可。



#include <stdio.h>

int main() {
    int s, d;
    while(scanf("%d %d", &s, &d) == 2) {
        int y_surplus = -1, year, i, j;
        for(year = (1<<12)-1; year >= 0; year--) {
            int tot = 0, flag = 1;
            for(i = 0; i < 12; i++) {
                if((year>>i)&1)
                    tot += s;
                else
                    tot -= d;
                if(i < 8) {
                    int tmp = 0;
                    for(j = i; j < i+5; j++)
                        if((year>>j)&1)
                            tmp += s;
                        else
                            tmp -= d;
                    if(tmp >= 0)    flag = 0;
                }
            }
            if(flag && y_surplus < tot)
                y_surplus = tot;
        }
        if(y_surplus < 0)   puts("Deficit");
        else
            printf("%d\n", y_surplus);
    }
    return 0;
}

台長: Morris
人氣(1,235) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][dp] 10532 - Combination! Once Again
此分類上一篇:[UVA][中國餘數定理] 756 - Biorhythms

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文