UVA11947 Cancer or Scorpio【日期计算】

Alice and Michael is a young couple who are planning on having their first child. Their wish their son Nelson was born on a special date for both of them.
    Alice has investigated in the internet and has found that the period of gestation is forty weeks. These forty weeks begin to count on the first day of the last menstrual cycle.
    Michael is passionate about astrology and even more about the zodiac signs, he has asked Alice to investigate the range of dates that correspond to each sign.

Sign Begin End
Aquarius January, 21 February, 19
Pisces February, 20 March, 20
Aries March, 21 April, 20
Taurus April, 21 May, 21
Gemini May, 22 June, 21
Cancer June, 22 July, 22
Leo July, 23 August, 21
Virgo August, 22 September, 23
Libra September, 24 October, 23
Scorpio October, 24 November, 22
Sagittarius November, 23 December, 22
Capricorn December, 23 January, 20
UVA11947 Cancer or Scorpio【日期计算】_第1张图片
    Alice and Michael ask for help to calculate the date of birth of their son Nelson and his zodiac sign.
Input
The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that follow.
    Each dataset consists of a single line of input that contains only eight digits that represent the date of the first day of the last menstrual cycle in format MMDDY Y Y Y .
Output
For each dataset, you should generate one line of output with the following values: The dataset number as a decimal integer (start counting at one), a space, the date of birth in format MM/DD/Y Y Y Y , a space, and the name (in lowercase) of zodiac sign that correspond according to the date of birth.
Note: Considers leap years.
Sample Input
2
01232009
01232008
Sample Output
1 10/30/2009 scorpio
2 10/29/2008 scorpio

问题链接:UVA11947 Cancer or Scorpio
问题简述:(略)
问题分析:日期计算问题,用模拟来解决。简单问题不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA11947 Cancer or Scorpio */

#include 

int getNum(char str[], int s, int e)
{
    int v = 0;
    for (int i = s; i <= e; i++)
        v = v * 10 + str[i] - '0';
    return v;
}

int  mdays[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int  zodiacBound[] = {0, 21, 20, 21, 21, 22, 22, 23, 22, 24, 24, 23, 23};
char zodiacName[][12] = {
    "capricorn", "aquarius", "pisces", "aries", "taurus", "gemini", "cancer",
    "leo", "virgo", "libra", "scorpio", "sagittarius", "capricorn"};

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

int main()
{
    int  n;
    char s[10];
    while (~scanf("%d",&n))
    for (int k = 1; k <= n; k++) {
        scanf("%s", s);
        int month = getNum(s, 0, 1);
        int day   = getNum(s, 2, 3);
        int year  = getNum(s, 4, 7);

        mdays[2] = 28 + leapyear(year);
        day += 280;
        while (day > mdays[month]) {
            day -= mdays[month];
            month ++;
            if (month > 12) {
                month = 1;
                year++;
                mdays[2] = 28 + leapyear(year);
            }
        }

        printf("%d %02d/%02d/%04d ", k, month, day, year);
        if (day < zodiacBound[month])
            printf("%s\n", zodiacName[month - 1]);
        else
            printf("%s\n", zodiacName[month]);
    }

    return 0;
}

你可能感兴趣的:(#,ICPC-日期与时间,#,ICPC-UVA)