zoukankan      html  css  js  c++  java
  • 题目1186:打印日期

    题目描写叙述:

    给出年分m和一年中的第n天。算出第n天是几月几号。

    输入:

    输入包含两个整数y(1<=y<=3000)。n(1<=n<=366)。

    输出:

    可能有多组測试数据。对于每组数据,
    按 yyyy-mm-dd的格式将输入中相应的日期打印出来。

    例子输入:
    2000 3
    2000 31
    2000 40
    2000 60
    2000 61
    2001 60
    例子输出:
    2000-01-03
    2000-01-31
    2000-02-09
    2000-02-29
    2000-03-01
    2001-03-01


    C++代码:

    #include <iostream>
        #include <stdio.h>
        using namespace std;
        int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
        int b[12]={31,29,31,30,31,30,31,31,30,31,30,31};
        int main()
        {
                int y,d;
                while(cin>>y>>d){
                        int i;
                        if((y%4==0&&y%100!=0) || y%400==0){
                                for(i = 0;i<12&&d>0;i++){
                                        d-=b[i];
                                }
                                d+=b[--i];
                        }
                        else{
                                for(i = 0;i<12&&d>0;i++){
                                                d-=a[i];
                                }
                                d+=a[--i];
                        }
                        printf("%04d-%02d-%02d
    ",y,i+1,d);
                }
        }
    /**************************************************************
        Problem: 1186
        User: Carvin
        Language: C++
        Result: Accepted
        Time:140 ms
        Memory:1520 kb
    ****************************************************************/

    Java代码:

    package oj1186;
    
    import java.util.Scanner;
    
    public class oj1186{
    	public static void main(String args[]){
    		int years, days;
    		int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
    		Scanner in =new Scanner(System.in);
    		while(in.hasNext()){
    			String outMonths = null,outDays = null,outYears=null;
    			years=in.nextInt();
    			days=in.nextInt();
    			if(years<1||years>3000||days<1||days>367)
    				continue;
    			int months=1;
    			if(years%400==0||(years%4==0&&years%100!=0))
    				month[1]=29;
    			for(int i=0;i<12;i++){
    				if(days-month[i]>0){
    					months++;
    					days-=month[i];
    				}
    				else
    					break;
    			}//for
    			if(months<10)
    				outMonths="0"+months;
    			else
    				outMonths=""+months;
    			if(days<10)
    				outDays="0"+days;
    			else
    				outDays=""+days;
    			if(years<1000&&years>=100)
    				System.out.println("0"+years+"-"+outMonths+"-"+outDays);
    			else if(years>=1000)
    				System.out.println(+years+"-"+outMonths+"-"+outDays);
    			else if(years<100&&years>=10)
    				System.out.println("00"+years+"-"+outMonths+"-"+outDays);
    			else if(years<10)
    				System.out.println("000"+years+"-"+outMonths+"-"+outDays);
    			}
    		}
    }



  • 相关阅读:
    10. Regular Expression Matching
    9. Palindrome Number
    6. ZigZag Conversion
    5. Longest Palindromic Substring
    4. Median of Two Sorted Arrays
    3. Longest Substring Without Repeating Characters
    2. Add Two Numbers
    链式表的按序号查找
    可持久化线段树——区间更新hdu4348
    主席树——树链上第k大spoj COT
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/7402427.html
Copyright © 2011-2022 走看看