topcoder 记录 srm 144 div2

题目要求:(srm 144 div2)

Problem Statement

Computers 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 :
Time
Method :
whatTime
Parameters :
int
Returns :
string
Method signature :
string whatTime(int seconds)
(be sure your method is public)
Limits


Time limit(s) :
2.000
Memory limit(MB) :
64
Constraints
-
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.

#include <string>
//#include <iostream>
using std::string;
//using std::cin;
//using std::cout;
//using std::endl;

class Time
{
public:
	Time();
	~Time();
	string whatTime(int seconds);
private:
	int m_hour;
	int m_minute;
	int m_second;
	string m_time;

	//string int2str_2bit(int key);
};

Time::Time():m_hour(0),m_minute(0),m_second(0){}

Time::~Time(){}

string Time::whatTime(int seconds)
{
	m_hour = seconds / 60 / 60;
	m_minute = seconds / 60 % 60;
	m_second = seconds % 60;
	char hour[3];
	char minute[3];
	char second[3];
	if (m_hour > 10)
	{
		hour[0] = m_hour / 10 + '0';
		hour[1] = m_hour % 10 + '0';
		hour[2] = '\0';
	}
	else
	{
		hour[0] = m_hour % 10 + '0';
		hour[1] = '\0';
	}
	
	if (m_minute > 10)
	{
		minute[0] = m_minute / 10 + '0';
		minute[1] = m_minute % 10 + '0';
		minute[2] = '\0';
	}
	else
	{
		minute[0] = m_minute % 10 + '0';
		minute[1] = '\0';
	}
	
	if (m_second > 10)
	{
		second[0] = m_second / 10 + '0';
		second[1] = m_second % 10 + '0';
		second[2] = '\0';
	}
	else
	{
		second[0] = m_second % 10 + '0';
		second[1] = '\0';
	}

	/*
	_itoa(m_hour, hour, 10);
	_itoa(m_minute, minute, 10);
	_itoa(m_second, second, 10);
	*/

	

	
	m_time.clear();
	m_time.append(hour);
	m_time.push_back(':');
	m_time.append(minute);
	m_time.push_back(':');
	m_time.append(second);
	
	return m_time;
}

int main(int arg, char ** argv)
{
	Time instance;
	instance.whatTime(0);
	instance.whatTime(3661);
	instance.whatTime(5436);

}

//string Time::int2str_2bit(int key)
//{
//	string res;
//	res[0] = key / 10;
//	res[1] = key % 10;
//	return res;
//}

代码测试结果:

[TEST CASE #1]:
Success: true
Status: 0
Return value: 0:0:0
Correct example: true
Execution time (ms): 0
Peak memory used (KB): 11888


[TEST CASE #2]:
Success: true
Status: 0
Return value: 1:1:1
Correct example: true
Execution time (ms): 0
Peak memory used (KB): 11888


[TEST CASE #3]:
Success: true
Status: 0
Return value: 1:30:36
Correct example: true
Execution time (ms): 0
Peak memory used (KB): 11888


[TEST CASE #4]:
Success: true
Status: 0
Return value: 23:59:59
Correct example: true
Execution time (ms): 2
Peak memory used (KB): 11888


心得:

这是我第一次在topcoder上做题目,一开始打算用itoa函数进行转化的,但系统不识别这个函数,这能自己写底层,但自己不太会写,于是参照了一些资料

点击打开链接http://bbs.csdn.net/topics/220013347

他们提到了几种转换的方法(我们的程序中主要参照第4种方法):

1.

#include <iostream>
#include <string>
using namespace std;
 
int main()
{
    int n = 65535;
    char t[256];
    string s;
 
    sprintf(t, "%d", n);
    s = t;
    cout << s << endl;
 
    return 0;
}
2.

//第二种方法
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
 
int main()
{
    int n = 65535;
    stringstream ss;
    string s;
    ss << n;
    ss >> s;
    cout << s << endl;
 
    return 0;
}



3.

atoi,atol,strtod,strtol,strtoul实现类型转换

4.

#include "iostream"
#include "string"
using namespace std;
 
string int2str(int n) { 
    char t[24];
    int i = 0; 
    while (n) {
        t[i++] = (n % 10) + '0';
        n /= 10;
    }
    t[i] = 0; 
    return string(strrev(t));
}
 
int main() {     
    int n = 1312355;
    string str = int2str(n);
    cout<<str<<endl;
    return 0;
}




你可能感兴趣的:(C++,topcoder,SRM)