挑战任务

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

例如: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. 高程三 BOM 读书笔记

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  2. tomcat jvm 内存调优 适用于 JDK 6 & 7

    参考:https://blog.csdn.net/m0_37327416/article/details/76185051 1.jvm内存管理机制: 1)堆(Heap)和非堆(Non-heap)内存 ...

  3. MonGoDB 在linux 上的安装和配置

    01: 下载 linux 版本的二进制包 => https://www.mongodb.com/ 02: 解压  => tar -zxf mongodb-linux-x86_64-3.4. ...

  4. groovy Date 格式化

    刚开始使用Java,瞬间爱上:换了个厂接触到了groovy,开始有点嫌弃Java了... 看看时间的格式化 java玩法: new SimpleDateFormat("yyyy-MM-dd ...

  5. element ui输入框监听enter事件

    <el-form-item label="关键字"> <el-input v-model="keywords" placeholder=&qu ...

  6. AJax知识介绍

    参考:http://www.runoob.com/ajax/ajax-asp-php.html

  7. 网关、子网掩码、DHCP, DNS

    都跟ip地址相关,IP地址构成:网络地址+主机地址 子网掩码可以确定网络地址,例如某IP:192.168.1.102 子网掩码:255.255.255.0, 那么网络地址就是192.168.1,主机地 ...

  8. 2312--1.3.4 Prime Cryptarithm 牛式

    Description 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. * * * x * * ------- * * * * * * ------ ...

  9. python list元素为dict时的排序

    # 简单的dict lst = [('d', 2), ('a', 4), ('b', 3), ('c', 2)] # 按照value排序 lst.sort(key=lambda k: k[1]) pr ...

  10. Idea使用Maven异常 --- Maven网络代理设置

    在conf/setting.xml和m2/repository/setting.xml中加入: <proxies> <!-- proxy | Specification for on ...