zoey
点击国内主页可以让浏览速度加快哦 ~ !

关于HDOJ 1076题(An Easy Task(x后第n个闰年)) 的理解(C/C++)

2019-08-09 hdoj 闰年
Word count: 537 | Reading time: 2min

题目描述

                       An Easy Task

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 28473 Accepted Submission(s): 18363

Problem Description
    Ignatius was born in a leap year, so he want to know when he could hold his birthday party. Can you tell him?

Given a positive integers Y which indicate the start year, and a positive integer N, your task is to tell the Nth leap year from year Y.

Note: if year Y is a leap year, then the 1st leap year is year Y.

Input
    The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains two positive integers Y and N(1<=N<=10000).

Output
    For each test case, you should output the Nth leap year from year Y.

Sample Input
3
2005 25
1855 12
2004 10000

Sample Output
2108
1904
43236

    Hint

        We call year Y a leap year only if (Y%4==0 && Y%100!=0) or Y%400==0.

Author
    Ignatius.L

原题链接

More info:Question

Accepted代码

直接暴力解,不超时。另外,闰年的计算按照题目要求的做,即认为满足(Y%4==0 && Y%100!=0)那么Y这年就可以被称为闰年。即不考虑大年份闰年的判别情况

但是实际上,闰年分为普通闰年和世纪闰年。

普通闰年:公历年份是4的倍数的,一般是闰年。(如2004年就是闰年);

世纪闰年:公历年份是整百数的,必须是400的倍数才是闰年(如1900年不是世纪闰年,2000年是世纪闰年);

大年份闰年:对于数值很大的年份,这年如果能整除3200,并且能整除172800则是闰年。如172800年是闰年,86400年不是闰年(因为虽然能整除3200,但不能整除172800)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;
int isloop(int y) {
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)) return 1;
else return 0;
}
int main() {
int t;
cin >> t;
while (t--) {
int year, n;
cin >> year >> n;
int nyear = year,count=0;
while (count < n) {
if (isloop(nyear)) {
count++;
}
nyear++;
}
cout << nyear-1 << endl;
}
return 0;
}

Author: Zoey

Link: https://zoey1038569979.github.io/2019/08/09/hdoj1076/

Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

< PreviousPost
关于HDOJ 1098题(Ignatius's puzzle) 的理解(C/C++)
NextPost >
关于HDOJ 1058题(Humble Numbers(有且仅有质因数2,3,5,7组成的数)) 的理解(C/C++)
CATALOG
  1. 1. 题目描述
  2. 2. 原题链接
  3. 3. Accepted代码