9.16 CISCO编程题(CPP版本)

1.输入一个日期和一个秒数,返回秒数之后的日期
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int howmanyday(int year, int mouth) {
    if (mouth == 1 || mouth == 3 || mouth == 5 || mouth == 7 || mouth == 8 || mouth == 10 || mouth == 12) return 31;
    if (mouth == 4 || mouth == 6 || mouth == 9 || mouth == 11) return 30;
    if (year % 400 == 0 || year % 100 != 0 && year % 4 == 0) return 29;
    else return 28;
}

int main () {
    string input;
    getline(cin, input);
    vector<int> nums;
    string temp;
    for (const auto& c : input) {
        if (c <= '9' && c >= '0') {
            temp += c;
        } else {
            if (!temp.empty()) nums.push_back(stoi(temp));
            temp.clear();
        }
    }
    if (!temp.empty()) {
        nums.push_back(stoi(temp));
        temp.clear();
    }
    int add = nums[6];
    int totalS = add + nums[5];
    nums[5] = totalS % 60;
    int totalM = nums[4] + totalS / 60;
    nums[4] = totalM % 60;
    int totalH = nums[3] + totalM / 60;
    nums[3] = totalH % 24;
    nums[2] += totalH / 24;
    while (nums[2] > howmanyday(nums[0], nums[1])) {
        int cur = howmanyday(nums[0], nums[1]);
        nums[2] -= cur;
        nums[1]++;
        if (nums[1] > 12) {
            nums[1] = 1;
            nums[0]++;
        }
    }
    printf("%04d-%02d-%02d %02d:%02d:%02d\n", nums[0], nums[1], nums[2], nums[3], nums[4], nums[5]);
    return 0;
}


#秋招##笔经##思科#
全部评论
写的真好,我只会写一堆 if
4 回复
分享
发布于 2021-09-16 21:09
#include<bits/stdc++.h> int DAY[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; bool Isleap(int year){     return (year%4==0&&year%100!=0||year%400==0); } using namespace std; struct time{     int yy,MM,dd,hh,mm,ss; }; int main(){     struct time x;     scanf("%d,%d,%d,%d,%d,%d,",&x.yy,&x.MM,&x.dd,&x.hh,&x.mm,&x.ss);     int num_ss;     int pre_dd = x.dd;     scanf("%d",&num_ss);     x.ss += num_ss;     while(x.ss>=60){         x.ss -= 60;         x.mm++;     }     while(x.mm>=60){         x.mm -=60;         x.hh++;     }     while(x.hh>=24){         x.hh -=24;         x.dd++;     }     int ddchazhi = x.dd - pre_dd;     if(Isleap(x.yy)){         DAY[1]=29;     }    for(int i=0;i<ddchazhi;++i){        x.dd++;        if(x.dd>DAY[x.MM-1]){            x.MM++;            x.dd =1;        }        if(x.MM>=12){            x.yy++;            x.MM=1;        }    }     printf("%04d-%02d-%02d %02d:%02d:%02d",x.yy,x.MM,x.dd,x.hh,x.mm,x.ss);     return 0; }
点赞 回复
分享
发布于 2021-09-23 11:47
联想
校招火热招聘中
官网直投

相关推荐

2 4 评论
分享
牛客网
牛客企业服务