UVa 700 - Date Bugs

来源:互联网 发布:java多线程 编辑:程序博客网 时间:2024/06/11 00:46

題目:有一些電腦有千年蟲問題(數據溢出),問最小的可能實際時間。

分析:搜索。枚舉其中一個電腦的可能時間和其他電腦匹配找到最小的合法值。

            yi,ai,bi分別代表當前顯示時間、溢出時重新計時時間、溢出上屆;

            實際時間為yi + k*(bi - ai),k取不小於0的整數;

            枚舉地一個電腦的可能時間,判斷其他的電腦是否合法即可。

說明:有是很久沒刷題了╮(╯▽╰)╭。

#include <stdio.h>#include <stdlib.h>int a[22], b[22], y[22];int main(){    int n, cases = 1;    while (~scanf("%d",&n) && n) {        for (int i = 0; i < n; ++ i) {            scanf("%d%d%d",&y[i],&a[i],&b[i]);        }                int ans = y[0], det = b[0] - a[0];        while (ans < 10000) {            int flag = 1;            for (int i = 1; i < n; ++ i) {                if (ans < y[i] || (ans-y[i])%(b[i]-a[i])) {                    flag = 0;                    break;                }            }            if (flag) {                break;            }            ans += det;        }                printf("Case #%d:\n",cases ++);        if (ans < 10000) {            printf("The actual year is %d.\n",ans);        }else {            printf("Unknown bugs detected.\n");        }        puts("");    }    return 0;}


0 0