网站建设资讯

NEWS

网站建设资讯

[代码Show]第一期-Calendar-创新互联

[训练营] A - Calendar

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

成都创新互联成立于2013年,我们提供高端成都网站建设网站制作公司网站设计、网站定制、营销型网站建设成都小程序开发、微信公众号开发、成都网站营销服务,提供专业营销思路、内容策划、视觉设计、程序开发来完成项目落地,为办公窗帘企业提供源源不断的流量和订单咨询。问题
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#include#includeusing 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 的基础语法 ,果然还是菜的一批。

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

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网页名称:[代码Show]第一期-Calendar-创新互联
网站URL:http://cdweb.net/article/dgjjcj.html