找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 479|回复: 4

高斯日记

[复制链接]

205

主题

173

回帖

6925

积分

论坛元老

积分
6925
发表于 2013-5-27 08:52:01 | 显示全部楼层 |阅读模式
[blockquote]
大数学家高斯有个好习惯:无论如何都要记日记。
他的日记有个与众不同的地方,他从不注明年月日,而是用一个整数代替,比如:4210,后来人们知道,那个整数就是日期,它表示那一天是高斯出生后的第几天。
这或许也是个好习惯,它时时刻刻提醒着主人:日子又过去一天,还有多少时光可以用于浪费呢?
高斯出生于:1777年4月30日。在高斯发现的一个重要定理的日记上标注着:5343,因此可算出那天是:1791年12月15日。
高斯获得博士学位的那天日记上标着:8113   请你算出高斯获得博士学位的年月日。[/blockquote][blockquote]
提交答案的格式是:yyyy-mm-dd, 例如:1980-03-21[/blockquote]

205

主题

173

回帖

6925

积分

论坛元老

积分
6925
 楼主| 发表于 2013-5-27 08:52:43 | 显示全部楼层
/**************************************************************
*     Copyright (C) 2006-2013 All rights reserved.
*       @Version: 1.0
*       @Created: 2013-05-26 19:01
*        @Author: chin - qin@126.com
*   @Description:
*
*       @History:
**************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
/*高斯的出生日期,此处可以改为你自己的生日来测试*/
#define BIRTH_YEAR  1777
#define BIRTH_MONTH 4
#define BIRTH_DAY 30

static int leap_month[] = {31,29,31,30,31,30,31,31,30,31,30,31};
static int non_leap_month[] = {31,28,31,30,31,30,31,31,30,31,30,31};

/**
* @brief is_leap_year
* 判断是否为闰年
* @Param: year
*
* Returns: 是闰年返回1,否则返回0
*/
int is_leap_year(int year)
{
    int leap= (year % 400 == 0) ||
        ((year % 4 == 0)&&(year % 100 != 0)) ;
    return leap;
}
/**
* @brief get_gauss_date
* 用户输入高斯日期
* Returns:
*/
unsigned long get_gauss_date()
{
    unsigned long date;
    char s[32];

    while(1)
    {
        printf("lease input a number as the Gauss date[1-37527]:");
        fgets(s, 32, stdin);
        date = atol(s);
        if(date != 0)
            break;
    }
    return date;
}
/**
* @brief still_left_time
* 计算XXXX年还剩下多少天
* @Param: year
* @Param: month
* @Param: day
*
* Returns:
*/
int still_left_time(int year, int month, int day)
{
    int *p, days = 0, i;

    if(is_leap_year(year))
        p = leap_month;
    else
        p = non_leap_month;
    days = p[month-1] - day;
    for(i=month; i< 12; i++)
        days += p;

    return days + 1;
}

/**
* @brief format_date
* 将距离当前日期date天转换成具体的日期格式
* @Param: date
* @Param: year 当前日期年
* @Param: month 当前日期月
* @Param: day 当前日期日
*
* Returns:
*/
char *format_date(unsigned long date, int year, int month, int day)
{
    int i = 0, *p;
    unsigned long count = 0;
    static char date_str[32];

    if(is_leap_year(year))
        p = leap_month;
    else
        p = non_leap_month;
    if(month == BIRTH_MONTH && day == BIRTH_DAY)
    {
        count = p[month-1] - day + 1;

        if(count >= date)
        {
            sprintf(date_str, "%04d-%02d-%02d",year, month, day + (int)date - 1);
            return date_str;
        }
    }
    for(i=month; i<12; i++)
    {
        count += p;
        if(count >= date)
        {
            month = i + 1;
            day = date - ( count - p );
            sprintf(date_str, "%04d-%02d-%02d",year, month, day);
            break;
        }
    }
    return date_str;
}

/**
* @brief caculate_gauss_date
* 计算高斯日期函数
* @Param: date  高斯日期
* @Param: date_str  格式化后的日期
*
* Returns:
*/
char *caculate_gauss_date(unsigned long date, char *date_str)
{
    unsigned long count1 = 0;
    int left_time;
    int days_of_year;
    int year = BIRTH_YEAR, month = BIRTH_MONTH, day = BIRTH_DAY;
    int i = 0;

    if(date_str)
    {
        /*计算xxxx年这一年还剩下多少天*/
        left_time = still_left_time(year, month, day);
        if( date <= left_time )
        {
            return strcpy(date_str, format_date(date, year, month, day ) );
        }
        date = date - left_time;
        year = BIRTH_YEAR;
        month = 12;
        day = 31;
        /*从1788-01-01开始计时*/
        while(1)
        {
            i++;
            days_of_year = is_leap_year( BIRTH_YEAR + i ) > 0 ? 366 : 365;
            count1 += days_of_year;
            if(count1 >= date)
            {
                year = BIRTH_YEAR + i;
                date = date - (count1 - days_of_year);
                break;
            }
        }
        /*不足一年数的情况,格式化成具体日期*/
        strcpy(date_str, format_date(date, year, 0, day ) );
    }

    return date_str;
}
int main()
{
    unsigned long date;
    char date_str[32];

    date = get_gauss_date();
    printf("Gauss log date is:%lu\n",date);

    caculate_gauss_date(date,date_str);

    printf("Format date is:%s\n",date_str);

    return 0;
}

1793

主题

457

回帖

0

积分

管理员

积分
0
发表于 2013-5-27 09:19:26 | 显示全部楼层
哦,很有意思,我得算算我多少天了

205

主题

173

回帖

6925

积分

论坛元老

积分
6925
 楼主| 发表于 2013-5-27 12:33:34 | 显示全部楼层

回 啊冲 的帖子

啊冲:哦,很有意思,我得算算我多少天了&#160;(2013-05-27 09:19)&#160;
呵呵,我活了8781天了

14

主题

65

回帖

0

积分

新手上路

积分
0
发表于 2013-5-27 22:36:04 | 显示全部楼层
[attachment=11]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

果子博客
扫码关注微信公众号

Archiver|手机版|小黑屋|风叶林

GMT+8, 2026-2-1 22:37 , Processed in 0.243681 second(s), 20 queries .

Powered by 风叶林

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表