hdu 4551 生日猜猜猜

生日猜猜猜

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


Problem Description
小明对生日十分看重,因为他可以得到祝福,可以和朋友亲人一起分享快乐,可以为自己的人生做一次总结,并且...能够收到好多礼物!
不过小明是个神秘的人,不会轻易告诉你他的生日,现在他想到一个办法,让你去猜他的生日是哪一天。

小明会告诉你如下三个信息:

1. 出生月份和出生日子的最大公约数;
2. 出生月份和出生日子的最小公倍数;
3. 出生年份;

现在要求你猜出小明的生日。
 

 

Input
第一行输入一个正整数T,表示总共有T组册数数据(T <= 200);
对于每组数据依次输入三个数x,y,z,
x表示出生月份和出生日子的最大公约数(1<= x <=1000);
y表示出生月份和出生日子的最小公倍数(1<= y <=1000);
z表示出生年份(1900 <= z <= 2013)。
每组输入数据占一行。
 

 

Output
对于每组数据,先输出Case数。
如果答案不存在 ,输出“-1”;
如果答案存在但不唯一 ,输出“1”;
如果答案唯一,输出生日,日期格式为YYYY/MM/DD;
每组输出占一行,具体输出格式参见样例。
 

 

Sample Input
3 12 24 1992 3 70 1999 9 18 1999
 

 

Sample Output
Case #1: 1992/12/24 Case #2: -1 Case #3: 1999/09/18
 
 
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>

using namespace std;

inline int gcd(int a, int b){
    if(a<b)swap(a,b);
    while(b){
        int tmp = a%b; a = b; b = tmp;
    }
    return a;
}

inline bool isLeapYear(int year){
    return (year%4 == 0&& year%100 != 0)||(year%400 == 0);
}

int main(){
    int T;
    cin >>T;
    for(int iCase = 1; iCase <= T; iCase ++){
        int x,y,z;
        cin >>x>>y>>z;
        int day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
        if(isLeapYear(z)) day[1]++;
        int cnt = 0;
        int birthMon,birthDay;
        bool flag = false;

        for(int i = 1; i <= 12 && !flag; i ++){
            for(int j = 1; j <= day[i-1] && !flag; j ++ ){
                if(i*j == x*y){
                    if(x == gcd(i,j)){
                        cnt++;
                        birthMon=i;
                        birthDay =j;
                    }
                    if( cnt > 1) {flag = true;}
                }
            }
        }
        if(cnt == 0) cout<<"Case #"<<iCase<<": "<<-1<<endl;
        else if(flag) cout<<"Case #"<<iCase<<": "<<1<<endl;
        else {
            printf("Case #%d: %.4d/%.2d/%.2d\n",iCase,z,birthMon,birthDay);
        }
    }
}

  

posted @ 2013-05-18 21:03  OpenSoucre  阅读(257)  评论(0编辑  收藏  举报