蓝桥杯_基础练习 时间转化

给定一个以秒为单位的时间t,要求用 “<H>:<M>:<S>”的格式来表示这个时间。<H>表示时间,<M>表示分钟, 而<S>表示秒,它们都是整数且没有前导的“0”。例如,若t=0,则应输出是“0:0:0”;若t=3661,则输出“1:1:1”。
输入格式
  输入只有一行,是一个整数t(0<=t<=86399)。
输出格式
  输出只有一行,是以“<H>:<M>:<S>”的格式所表示的时间,不包括引号。
样例输入
0
样例输出
0:0:0
样例输入
5436
样例输出
1:30:36

这里其实最主要考察的还是字符串转化,建议用stringstream字符串流来操作。

#include<iostream>
#include<string>
#include<sstream>
#include<algorithm> 
using namespace std;

int main()
{
	int time;
	cin >> time;
	int H, M, S;
	H = M = S = 0;
	H = time/3600;
	M = time%3600/60;
	S = time%60;
	string out;
	stringstream ss;
	ss << H << ':' << M << ':' << S;
	cout << ss.str();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41282102/article/details/88528675