挑战任务

我们吃的食物都有保质期,现在食品监督管理局想要制作一个能准确计算食品过期日期的小程序,需要请你来进行设计。

例如:A食品在2018年1月1日生产,保质期是20天,则它的过期日期在2018年1月21日。

编程要求

补全函数string getDate(string releaseDate,int day)其中releaseDate表示食品出厂日期day表示保质期,请根据传入的数据计算食品的过期日期,格式为yyyy-mm-dd4位年份2位月份2位日期。比如:2015-02-19

请严格按照格式书写,不能出现其它文字或符号,并将最终结果做为函数的返回值返回。

测试说明

样例1:

输入:
2016-01-0120

输出:

2016-01-21

#ifndef _TEST
#define _TEST
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
std::vector<std::string> split(std::string str,std::string pattern)
{
std::string::size_type pos;
std::vector<std::string> result;
str+=pattern;
int size=str.size(); for(int i=0; i<size; i++)
{
pos=str.find(pattern,i);
if(pos<size)
{
std::string s=str.substr(i,pos-i);
result.push_back(s);
i=pos+pattern.size()-1;
}
}
return result;
}
class Task{
public:
string getDate(string releaseDate,int days){ int year = atoi(split(releaseDate,"-")[0].c_str());
int month = atoi(split(releaseDate,"-")[1].c_str());
int day = atoi(split(releaseDate,"-")[2].c_str()); int j = 0;
for(j=1;j<=days;j++)
{
day=day+1;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
if(day==32)
{
month=month+1;
day=1;
}
if(month==13)
{
year=year+1;
month=1;
}
}
if(month==2)
{
if(((year%4==0&&year%100!=0)||year%400==0))
{
if(day==30)
{
day=1;
month=month+1;
}
}
else
{
if(day==29)
{
day=1;
month=month+1;
}
}
}
if(month==4||month==6||month==9||month==11)
{
if(day==31)
{
month=month+1;
day=1;
}
if(month==13)
{
year=year+1;
month=1;
}
}
} char tmp[15] = "";
sprintf(tmp,"%d-%02d-%02d",year,month,day);
string re = tmp;
return re;
}
};
#endif

  

#ifndef _TEST
#define _TEST
#include <iostream>
#include <vector>
using namespace std; class Task{
public:
vector<char> inversion(string str){
vector <char> vec;
int index = str.length();
while(index--){
vec.push_back(str[index]);
}
return vec;
}
};
#endif

  

#include <iostream>
#include <vector>
#include <stdio.h>
#include "Task.hpp" using namespace std; int main(){
string str;
getline(cin,str); Task tt;
vector <char> result;
result = tt.inversion(str);
for(int i = 0; i<str.length();i++){
cout << result[i];
}
cout << endl;
}

运行结果

 

