[C++] hdoj1036简单题、格式化输出 →→→→→进入此内容的聊天室

来自 , 2020-01-22, 写在 C++, 查看 126 次.
URL http://www.code666.cn/view/e1314fc0
  1. #include<iostream>
  2. #include <math.h>
  3. #include<string>
  4. using namespace std;
  5.  
  6. int round(double a){
  7.         if(a - floor(a) >= 0.5){
  8.                 a = (int)(a + 1);
  9.         }
  10.         return a;
  11. }
  12.  
  13. int main(){
  14.         int n1, n2;//n2是队伍号码
  15.         string n4;//n4是时间
  16.         int total;
  17.         int flag;
  18.         double n3;
  19.         cin>>n1>>n3;//n1是接力人数,n3是公里数
  20.         while(cin>>n2){//处理每一队的数据
  21.                 total = 0;
  22.                 cout.width(3);
  23.                 cout<<n2<<": ";
  24.                 flag = 0;
  25.                 for(int i = 0; i < n1; i++){
  26.                         cin>>n4;
  27.                         if(n4 != "-:--:--"){
  28.                                 total+=(int)(n4[6] - 48);
  29.                                 total+=((int)(n4[5] - 48)) * 10;
  30.                                 total+=((int)(n4[3] - 48)) * 60;
  31.                                 total+=((int)(n4[2] - 48)) * 600;
  32.                                 total+=((int)(n4[0] - 48)) * 3600;
  33.                         }else{
  34.                                 flag = 1;
  35.                                 cout<<"-"<<endl;
  36.                                 if(i != n1 - 1){
  37.                                         string s1;
  38.                                         getline(cin, s1);
  39.                                         break;
  40.                                 }
  41.                         }
  42.                 }
  43.                 if(flag == 1){
  44.                         continue;
  45.                 }
  46.                 total = round(total / n3);
  47.                 int t1, t2, t3, t4, t5;
  48.                 t1 = total / 3600;
  49.                 if(t1){
  50.                         cout<<t1<<":";
  51.                         total %= 3600;
  52.                 }
  53.                 t2 = total / 600;
  54.                 if(t2){
  55.                         cout<<t2;
  56.                         total %= 600;
  57.                 }else if(t1){
  58.                         cout<<t2;
  59.                         total %= 600;
  60.                 }
  61.  
  62.                 t3 = total / 60;
  63.                 cout<<t3;
  64.                 if(t3){
  65.                         total %= 60;
  66.                 }
  67.                 cout<<":";
  68.  
  69.                 t4 = total /10;
  70.                 cout<<t4;
  71.                 if(t4){
  72.                         total %= 10;
  73.                 }
  74.  
  75.                 t5 = total;
  76.                 cout<<total;
  77.  
  78.                 cout<<" min/km"<<endl;
  79.         }
  80. }

回复 "hdoj1036简单题、格式化输出"

这儿你可以回复上面这条便签

captcha