CH1-UVA10070-Leap Year or Not Leap Year and ...

The ancient race of Gulamatu is very advanced in their year calculation scheme. They understand what
leap year is (A year that is divisible by 4 and not divisible by 100 with the exception that years that
are divisible by 400 are also leap year.) and they have also similar festival years. One is the Huluculu
festival (happens on years divisible by 15) and the Bulukulu festival (Happens on years divisible by 55
provided that is also a leap year). Given an year you will have to state what properties these years
have. If the year is not leap year nor festival year, then print the line ‘This is an ordinary year.’
The order of printing (if present) the properties is: leap year → huluculu → bulukulu.
Input
Input will contain several years as input. Each year will be in separate lines. Input is terminated by
end of file. All the years will not be less than 2000 (to avoid the earlier different rules for leap years).
Please don’t assume anything else.

Output
For each input, output the different properties of the years in different lines according to previous
description and sample output. A blank line should separate the output for each line of input. Note
that there are four different properties.

Sample Input

2000
3600
4515
2001

Sample Output

This is leap year.

This is leap year.
This is huluculu festival year.

This is huluculu festival year.

This is an ordinary year.

易错点:

1.由于题目说明了Please don’t assume anything else, 所以输入会有可能是大整数, 所以我们得采用字符串来记录输入, 再转换成对应的整数进行相应的大叔计算

2.题目要求每组之间必须用空行隔开, 这看似很简单, 但是对于最后一组不应该再有空行。

思想

对于大整数的处理, 我们的思想是用字符串存储大整数, 再按位转换为对应的整数存储在整数数组中, 然后进行相关的运算。

对于取mod运算, 我们主要通过模拟实际运算过程。如下就是模拟除法运算求余数的过程。先求第一位的余数, 就是我们除法中的运算。

#include 
#include 
using std::cin;
#define MAX_LEN 100000
char input[MAX_LEN];

int bigNumberMod(char *in , int divisor) {
    int mod = 0;
    for (char *p = in; *p; p++) {
      mod = (mod * 10 + (*p - '0')) % divisor;
    }
    return mod;
}

int main() {
    int print = 0;
    while (cin >> input) {

        bool isLeap = false;
        bool isFestival = false;

        // 避免了给最后一组数据加上空行
        if ( print != 0 )
            printf("\n");
        print = 1;

        if (bigNumberMod(input, 4) == 0 && bigNumberMod(input, 100) != 0 || bigNumberMod(input, 400) == 0) {
            printf("This is leap year.\n");
            isLeap = true;
        }

        if ( 0 == bigNumberMod(input, 15) ) {
            printf("This is huluculu festival year.\n");
            isFestival = true;
        }

        if (isLeap && (0 == bigNumberMod(input, 55))) {
            printf("This is bulukulu festival year.\n");
            isFestival = true;
        }

        if (!isLeap && !isFestival) printf("This is an ordinary year.\n");
    }
    return 0;
}



你可能感兴趣的:(CH1-UVA10070-Leap Year or Not Leap Year and ...)