首页 > ACM题库 > 九度OJ > 九度-1186-打印日期[解题代码]
2013
12-13

九度-1186-打印日期[解题代码]

题目来源:2003-2005年华中科技大学计算机研究生机试真题

题目描述:

给出年分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

cpp 代码如下:
#include <cstdlib>
#include <iostream>
#include <stdio.h>

using namespace std;
	int month1[] = {31,29,31,30,31,30,31,31,30,31,30,31};
	int *arr1 = new int[12];
	int *arr2 = new int[12];

   int isRun(int year){
		if( year%400==0 || (year%4 ==0 && year%100!=0))
			return 1;
		return -1;
	}	

int main(int argc, char *argv[])
{
    	int sum = 0;
		for(int i=0; i<12; i++){
			sum += month1[i];
			arr1[i] = sum;
		}
		arr2[0] = 31;
		for(int i=1; i<12; i++)
			arr2[i] = arr1[i]-1;
	
    int year,day;
    while(scanf("%d %d",&year,&day) != EOF){
                    
                    int month = 1;
			int date = day;    
             if(day > 31){
				if(isRun(year) == 1){
					for(int i=0; i<11; i++){
						if(day>arr1[i] && day<=arr1[i+1]){
							month = i+2;
							date = day - arr1[i];
							break;
						}
					}
				}else{
					for(int i=0; i<11; i++){
						if(day>arr2[i] && day<=arr2[i+1]){
							month = i+2;
							date = day - arr2[i];
							break;
						}
					}
				}
			}              
         printf("%04d-%02d-%02d\n",year,month,date);               
   }
    
    return EXIT_SUCCESS;
}

/**************************************************************
	Problem: 1186
	User: coder
	Language: C++
	Result: Accepted
	Time:20 ms
	Memory:1512 kb
****************************************************************/


  1. Good task for the group. Hold it up for every yeara??s winner. This is a excellent oppotunity for a lot more enhancement. Indeed, obtaining far better and much better is constantly the crucial. Just like my pal suggests on the truth about ab muscles, he just keeps obtaining much better.

  2. a是根先忽略掉,递归子树。剩下前缀bejkcfghid和后缀jkebfghicd,分拆的原则的是每个子树前缀和后缀的节点个数是一样的,根节点出现在前缀的第一个,后缀的最后一个。根节点b出现后缀的第四个位置,则第一部分为四个节点,前缀bejk,后缀jkeb,剩下的c出现在后缀的倒数第2个,就划分为cfghi和 fghic,第3部分就为c、c