演算法筆記習題3.4

tenia_發表於2018-07-19
  • 問題 A: 日期差值

 

題目描述

有兩個日期,求兩個日期之間的天數,如果兩個日期是連續的我們規定他們之間的天數為兩天。

輸入

有多組資料,每組資料有兩行,分別表示兩個日期,形式為YYYYMMDD

輸出

每組資料輸出一行,即日期差值

樣例輸入

20130101
20130105

樣例輸出

5

 

#include <stdio.h>
int month[13][2]={{0,0},{31,31},{28,29},{31,31},
{30,30},{31,31},{30,30},{31,31},{31,31},{30,30},
{31,31},{30,30},{31,31}};
bool isleap(int year)
{
	return (year%4==0 && year%100 !=0) || (year%400==0);
}
int main()
{
	int time1,time2,temp;
	int y1,y2,m1,m2,d1,d2,count;
	while (scanf("%d%d",&time1,&time2) != EOF)
	{
		if(time1>time2)
		{
			temp=time1;
			time1=time2;
			time2=temp;
		}
		y1=time1/10000;
		m1=time1%10000/100;
		d1=time1%100;
		y2=time2/10000;
		m2=time2%10000/100;
		d2=time2%100;
		count=1;
		while(y1<y2 || m1<m2 || d1<d2)
		{
			d1 ++;
			if(d1 == month[m1][isleap(y1)] + 1)
			{
				m1 ++;
				d1 = 1;
			}
			if(m1 == 13)
			{
				y1 ++;
				m1 = 1;
			}
			count ++;
		}
		printf("%d\n",count);
	}
	return 0;
 } 

 

  • 問題 B: Day of Week

 

題目描述

We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400.
For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap.
Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.

輸入

There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.

輸出

Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.

樣例輸入

21 December 2012
5 January 2013

樣例輸出

Friday
Saturday
#include <stdio.h>
#include <string.h>
bool isleap(int year)//判斷是否閏年 
{
	return (year%4 == 0 && year%100 != 0) || (year%400==0) ;	
}

//平年和閏年的每月天數 
int month0[13][2]={{0,0},{31,31},{28,29},{31,31},
{30,30},{31,31},{30,30},{31,31},{31,31},{30,30},
{31,31},{30,30},{31,31}};

//月份 
char m[13][15]={"0","January","February","March","April","May",
"June","July","August","September","October","November","December"};

