现在的位置: 首页 > 综合 > 正文

hdu 4515 小Q系列故事——世界上最遥远的距离

2018年11月09日 ⁄ 综合 ⁄ 共 1708字 ⁄ 字号 评论关闭

搞IT还是该会一点java

代码:

import java.io.*;
import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        int t = cin.nextInt();
        while ( t -- > 0 )
        {
            int d = cin.nextInt();
            Calendar calendar = Calendar.getInstance();

            calendar.add(Calendar.DAY_OF_MONTH, d+1);
            int year = calendar.get( Calendar.YEAR );
            int month = 1 + calendar.get( Calendar.MONTH );
            int day = calendar.get( Calendar.DAY_OF_MONTH );
            
            System.out.print(year+"/");
            if ( month < 10 )
                System.out.print("0");
            System.out.print(month+"/");
            if ( day < 10 )
                System.out.print("0");
            System.out.print(day+" ");

            Calendar calendar2 = Calendar.getInstance();

            calendar2.add(Calendar.DAY_OF_MONTH, 1-d);
            year = calendar2.get( Calendar.YEAR );
            month = 1+ calendar2.get( Calendar.MONTH );
            day = calendar2.get( Calendar.DAY_OF_MONTH );
            
            System.out.print(year+"/");
            if ( month < 10 )
                System.out.print("0");
            System.out.print(month+"/");
            if ( day < 10 )
                System.out.print("0");
            System.out.println(day);
        }
    }
}

C/C++代码:

#include<stdio.h>


int judge(int n){return n%400==0||(n%4==0&&n%100!=0);}

int mont[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int init(int n)
{
	int ans=0,i;
	for(i=1;i<=12;i++)
		mont[i]+=mont[i-1];
	for(i=1601;i<n;i++)
	{
		if(judge(i))
			ans+=366;
		else ans+=365;
	}
	return ans;
}
int calyear(int &n)
{
	int i,ans=1601,flag;
	for(i=1;;i++){
		flag=judge(ans);
		if(n>365+flag) n-=365+flag;
		else break;
		ans++;
	}
	return ans;
}
void calmont(int is,int &n,int& m)
{
	int i;
	if(n<=mont[1]) m=1;
	else if(n<=mont[2]+is){ m=2 ;n-=31;}
	else {
		for(i=3;i<=12;i++)
			if(n<=mont[i]+is)
				break;
		m=i;
		n-=mont[i-1]+is;
	}
}
int main()
{
	int c,n,cas,y,m,d,t;
	c=init(2013)+31+28+24;
	scanf("%d",&cas);
	while(cas--)
	{
		scanf("%d",&n);t=n;n+=c;
		y=calyear(n);
		calmont(y%400==0||(y%4==0&&y%100!=0),n,m);
		d=n;
		printf("%04d/%02d/%02d ",y,m,d);
		n=c-t;
		y=calyear(n);
		calmont(y%400==0||(y%4==0&&y%100!=0),n,m);
		d=n;
		printf("%04d/%02d/%02d\n",y,m,d);
	}
	return 0;
}

抱歉!评论已关闭.