全国高校绿色计算大赛 预赛第一阶段(C++)第4关:计算日期的更多相关文章

  1. 全国高校绿色计算大赛 预赛第一阶段(Python)

    第1关将字符串反转 #!/usr/bin/env python # -*- coding: utf-8 -*- class Task: def inversion(self, str): # **** ...

  2. 全国高校绿色计算大赛 预赛第一阶段(C++)第2关:扔桃子

    挑战任务 动物园有一只小猴子喜欢吃桃子,不过它有个很独特的习惯,每次都把找到的桃子分成相等的两份,吃掉一份,留一份.如果不能等分,小猴子就会丢掉一个然后再分.第二天再继续这个过程,直到最后剩一个桃子了 ...

  3. 全国高校绿色计算大赛 预赛第一阶段(C++)第3关:旋转数组

    挑战任务 在计算机中,一张数字图像,可以被看做是一个矩阵或者说数组. 学过线性代数的同学对矩阵肯定不陌生.一般来说,图像是一个标准的矩形,有着宽度(width)和高度(height).而矩阵有着行(r ...

  4. 全国高校绿色计算大赛 预赛第一阶段(C++)第1关:将字符串反转

    挑战任务 将输入的字符串str进行反转. 编程要求 补全右侧vector<char> inversion(string str)函数实现字符串的反转并返回反转之后的字符串,其中函数参数st ...

  5. 全国高校绿色计算大赛 预赛第三阶段(Python)(随机数)

    只提交了随机数 (真心不会 T-T ) import csv import random import pandas as pd import numpy as np # 预测结果文件:src/ste ...

  6. 全国高校绿色计算大赛 预赛第二阶段(Python)第1关:统计分数的麻烦

    挑战任务 “绿盟杯”比赛过后,赛事承办方的各位工作人员们就开始分头统计各个参赛队伍和同学的成绩了.赛事规模很大,有10000个队伍参加.经过工作人员认真负责的统计,本来已经统计好了这一万个队伍的分数和 ...

  7. 全国高校绿色计算大赛 预赛第二阶段(Python)

    第1关统计分数的麻烦 class Task: def get_lost_scores(self, scores): s = "" index = [1 for i in range ...

  8. echarts全国疫情统计可视化地图(第一阶段)

    DBUtil.java package com.helloechart; import java.sql.Connection; import java.sql.DriverManager; impo ...

  9. 2019年全国高校计算机能力挑战赛初赛C语言解答

    http://www.ncccu.org.cn 2019年全国高校计算机能力挑战赛分设大数据算法赛,人工智能算法赛,Office高级应用赛,程序设计赛4大赛项 C语言初赛解答 1:编程1 16.现有一 ...

随机推荐

  1. Mybatis学习4——核心文件sqlMapperConfig.xml属性

    1.外部文件jdbc.properties jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis ...

  2. vs2008发布项目失败的解决方法

    解决办法: 要知道发布是怎么失败的,用组合键"Ctrl+Alt+O"即可,仔细查看信息可发现有没发布成功的详细提示,然后在资源管理器中找到那一项,删除或排除到项目外,重新生成之后再 ...

  3. python大法好——变量、常量、input()、数据类型、字符串、格式化输出、运算符、流程控制语句、进制、字符编码

    python基础知识 1.变量 变量:把程序运算的中间结果临时存到内存里,以备后面的代码可以继续调用. 作用:A.存储数据. B.标记数据. 变量的声明规则: A:变量名只能是字母,数字或下划线任意组 ...

  4. [记录] Ubuntu 配置Apache虚拟站点

    版本 Ubuntu 16.04 1 . 首先找到Apapche配置文件夹  /etc/apache2/ apache2.conf conf-enabled magic mods-enabled sit ...

  5. 小程序登录&授权&获取用户信息

    一 .登录 时序图如下: wx.login() 获取js_code 示例代码: App({   onLaunch: function() {     wx.login({       success: ...

  6. Android虚拟机与Java虚拟机 两种虚拟机的比较

    在Android的体系框架中有一部分叫做Android Runtime,即Android运行时环境,这个环境包括了两个部分,一个是Android的核心类库,还有一个就是Dalvik虚拟机了. Andr ...

  7. Struts2中使用HttpServletRequest和HttpServletResponse

    一.非Ioc方式 这种方式主要是利用了com.opensymphony.xwork2.ActionContext类以及org.apache.struts2.ServletActionContext类, ...

  8. leetcode ex3 找出穿过最多点的直线 Max Points on a Line

    题目 https://oj.leetcode.com/problems/max-points-on-a-line/ 答案与分析 http://www.aiweibang.com/yuedu/18326 ...

  9. linux下安装Tomcat7.0

    1.下载安装文件:Download apache-tomcat-7.0.14.tar.gz        下载1              下载 2.输入命令解压文件:tar - zxvf apach ...

  10. Python在cmd上打印彩色文字

    在Windows上编写python程序时,有时候需要对输出的文字颜色进行设置,特别是日志显示,不同级别的日志设置不同的颜色进行展示可以直观查看.本文主要描述通过ctypes.windll.kernel ...