close
內容
前幾天看到一隻 b961 YoJudge怪獸,感覺太強大了,所以先打Lo練功,期盼來日再戰YoJudge怪獸。
這次任務較簡單,將以下各種格式的時間單位統一轉換為毫秒。 以下 x,y,z,a,b皆為非負整數
可能出現的時間單位格式如下:
xhour :代表 x 小時, 0<=x<24
xhym :代表 x 小時又 y 分鐘, 0<=x<24, 0<=y<60
xhymzs :代表 x 小時又 y 分鐘又 z 秒, 0<=x<24, 0<=y<60, 0<=z<60
ymin :代表 y 分鐘, 0<=y<60
ymzs :代表 y 分鐘又 z 秒, 0<=y<60, 0<=z<60
zs :代表 z 秒, 0<=y<60, 0<=z<60
z.as :代表 z 秒又 a*100毫秒, 0<=z<60, 0<=a<10
bms :代表 b 毫秒, 0<=b<1000
輸入說明
多行直到 EOF,每行只有如上題目所說的時間格式,沒有空格
輸出說明
將輸入的每一行換算為毫秒,輸出一行整數
C++
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string is,cs1; stringstream ss; long long ans,n=string::npos,h1,h2; double t; while(cin>>is)//輸入時間 { ans=0; ss.clear(); if(is.find("hour")!=n) { h1=is.find("h"); ss<<cs1.assign(is,0,h1);//這是字串轉數字的用法 //先把時間指定給另一個字串 //再用到字串流裡 ss>>t; //再轉成數字型態 cout<<int(t*60*60*1000); } else if(is.find("h")!=n) { h1=is.find("h"); ss<<cs1.assign(is,0,h1); ss>>t; ans=t*60*60*1000; if(is.find("m")!=n) { h2=is.find("m"); ss.clear(); ss<<cs1.assign(is,h1+1,h2-h1-1); ss>>t; ans=ans+t*60*1000; h1=h2; if(is.find("s")!=n) { h2=is.find("s"); ss.clear(); ss<<cs1.assign(is,h1+1,h2-h1-1); ss>>t; ans=ans+t*1000; } } cout<<ans; } else if(is.find("ms")!=n)//在找m或s之前 先找ms才不會出錯 { h1=is.find("ms"); ss<<cs1.assign(is,0,h1); ss>>t; cout<<t; } else if(is.find("min")!=n) { h1=is.find("min"); ss<<cs1.assign(is,0,h1); ss>>t; cout<<int(t*60*1000); } else if(is.find("m")!=n) { h1=is.find("m"); ss.clear(); ss<<cs1.assign(is,0,h1); ss>>t; ans=t*60*1000; if(is.find("s")!=n) { h2=is.find("s"); ss.clear(); ss<<cs1.assign(is,h1+1,h2-h1-1); ss>>t; ans=ans+t*1000; } cout<<ans; } else if(is.find("s")!=n) { h1=is.find("s"); ss.clear(); ss<<cs1.assign(is,0,h1); ss>>t; ans=t*1000; cout<<ans; } cout<<endl; } return 0; }
Python
while True: try: n = input() if n.find('h') > 0: n = n.split('h') t = int(n[0])*60*60*1000 if n[1].find('u') > 0: pass else: w = n[1] w = w.split('m') t = t+int(w[0])*60*1000 if n[1].find('s') > 0: t = t+int(w[1][0:w[1].find('s')])*1000 elif n.find('ms') > 0: n = n.split('ms') t = int(n[0]) elif n.find('m') > 0: n = n.split('m') t = int(n[0])*60*1000 if n[1].find('n') > 0: pass else: w = n[1].split('s') t = t+int(w[0])*1000 else: n = n.split('s') t = float(n[0])*1000 print(int(t)) except EOFError: break
全站熱搜