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;
}
查看28道真题和解析