Topcoder 字符串转换题

原题:

 

 

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)
    
 

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"
 

我的代码:

#include <iostream>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <string>
#include <vector>
#include <list>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
class  Time{
public:
      string  whatTime(int seconds);
};
string   Time::whatTime(int seconds)
{
       string   sTime;//,sHour,sMinute,sSec;
    char  strH[10000],strM[10000],strS[10000];
    int  nHour=0,nMinute=0,nSec=0;
    int  nTemp=0;
    nSec = seconds%60;
    nTemp=(seconds-nSec)/60;
       nMinute=nTemp%60;
    nHour=nTemp/60;
    sprintf(strH,"%d",nHour);
    sprintf(strM,"%d",nMinute);
    sprintf(strS,"%d",nSec);
       string  sHour(strH);
    string  sMinute(strM);
    string  sSec(strS);
    sHour.push_back(':');
    sMinute.push_back(':');
    sTime+=sHour;
    sTime+=sMinute;
    sTime+=sSec;
    return  sTime;
}
int  main()
{
    Time    A;
 cout<<A.whatTime(86399)<<endl;

 return 0;
}

 

牛人代码:

#include <iostream>
#include <sstream>
using  namespace  std;
class  Time{
public:
 string  whatTime(int seconds)
 {
  int  hours=seconds/3600;
  int  mins=(seconds%3600)/60;
  int  secs= (seconds%3600)%60;
  stringstream   ss;
  ss<<hours<<":"<<mins<<":"<<secs;
  return   ss.str();
 }
};
int  main()
{
 Time     A;
 cout<<A.whatTime(86399)<<endl;
}

 

开始我用了itoa函数,但是topcoder平台不支持该函数,我的最初代码如下:

 

 #include <iostream>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <string>
#include <vector>
#include <list>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
class  Time{
public:
      string  whatTime(int seconds);
};
string   Time::whatTime(int seconds)
{
    string   sTime,sHour,sMinute,sSec;
    char  strH[10000],strM[10000],strS[10000];
    int  nHour=0,nMinute=0,nSec=0;
    int  nTemp=0;
    nSec = seconds%60;
    nTemp=(seconds-nSec)/60;
       nMinute=nTemp%60;
    nHour=nTemp/60;
    sHour.assign(itoa(nHour,(char*)sHour.c_str(),10));
    sMinute.assign(itoa(nMinute,(char *)sMinute.c_str(),10));
    sSec.assign(itoa(nSec,(char*)sSec.c_str(),10));
    sHour.push_back(':');
    sMinute.push_back(':');
    sTime+=sHour;
    sTime+=sMinute;
    sTime+=sSec;
    return  sTime;
}
int  main()
{
    Time    A;
 cout<<A.whatTime(86399)<<endl;

 return 0;
}

 

小结:

这次做题,复习了string的相关函数,并且学会了使用sprintf,同时还向牛人学了不少东西。

你可能感兴趣的:(Topcoder 字符串转换题)