HDU 1076 An Easy Task

对于闰年判断的题已经做过很多,但是这个题令我印象深刻。

刚开始,我的代码是

 1 #include<stdio.h>
 2 int is_leap(int n)
 3 {
 4     if((n%4==0&&n%100!=0)||n%400==0)
 5         return 1;
 6     else
 7         return 0;
 8 }
 9 int main()
10 {
11     int m;
12     scanf("%d",&m);
13     int i;
14     int a,b;
15     for(i=1;i<=m;i++)
16     {
17         scanf("%d%d",&a,&b);
18         if(is_leap(a)==1)
19             a+=4*(b-1);
20         else
21         {
22             int j;
23             for(;;a++)
24             {
25                 if(is_leap(a)==1)
26                     break;
27             }
28             a+=4*b;
29         }
30         printf("%d\n",a);
31
32     }
33     return 0;
34 }

样例输入是

3

2005 25

1855 12

2004 10000

样例输出最后一组数据出错,说明这题的逻辑有错误。

正确的代码:

#include<stdio.h>
int main()
{
    int m;
    scanf("%d",&m);
    int i;
    int a,b;
    for(i=1;i<=m;i++)
    {
        int count = 0;
        scanf("%d%d",&a,&b);
        while(count<b)
        {
            if((a%4==0&&a%100!=0)||a%400==0)
            {
                count++;
                a++;
            }
            else
                a++;
        }
        printf("%d\n",a-1);
    }
}

这样即使在循环中遇到闰年,也不会出错了。

时间: 2024-08-06 11:54:17

HDU 1076 An Easy Task的相关文章

杭电OJ(HDU)-ACMSteps-Chapter Two-《An Easy Task》《Buildings》《decimal system》《Vowel Counting》

http://acm.hdu.edu.cn/game/entry/problem/list.php?chapterid=1§ionid=2 1.2.5 #include<stdio.h> /* 题意:找闰年. if((i%4==0 && i%100!=0) || i%400==0)count++; 3 2005 25 1855 12 2004 10000 2108 1904 43236 */ int main() { int t,y,n; int i,count=0; whil

(HDU)1076 --An Easy Task(简单任务)

题目链接:http://vjudge.net/problem/HDU-1076 数据规模小,直接枚举过去. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 int main() 7 { 8 int t,y,n,i,ans; 9 scanf("%d",&t); 10 while(t--) 11 { 12 scanf

HDOJ 1076 An Easy Task

[题意]:输入两个数,Y和N.输出从Y(如果Y是闰年则包含Y)开始的第N个闰年. [代码:AC] #include <iostream> #include <iomanip> #include <cstring> #include <cstdlib> #include <cstdio> using namespace std; int isLeapYear(int year) { if ((year%4 == 0 && year%

哈理工2015暑假训练赛BNU16488 Easy Task(简单题)

A - Easy Task Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu SubmitStatusPracticeZOJ 2969 Description Calculating the derivation of a polynomial is an easy task. Given a function f(x) , we use (f(x))' to denote its derivatio

HDU-1076-An Easy Task(Debian下水题测试.....)

An Easy Task Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17062    Accepted Submission(s): 10902 Problem Description Ignatius was born in a leap year, so he want to know when he could hold h

[ACM] ZOJ 3844 Easy Task (模拟+哈希)

Easy Task Time Limit: 2 Seconds      Memory Limit: 65536 KB You are given n integers. Your task is very easy. You should find the maximum integer a and the minimum integer b among these n integers. And then you should replace both a and bwith a-b. Yo

ZOJ 2969: Easy Task

ZOJ 2969: Easy Task 1 ///@date 2017-02-07 2 ///@author Sycamore, ZJNU 3 #include <iostream> 4 using namespace std; 5 int c[1001]; 6 int main() 7 { 8 int T; 9 cin >> T; 10 while (T--) 11 { 12 int N; 13 cin >> N; 14 for (int i = N; i >=

HDU-------An Easy Task

An Easy Task Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4088 Accepted Submission(s): 2327   Problem Description Ignatius was born in a leap year, so he want to know when he could hold his bir

2016 省热身赛 Easy Task

Easy Task Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description You are given n integers. Your task is very easy. You should find the maximum integer a and the minimum integer b among these nintegers. And then you sho