【topcoder】 SRM 144 div 2 200
2016-04-24 13:03
766 查看
Problem
Problem StatementComputers tend to store dates and times as single numbers which represent the number of seconds or milliseconds since a particular date. Your task in this problem is to write a method whatTime, which takes an int, seconds, representing the number of seconds since midnight on some day, and returns a string formatted as “< H >:< M >:< S >”. Here, < H > represents the number of complete hours since midnight, < M > represents the number of complete minutes since the last complete hour ended, and < S > represents the number of seconds since the last complete minute ended. Each of < H >, < M >, and < S > should be an integer, with no extra leading 0’s. Thus, if seconds is 0, you should return “0:0:0”, while if seconds is 3661, you should return “1:1:1”.
Definition
Class:
TimeMethod:
whatTimeParameters:
intReturns:
stringMethod signature:
string whatTime(int seconds)(be sure your method is public)
Limits
Time limit (s):
2.000Memory limit (MB):
64Constraints
seconds will be between 0 and 24*60*60 - 1 = 86399, inclusive.Examples
0)0
Returns: “0:0:0”
1)
3661
Returns: “1:1:1”
2)
5436
Returns: “1:30:36”
3)
86399
Returns: “23:59:59”
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
Solution
Get 155.57/200 points#include <iostream> #include <string> #include <sstream> using namespace std; class Time{ public: string whatTime(int seconds); }; string Time::whatTime(int seconds){ int h = seconds / 60 / 60; int m = seconds / 60 - h*60; int s = seconds - h*60*60 - m*60; stringstream ss; string str, str2, str3; ss << h; ss >> str; ss.clear(); ss << m; ss >> str2; ss.clear(); ss << s; ss >> str3; return str + ":" + str2 + ":" + str3; }
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析