//周 
char week0[8][15]={"0","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
int main()
{
	int year1,day1,month1,year2,month2,day2;
	char month[15];
	const int time0=20121217;//定義之後每次比較後不會改變 
	int time1,time2,count,flag;
	//flag標記在該天之前還是之後,count記錄與該天相差天數 
	while (scanf("%d%s%d",&day2,&month,&year2) != EOF)
	{
		flag=0;
		for(int i=1;i<13;i++)
		{
			if(strcmp(m[i],month) == 0)
			{
				month2 = i;	
			}
		}
		//printf("%d\n%s\n",month1,m[12]);
		time2=year2*10000 + month2*100 + day2 ;
		time1=time0;
		if(time1>time2)//保證從time1數到time2,小到大。 
		{
			flag=1;
			int temp = time2;
			time2 = time1;
			time1 = temp;
		}
		year1=time1/10000;month1=time1%10000/100;day1=time1%100;
		year2=time2/10000;month2=time2%10000/100;day2=time2%100;
		count=1;
		//printf("%d %d %d %d\n",time1,year1,month1,day1);
		//printf("%d %d %d %d\n",time2,year2,month2,day2);
		while(year1<year2 || month1<month2 || day1<day2)
		{
			day1 ++;
			if(day1 == month0[month1][isleap(year1)] +1 )
			{
				month1 ++ ;//超過該月的最後一天 
				day1 = 1;
			}
			if(month1 == 13)
			{
				year1 ++; 
				month1 = 1;
			}
			count ++;
		}
		//printf("%d\n",count);
		//printf("%s %s\n",week0[1],week0[2]);
		if(flag && count%7 == 0)  count = 2; 
		//在該天之前,並且相差天數為7的倍數 
		else if(flag == 0 && count%7 == 0) count = 7;
		else if(flag && (count%7 != 1) && (count%7 != 0))
		{
			count = 9-(count%7);
		}
		else if(count%7 != 0) count = count % 7;
		for(int i=1;i<=7;i++)
		{
			if(count==i)
			{
				printf("%s\n",week0[i]);
			}
		}
	}
	return 0;
 } 

 

  • 問題 C: 列印日期

 

題目描述

給出年分m和一年中的第n天,算出第n天是幾月幾號。

輸入

輸入包括兩個整數y(1<=y<=3000),n(1<=n<=366)。

輸出

可能有多組測試資料,對於每組資料,按 yyyy-mm-dd的格式將輸入中對應的日期列印出來。

樣例輸入

2013 60
2012 300
2011 350
2000 211

樣例輸出

2013-03-01
2012-10-26
2011-12-16
2000-07-29

 

#include <stdio.h>
//判斷是否閏年 
bool isleap(int year)
{
	return (year%4==0 && year%100!=0) || (year%400==0);
 } 
//平年和閏年月份天數 
int Month[13][2]={{0,0},{31,31},{28,29},{31,31},{30,30},{31,31},
{30,30},{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}};


int main ()
{
	int m,n,count;
	int year,month,day;
	while(scanf("%d%d",&m,&n) != EOF)
	{
		day=0;
		month=1;
		for(int i=0;i<n;i++)
		{
			day ++; 
			if(day == Month[month][isleap(m)]+1) 
			{
				month ++;
				day=1;
			}
		}
		printf("%04d-%02d-%02d\n",m,month,day);
	} 
	return 0;
}

 

  • 問題 D: 日期類

 

題目描述

編寫一個日期類,要求按xxxx-xx-xx 的格式輸出日期,實現加一天的操作。

輸入

輸入第一行表示測試用例的個數m,接下來m行每行有3個用空格隔開的整數,分別表示年月日。測試資料不會有閏年。

輸出

輸出m行。按xxxx-xx-xx的格式輸出,表示輸入日期的後一天的日期。

樣例輸入

2
1999 10 20
2001 1 31

樣例輸出

1999-10-21
2001-02-01

 

#include <stdio.h>

int Month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int main()
{
	int m,year,month,day;
	scanf("%d",&m);
	while(m--)
	{
		scanf("%d%d%d",&year,&month,&day);
		day ++;
		if(day == Month[month] + 1)
		{
			month ++;
			day=1;
		}
		printf("%04d-%02d-%02d\n",year,month,day);
	} 
	return 0;
} 

 

  • 問題 E: 日期累加

 

題目描述

設計一個程式能計算一個日期加上若干天后是什麼日期。

輸入

輸入第一行表示樣例個數m,接下來m行每行四個整數分別表示年月日和累加的天數。

輸出

輸出m行,每行按yyyy-mm-dd的個數輸出。

樣例輸入

1
2008 2 3 100

樣例輸出

2008-05-13

 

#include <stdio.h>
bool isleap(int year)
{
	return (year%4==0 && year%100 !=0) || (year%400 == 0);
}

int Month[13][2]={{0,0},{31,31},{28,29},{31,31},{30,30},{31,31},
{30,30},{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}};

int main()
{
	int m,year,month,day,n;
	scanf("%d",&m);
	while (m--)
	{
		scanf("%d%d%d%d",&year,&month,&day,&n);
		for(int i=0;i<n;i++)
		{
			day ++;
			if(day == Month[month][isleap(year)] + 1)
			{
				month ++;
				day = 1;
			}
			if(month == 13)
			{
				year ++;
				month=1;
			}
		}
		printf("%04d-%02d-%02d\n",year,month,day);
	}
	return 0;
}

 

相關文章