返回列表 發帖

C++第一題:將輸入之秒數轉換成「時:分:秒」。

本帖最後由 stephen 於 2010-2-20 23:26 編輯

C++第一題:將輸入之秒數轉換成「時:分:秒」。如輸入 10000,則輸出 02:46:40。
  1. #include <iostream>  
  2. using namespace std;  
  3. int main(void){
  4.     /*1.將輸入之秒數轉換成「時:分:秒」。如輸入 10000,則輸出 02:46:40。 */
  5.    
  6.     int time;  
  7.     cout << "Please input second to transfer : " << endl;
  8.     cin >> time;
  9.    
  10.     cout << time / 3600 << " : ";  // get hours
  11.     time = time - ((time / 3600) * 3600);  // get second cut hours
  12.     cout << time / 60 << " : ";  // get Minutes
  13.     time = time - ((time / 60) * 60);  // get second cut Minutes
  14.     cout << time << endl;  // seconds
  15.       
  16.     system("pause");
  17.     return 0;
  18. }  
複製代碼

  1. /**1. 將輸入之秒數轉換成「時:分:秒」。如輸入 10000,則輸出 02:46:40。 **/
  2. #include <iostream>
  3. #include <cstdlib>
  4. using namespace std;
  5. int main(void){
  6.     int s;
  7.     int m;
  8.     int h;
  9.    
  10.     cout << "輸入秒數" << endl;
  11.     cin >> s;
  12.    
  13.     h = s / 3600;
  14.     m = (s-h*3600) / 60;
  15.     s = s - (h*3600) - (m*60);
  16.    
  17.     cout << h << ":" << m << ":" << s << endl;
  18.    
  19.     system("pause");
  20.     return 0;
  21. }
複製代碼
我是小紅老師,小紅老師是我!!

TOP

返回列表