[代码Show]第一期- Calendar

[训练营] A - Calendar

孩子(我)的奇葩代码Show,大佬勿喷。

问题

A calendar is a system for measuring time, from hours and minutes, to months and days, and finally to years and centuries. The terms of hour, day, month, year and century are all units of time measurements of a calender system.
According to the Gregorian calendar, which is the civil calendar in use today, years evenly divisible by 4 are leap years, with the exception of centurial years that are not evenly divisible by 400. Therefore, the years 1700, 1800, 1900 and 2100 are not leap years, but 1600, 2000, and 2400 are leap years.
Given the number of days that have elapsed since January 1, 2000 A.D, your mission is to find the date and the day of the week.

输入

The input consists of lines each containing a positive integer, which is the number of days that have elapsed since January 1, 2000 A.D. The last line contains an integer −1, which should not be processed.
You may assume that the resulting date won’t be after the year 9999.

输出

For each test case, output one line containing the date and the day of the week in the format of "YYYY-MM-DD DayOfWeek", where " DayOfWeek " must be one of "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" and "Saturday".

样例

“妙”答

本身不算什么难题,我只是想整个活。

总体来说就是先算年,再在函数中算月和日,星期几过于好算,模7就完了。

C++代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

char weekday[7][20] = {"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};
int monthday[2][13] =  {{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};

int* dayday(int ifleap,int days)	//计算月和日
{
    int *ret = (int*)calloc(2,sizeof(int)) ;
    ret[0] = 1;
    for(;;days-=monthday[ifleap][ret[0]++])
    {
        if(days <= monthday[ifleap][ret[0]]){
            ret[1] = days;
            return ret;
        }
    }
} 

int main()
{
    long n=0,days2000=365,days400y = 365*400+100-3,days100y = 365*100+25-1,days4y = 365*4+1;
    int year,*ref;
    while(scanf("%ld",&n)*n+1)
    {
        long nn = n;
        if(n<=365){
            year = 2000;
            ref = dayday(1,n+1);
            printf("%d-%02d-%02d %s\n",year,ref[0],ref[1],weekday[nn%7]);
            continue;
        }
        year = (n-=365)/days400y*400 + (n%=days400y)/days100y*100 + (n%=days100y)/days4y*4 + (n%=days4y)/365 + 2001;
        ref = dayday((!(year%400) ||(year%100 && !(year%4))),(n%=365));
        printf("%d-%02d-%02d %s\n",year,ref[0],ref[1],weekday[nn%7]);
    }
    return 0;
}

Java代码如下(其实没有什么区别):

import java.util.Scanner;

public class Main {
    static String weekday[] = {"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};
    static int monthday[][] =  {{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
    static int ret[] = {1,0};
    private static void dayday(Boolean ifleap,int days){
        ret[0] = 1;
        for(;;days-=monthday[ifleap?1:0][ret[0]++])
        {
            if(days <= monthday[ifleap?1:0][ret[0]]){
                ret[1] = days;
                return ;
            }
        }
    }
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int n=0,days400y = 365*400+100-3,days100y = 365*100+25-1,days4y = 365*4+1;
        int year;
        while((n = scan.nextInt())!=-1)
        {
            int nn = n;
            if(n<=365){
                year = 2000;
                dayday(true,n+1);
                System.out.printf("%d-%02d-%02d %s\n",year,ret[0],ret[1],weekday[nn%7]);
                continue;
            }
            year = (n-=365)/days400y*400 + (n%=days400y)/days100y*100 + (n%=days100y)/days4y*4 + (n%=days4y)/365 + 2001;
            dayday(((year%400==0) ||((year%100!=0) && (year%4==0))),(n%=365));
            System.out.printf("%d-%02d-%02d %s\n",year,ret[0],ret[1],weekday[nn%7]);
        }
        scan.close();
    }
}

顺便一提,这个代码在 vjudge 里面WA,我就是说一整个迷惑住了。

u1s1,做这道题只是因为好久没做编程题了,做一个练练手,顺便复习一下Java 的基础语法 ,果然还是菜的一批。

以上内容仅供娱乐,水平不高,大家见笑。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值