HDOJ 1076 An Easy Task(闰年计算)

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.

题意就是:输入一个年份year和一个n,求这个year之后的第n个闰年,
如果year是闰年,则算第一个闰年。

直接暴力做,并没有超时。

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-->0){
            int year = sc.nextInt();
            int n = sc.nextInt();
            int nyear = 0;
            if((year%4==0&&year%100!=0)|(year%400==0)){
                nyear=1;
            }//如果year是闰年,nyear就加一
            while(nyear<n){//当nyear小于n时,就循环
                year++;
                if((year%4==0&&year%100!=0)|(year%400==0)){
                    nyear++;
                }//year是闰年,nyear就加加
            }
            System.out.println(year);
        }
    }

}
时间: 2024-09-20 12:27:59

HDOJ 1076 An Easy Task(闰年计算)的相关文章

HDOJ 1040 As Easy As A+B

Problem Description These days, I am thinking about a question, how can I get a problem as easy as A+B? It is fairly difficulty to do such a thing. Of course, I got it after many waking nights. Give you some integers, your task is to sort these numbe

HDOJ 1339 A Simple Task(简单数学题,暴力)

Problem Description Given a positive integer n and the odd integer o and the nonnegative integer p such that n = o2^p. Example For n = 24, o = 3 and p = 3. Task Write a program which for each data set: reads a positive integer n, computes the odd int

HDOJ 2055 An easy problem

Problem Description we define f(A) = 1, f(a) = -1, f(B) = 2, f(b) = -2, - f(Z) = 26, f(z) = -26; Give you a letter x and a number y , you should output the result of y+f(x). Input On the first line, contains a number T.then T lines follow, each line

java和javascript日期校验和闰年问题分析和解决方案

1.闰年的介绍 地球绕太阳运行周期为365天5小时48分46秒(合365.24219天)即一回归年.公历的平年只有365日,比回归年短约0.2422 日,所余下的时间约为四年累计一天,故四年于2月加1天,使当年的历年长度为366日,这一年就为闰年. 现行公历中每400年有97个闰年.按照每四年一个闰年计算,平均每年就要多算出0.0078天,这样经过四百年就会多算出大约3天来,因此,每四百年中要减少三个闰年.所以规定,公历年份是整百数的,必须是400的倍数的才是闰年,不是400的倍数的,虽然是4的

c#基于task的异步模式的定义及实现

基于Task的异步模式的定义 命名,参数和返回类型 在TAP(Task-based Asynchronous Pattern)中的异步操作的启动和完成是通过一个单独的方法来表现的,因此只有一个方法要命名.这与IAsyncResult模式或者APM(Asynchronous Programming Model,异步编程模型)模式形成对比,后者必须要有开始方法名和结束方法名:还与基于事件(event-based)的异步模式(EAP)不同,它们要求方法名以Async为后缀,而且要求一个或多个事件,事件

UVa 270 / POJ 1118 Lining Up:计算几何

270 - Lining Up Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=206 http://poj.org/problem?id=1118 ``How am I ever going to solve this problem?" sai

Spark配置参数

以下是整理的Spark中的一些配置参数,官方文档请参考Spark Configuration. Spark提供三个位置用来配置系统: Spark属性:控制大部分的应用程序参数,可以用SparkConf对象或者Java系统属性设置 环境变量:可以通过每个节点的 conf/spark-env.sh脚本设置.例如IP地址.端口等信息 日志配置:可以通过log4j.properties配置 Spark属性 Spark属性控制大部分的应用程序设置,并且为每个应用程序分别配置它.这些属性可以直接在Spark

动态广告管理程序制作例子

程序|动态|广告 By Wayne Berry This IssueMany sites that are content specific depend on banner advertisement for revenue. Such is the case for 15 Seconds. A banner is displayed at the top of the page for every page viewed. Clients usually buy a set number o

UVa 10763:Foreign Exchange

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1704 原题: Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very succes