任务描述

本关任务:设计一个程序用于向后推算指定日期经过n天后的具体日期。

编程要求

根据提示,在右侧编辑器补充代码,输入为长度为8的字符串str和一个正整数n,str前四位表示年份,后四位表示月和日。当推算出的年份大于4位数时,输出“out of limit!”,否则输出8位的具体日期。

测试说明

平台会对你编写的代码进行测试:

测试输入: 00250709 60000 预期输出: 01891017

下面这一代码版本有2个测试集通不过

测试输入:73860425 72431 预期输出:75840816 实际输出:75840815

测试输入:06250615 1099666 预期输出:36360327 实际输出:36360326

//有问题的代码
#include <iostream>
#include<iomanip>
using namespace std;
int a[2][12] = { { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };
int b[2] = { 365,366 };
int judge(int a)
{if ((a % 4 == 0 && a % 100 != 0) || a % 400 == 0) return 1;else return 0;
}
//返回从1年1月1日开始(包括当天)到输入天数(不包括当天)所经历的天数
int days_1(int year, int month, int day)
{int sum = 0;for (int i = year; i > 1; i--){sum += b[judge(i)];}for (int j = month; j > 1; j--){sum += a[judge(year)][j - 1];}sum += day - 1;return sum;
}
//输入n(n大于0),则返回从1年1月1日(包括当天)开始经过了n天后的日期(不包括当天)
void days_2(int sum, int& year, int& month, int& day)
{for (year = 1; sum >= b[judge(year)]; year++){sum -= b[judge(year)];}for (month = 0; sum >= a[judge(year)][month]; month++){sum -= a[judge(year)][month];}month++;day = sum + 1;return;
}
int main()
{int a, n, year, month, day;cin >> a >> n;year = a / 10000;month = a %10000/100;day = a % 100;//cout << year <<" "<< month<<" " << day;//for (i = 0; i < 8; i++)//    cin >> a[i];//for (i = 0; i < 8; i++)//{//    d =d+ (a[i] - 48);//}n += days_1(year, month, day);days_2(n, year, month, day);if (year / 10000 > 0) cout << "out of limit!";else cout << setfill('0') << setw(4) << year << setfill('0') << setw(2) << month << setfill('0') << setw(2) << day << endl;//cout << year << "." << month << "." << day << endl;return 0;
}

下面是2种错得很离谱的代码,菜鸟落泪,有空请看看。

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;int main()
{   int m_year = 0;for (int i=0; i<4; i++) {int c = cin.get(); m_year += c * pow(10, 3-i);}int m_month = 0;for (int j=0; j<2; j++) {int c = cin.get();m_month += c * pow(10, 1-j);}int m_day = 0;for (int j=0; j<2; j++) {int c = cin.get();m_day += c * pow(10, 1-j);}cin.get();int n;cin >> n;class Date {int month_days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};int year;int month;int day;public:Date(int y=0, int m=0, int d=0) {year = y;month = m;day = d;};static int getMonthDay(int y, int n); //返回y年n月的天数bool leapYear(int y) const;int countLeapYear(int y) const;};int Date::getMonthDay(int y, int n) {return month_days[n-1] + (n==2 && (y%400==0 || (y%4==0&&y%100!=0)));}bool Date::leapYear(int y) const {return y%400==0 || (y%400==0 || (y%4==0&&y%100!=0));}int Date::countLeapYear(int y) const {if (year == y) return leapYear(y);int begin = year;int end = y - 1;if (year > y) {begin = year;end = y - 1;}while (!leapYear(begin)) {++begin;}while (!leapYear(end)) {--end;}int tmp = begin;int count = 0;if (begin <= end) {while (tmp <= end && tmp % 100) {++tmp;}for (; tmp<=end; !leapYear(tmp) ? ++count, tmp += 100);return (end-begin) / 4 + 1 -count;}return 0;}Date Date::operator+(size_t n) const {Date tmp = *this;size_t count = 0;for (int i=1; i<tmp.month; i++) {count += getMonthDay(tmp.year, i);}count += tmp.day;if (n>=(365 + leapYear(tmp.year) - count)) {n -= (365 + leapYear(tmp.year) - count);tmp.month = 12;tmp.day = 31;while (n >= (365 + leapYear(tmp.year+1))) {n -= (365 + leapYear(tmp.year+1));}}for (; n>0; --n) {if (tmp.day == getMonthDay(tmp.year, tmp.month)) {tmp.day = 1;tmp.month == 12 ? tmp.month = 1, ++ tmp.year : ++tmp.month;}else {++tmp.day;}}return tmp;}Date a = Date(m_year, m_month, m_day);Date b = a+n;cout << setw(4) << setfill('0') << b.year << setw(2) << setfill('0') << b.month << setw(2) << setfill('0') << b.day;return 0;
}
#include <iostream>
#include<iomanip>
using namespace std;int a[2][12] = { { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };
int b[2] = { 365,366 };int judge(int a)
{if ((a % 4 == 0 && a % 100 != 0) || a % 400 == 0)return 1;else return 0;
}//输入n(n大于0),则返回从1年1月1日(包括当天)开始经过了n天后的日期(不包括当天)
void date(int n, int& year, int& month, int& day)
{for (int i=month-1; i<12; i++) n -= a[judge(year)][i];n -= (a[judge(year)][month-1] - day);year ++;while (n>365) {n -= (judge(year)+365);year++;}for (int i=0; n >= a[judge(year)][i]; i++) {month++;n -= a[judge(year)][i];}day = n;
}int main()
{int a, n, year, month, day;cin >> a >> n;year = a / 10000;month = (a % 10000)/100;day = a % 100;date(n, year, month, day);if (year >= 9999)cout << "out of limit!";else cout << setfill('0') << setw(4) << year << setfill('0') << setw(2) << month << setfill('0') << setw(2) << day << endl;//cout << year << "." << month << "." << day << endl;return 0;
}

大佬的代码,正确!

#include <iostream>
using namespace std;int yearday(int year);
int convertchartoint(char* text,int length);
void converinttochar(int num,char* const text,int n);
void convertobegin(int &year,int &month,int &day,long int &n);int main()
{char p[9];cin>>p;long int n;cin>>n;//将字符串中的年份移到int型变量year中int year=convertchartoint(p,4);//月份转移到int型变量month中int month=convertchartoint(p+4,2);//将几号转移到int型变量day中int day=convertchartoint(p+6,2);//往前数化归到起始日期为某年第一天的问题convertobegin(year,month,day,n);//求过了n天后是哪一年,结果为year,还剩下n天while(n>=0){n-=yearday(year);++year;}--year;n+=yearday(year);if(year>=10000){cout<<"out of limit!";return 0;}//求过了n天后是哪一月,结果为month,还剩下n天int monthday[12]={31,( (yearday(year)==365)?28:29 ),31,30,31,30,31,31,30,31,30,31};while(n>=0){n-=monthday[month-1];//烦,month月有monthday[month-1]天!++month;}--month;n+=monthday[month-1];//求是哪一天,结果为dayday=n+1;//输出模块converinttochar(year,p+3,4);converinttochar(month,p+5,2);converinttochar(day,p+7,2);cout<<p<<endl;return 0;
}
//化归到起始日期为某年第一天的问题
void convertobegin(int &year,int &month,int &day,long int &n)
{n+=day-1;day=1;int monthday[12]={31,( (yearday(year)==365)?28:29 ),31,30,31,30,31,31,30,31,30,31};for(;month>1;--month){n+=(monthday[month-1-1]);}
}
//将整型放到text结尾且总长为n的字符串,前面补0,text下一位
void converinttochar(int num,char* const text,int n)
{text[1]='\0';for(int i=0;i<n;++i){text[0-i]=('0'+num%10);num/=10;}
}
//将一串字符转成整型
int convertchartoint(char* text,int length)
{int num=0;for(int i=0;i<length;++i){num=num*10+(text[i]-'0');}return num;
}
//计算这一年有几天
int yearday(int year)
{bool leapyear=false;if( ((year%4==0)&&(year%100!=0))||(year%400==0) ){leapyear=true;}return ((leapyear)?366:365);
}

日期推算 (函数、指针)相关推荐

  1. 根据日期推算星期和历法由来

    太阳历和公历(儒略历与格里历) 现在世界上通用的历法--公历,有人曾似是而非地称之为"西历".其实,究其根 源,这种历法并非产生于西方,而是产生于6000多年前的古埃及.     ...

  2. MySQL中常用日期时间函数及获得

    MySQL中常用日期时间函数: 下面的查询选择了所有记录,其date_col的值是在最后30天以内: mysql> SELECT something FROM table WHERE TO_DA ...

  3. 第16周项目3--用函数指针调用函数(吃饭,睡觉,打豆豆)

    /* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作 者:刘畅 * 完成日期:2014 年 12 ...

  4. C++函数指针定义及调用

    今天在一问一答审核题目的时候发现一个题目的题干是错的,主要意思就是定义一个函数指针,现在将修改后的结果放上来,给出定义函数指针的方法: /** 作者: 齐士垚 日期: 2013.5.8 功能: 定义函 ...

  5. Java实现获取前、后N天日期的函数分享2

    两日期之间的旬差 for (int j = 1; j <= 5; j++) {//取得最近5个旬度的时间,从当前旬的上一旬开始往前推算Date curDate = new Date();int ...

  6. c/c++中的函数指针和指针函数

    定义 1.指针函数,本质是函数,返回值为指针,形如,int *pfun(int, int),由于"*"的优先级低于"()"的优先级,所以等同于int *(pfu ...

  7. MySQL 学习笔记(3)— 字符串函数、数值函数、日期时间函数、流程函数、聚集函数以及分组数据

    1. 字符串函数 MySQL 的常用函数包括字符串函数.数值函数.日期时间函数.流程函数等. SELECT ascii("abc"),char(97),concat("h ...

  8. 函数指针amp;绑定: boost::functoin/std::function/bind

    see link: https://isocpp.org/wiki/faq/pointers-to-members function vs template: http://stackoverflow ...

  9. 0709 C语言常见误区----------函数指针问题

    1.函数指针的定义 对于函数 void test(int a, int b){ // } 其函数指针类型是void (* ) (int , int), 注意这里第一个括号不能少, 定义一个函数指针,v ...

  10. java跨函数跳转_C语言中将绝对地址转换为函数指针以及跳转到内存指定位置处执行的技巧...

    1.方法一 要对绝对地址0x100000赋值,我们可以用 (unsigned int  * ) 0x100000 = 1234; 那么要是想让程序跳转到绝对地址是0x100000去执行,应该怎么做? ...

最新文章

  1. Svn 笔记—— Hooks
  2. 把blogengine当作cms作公司网站
  3. golang中的new和make的区别
  4. Linux环境下增加swap交换分区
  5. [习题].FindControl()方法 与 PlaceHolder控件 #2(动态加入「子控件」的事件)
  6. pytest.mark.parametrize()基本用法
  7. SSIS(2012版本)连接MongoDB,使用SSIS2012导入MongoDB
  8. MBProgressHud添加自定义动画
  9. keytool 错误 java.io.IOException: incorrect AVA format
  10. mybatise 实现同一字段多模糊查询
  11. 深度学习入门:一句话告诉你什么是神经网络(CNN,RNN,DNN)
  12. 一次使用BeanPostProcessor疏漏引起的重大bug
  13. vim无法写入hosts文件(提示hosts是一个只读文件)
  14. 深信服(scsa认证)学习过程
  15. c语言 公式编辑器,AxMath(公式计算编辑器)
  16. markdown 语法
  17. MATLAB神经网络工具箱(代码简单实现)
  18. 领域驱动设计(1) DDD的一些基础概念
  19. 网站死链检查处理方法
  20. C#下支付宝新版异步回调数据处理及校验(需支付宝提供的AopSdk)

热门文章

  1. 助力佰家当乘风破浪 湖南中检鉴定保驾护航
  2. js 判断值是否为数字
  3. Java Web —— Session 和 cookie 保存登录信息
  4. 就测试现状小结一下20150909
  5. 22-05-19 西安 javaweb(04) xml、DOM4J,Xpath、 tomcat应用服务器、HTTP协议
  6. HTML 块级元素、行内元素和行内块级元素
  7. iphone测试cpu性能的软件,iPhone 6S三大性能实测:恐怖黑科技!
  8. JAVA计算机毕业设计蔬菜水果销售系统源码+系统+mysql数据库+lw文档
  9. 易中天著,《易中天中华史之15女皇武则天》
  10. android 电量排行榜,最省电的安卓手机(2021最新手机续航排名)