孩子(我)的奇葩代碼Show,大佬勿噴。
成都創(chuàng)新互聯(lián)成立于2013年,我們提供高端成都網(wǎng)站建設(shè)、網(wǎng)站制作公司、網(wǎng)站設(shè)計(jì)、網(wǎng)站定制、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、成都小程序開(kāi)發(fā)、微信公眾號(hào)開(kāi)發(fā)、成都網(wǎng)站營(yíng)銷(xiāo)服務(wù),提供專(zhuān)業(yè)營(yíng)銷(xiāo)思路、內(nèi)容策劃、視覺(jué)設(shè)計(jì)、程序開(kāi)發(fā)來(lái)完成項(xiàng)目落地,為辦公窗簾企業(yè)提供源源不斷的流量和訂單咨詢(xún)。問(wèn)題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".樣例 “妙”答
本身不算什么難題,我只是想整個(gè)活。
總體來(lái)說(shuō)就是先算年,再在函數(shù)中算月和日,星期幾過(guò)于好算,模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) //計(jì)算月和日
{
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代碼如下(其實(shí)沒(méi)有什么區(qū)別):
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();
}
}
順便一提,這個(gè)代碼在 vjudge 里面WA,我就是說(shuō)一整個(gè)迷惑住了。
u1s1,做這道題只是因?yàn)楹镁脹](méi)做編程題了,做一個(gè)練練手,順便復(fù)習(xí)一下Java 的基礎(chǔ)語(yǔ)法 ,果然還是菜的一批。
以上內(nèi)容僅供娛樂(lè),水平不高,大家見(jiàn)笑。
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