天天看點

藍橋杯2013年省賽c/c++A組題1(高斯日記)

題目标題: 高斯日記

大數學家高斯有個好習慣:無論如何都要記日記。他的日記有個與衆不同的地方,他從不注明年月日,而是用一個整數代替,比如:4210

後來人們知道,那個整數就是日期,它表示那一天是高斯出生後的第幾天。這或許也是個好習慣,它時時刻刻提醒着主人:日子又過去一天,還有多少時光可以用于浪費呢?

高斯出生于:1777年4月30日。在高斯發現的一個重要定理的日記上标注着:5343,是以可算出那天是:1791年12月15日。

高斯獲得博士學位的那天日記上标着:8113 , 請你算出高斯獲得博士學位的年月日。

送出答案的格式是:yyyy-mm-dd, 例如:1980-03-21

請嚴格按照格式,通過浏覽器送出答案。

注意:隻送出這個日期,不要寫其它附加内容,比如:說明性的文字。

參考答案:

1799-07-16

注意: 根據題目給的5343是1791年12月15日,得出生的那天過去了也算是一天的。

解題C語言代碼如下:

/***********************************************************************************************
 ***                 藍橋杯2013年省賽c/c++A組題1(高斯日記)                                 ***
 ***********************************************************************************************
 *                                                                                             *
 *                 Project Name : -----------------                                            *
 *                                                                                             *
 *                    File Name : T1高斯日記.c                                                 *
 *                                                                                                                                                                                   *
 *                   Start Date : 2020-08-05                                                   *
 *                                                                                             *
 *                  Last Update : 2020-08-05 [JYH]                                             *
 *                                                                                             *
 *---------------------------------------------------------------------------------------------*
 * Functions:  																				   * 
 *   main -- 主函數,日期計算模拟器															   *
 *   is_leap_year -- 判斷某年是否為閏年                                                        *
 *   each_month_date_count -- 計算某年某月的天數                                               *
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#include <stdio.h>
#define NUM 8113  // 日記中的數字 

int is_leap_year(int year);
int each_month_date_count(int year, int month);


/***********************************************************************************************
 * main -- 主函數,日期計算模拟器	                                                           *
 *                                                                                             *
 *    計算高斯日記中某個數字對應的日期                                                         *
 *                                                                                             *
 * INPUT:   none                                                                               *
 *                                                                                             *
 * OUTPUT:  none								                                               *
 *                                                                                             *
 * WARNINGS:   none                                                                            *
 *                                                                                             *
 * HISTORY:                                                                                    *
 *   2020-08-05 JYH : Created.                                                                 *
 *=============================================================================================*/
int main()
{
	int year=1777, month=4, date=30;
	int num = 1;
	while(num<NUM){
		if(month==12&&date==31){
			year++;
			month = 1;
			date = 1;
		}
		else if(date==each_month_date_count(year,month)){
			month++;
			date = 1;
		}
		else{
			date++;
		}
		num++;
	}
	printf("%d-%d-%d",year,month,date);
}


/***********************************************************************************************
 * is_leap_year -- 判斷某年是否為閏年		                                                   *
 *                                                                                             *
 *    none							                                                           *
 *                                                                                             *
 * INPUT:   year     -- 年份      															   *
 *                                                                                             *
 * OUTPUT:  1        -- 是閏年      			                                               *
 *          0        -- 不是閏年															   * 
 *                                                                                             *
 * WARNINGS:   none                                                                            *
 *                                                                                             *
 * HISTORY:                                                                                    *
 *   2020-08-05 JYH : Created.                                                                 *
 *=============================================================================================*/
int is_leap_year(int year)
{
	int s;
	if(year%4!=0){
		s = 0;
	}
	else{
		if(year%100==0){
			if(year%400==0){
				s = 1;
			}
			else{
				s = 0;
			}
		}
		else{
			s = 1;
		}
	}
	return s;
}

 
 /***********************************************************************************************
  * each_month_date_count -- 計算某年某月的天數                                                 *
  *                                                                                             *
  *    none							                                                            *
  *                                                                                             *
  * INPUT:   year     -- 年份      															    *
  *                                                                                             *
  *          month    -- 月份                                                                   *
  *                                                                                             *
  * OUTPUT:  對應某一年某一月的天數   			                                                *
  *                                                                                             *
  * WARNINGS:   none                                                                            *
  *                                                                                             *
  * HISTORY:                                                                                    *
  *   2020-08-05 JYH : Created.                                                                 *
  *=============================================================================================*/
int each_month_date_count(int year, int month)
{
	int days;
	switch(month){
		case 2:{
			if(is_leap_year(year)){
				days = 29;
			}
			else{
				days =28;
			}
			break;
		}
		case 4:
		case 6:
		case 9:
		case 11:{
			days = 30;
			break;
		}
		default: days = 31;
	}
	return days;
}
           

源檔案可前往下面網址下載下傳:

高斯日記下載下傳通道

繼續閱讀