笔试 CVTE
程序员文章站
2022-03-28 20:22:51
...
将毫秒转为天、时、分钟
#include "pch.h"
#include <iostream>
#include <sstream>
using namespace std;
std::string FormatTime(std::uint64_t timestamp, const std::string& format) {
long days = timestamp / (1000 * 60 * 60 * 24);
long hours = (timestamp % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
long minutes = (timestamp % (1000 * 60 * 60)) / (1000 * 60);
long seconds = (timestamp % (1000 * 60)) / 1000;
long ms = timestamp % 1000;
std::ostringstream os;
std::string result;
for (int i = 0; i < format.size(); i++) {
if (format[i] == 'D') {
os << days << "天";
}
else if (format[i] == 'H') {
os << hours << "时";
}
else if (format[i] == 'M') {
os << minutes << "分钟";
}
else if (format[i] == 'S') {
os << seconds << "秒";
}
else if (format[i] == 'L') {
os << ms << "毫秒";
}
}
return os.str();
}
int main()
{
std::uint64_t timestamp = 86400001;
const std::string& format = "DHMSL";
std::string result = FormatTime(timestamp, format);
cout << result << endl;
return 0;
}
主要难点:long转string,用ostringstream,需要include<sstream>
同时ostringstream转string, ostringstreamObj.str()
初次笔试总结(一点点):
(1)需要装好虚拟机
(2)时间上需要把握好,大概剩下60分钟处理两道编程题差不多