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 <cstdio>
#include <iostream>
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;
}



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 【一个少妇去应征工作,随手将走廊上的纸片捡起来,放进了垃圾桶,被路过的考官看到了,他因此得到了这份工作】 类似这样...
    贺小桶阅读 1,202评论 0 1
  • 说到最佳方案, 想起很早时候(刚参加工作)读到的一本书《Ubuntu最佳解决方案》,很是喜欢。方案的优劣,仁者见仁...
    jiaxiaolei阅读 1,571评论 0 0

友情链接更多精彩内容