zoukankan      html  css  js  c++  java
  • hdu_1036(取整和格式控制)

    题意很简单,求平均时间

    复习一下如何取整

    (int) fl 是直接向下取整  ==  floor(fl)

    向上取整 (int)(fl+1)  == ceil(fl)

    四舍五入 (int)(fl+0.5)

    技巧:输入时候决定什么时候读入int,什么时候读入char有时候可以省很多事情

    代码:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 int hour, minutes, second;
     6 bool fl;int t;
     7 char tm[7];
     8 void solve(){
     9     if(tm[0]=='-'){fl = 0; return;}
    10     hour += tm[0]-'0';
    11     minutes = minutes + (tm[2]-'0')*10+(tm[3]-'0');
    12     second = second + (tm[5]-'0')*10+(tm[6]-'0');
    13 }
    14 void pout(){
    15     if(t/100==0) printf(" ");
    16     if(t/10==0) printf(" ");
    17     printf("%d",t);
    18     printf(": %d:",minutes);
    19     if(second/10==0) printf("0");
    20     printf("%d min/km
    ",second);
    21 }
    22 int main()
    23 {
    24     int n;  double d;
    25     char tt[3];
    26     while(~scanf("%d%lf",&n,&d))
    27     {
    28         while(~scanf("%d",&t)){
    29             hour = 0, minutes = 0; second = 0;
    30             fl = 1;
    31             for(int i = 0; i < n; i++){
    32                 scanf("%s",&tm);
    33                 solve();
    34             }
    35             if(fl==0){
    36                 if(t/100==0) printf(" ");
    37                 if(t/10==0) printf(" ");
    38                 printf("%d",t);
    39                 printf(": -
    ");
    40                 continue;
    41             }
    42             double allminutes = hour*60+minutes+(double)second/60;
    43             //printf("min = %lf
    ",allminutes);
    44             allminutes /= d;
    45             minutes = (int)allminutes;
    46             second =(int)((allminutes-(int)allminutes)*60+0.5);
    47             if(second==60){
    48                 second = 0;
    49                 minutes ++;
    50             }
    51             pout();
    52         }
    53     }
    54     return 0;
    55 }
  • 相关阅读:
    Tree
    a letter and a number
    A problem is easy
    connect设置超时的方法
    C++客户端访问Java服务端发布的SOAP模式的WebService接口
    gSoap的“error LNK2001: 无法解析的外部符号 _namespaces”解决方法
    先序序列和后序序列并不能唯一确定二叉树
    二叉树的非递归遍历
    web service,soap ,http,tcp,udp
    byte[]数组和int之间的转换
  • 原文地址:https://www.cnblogs.com/shanyr/p/7348931.html
Copyright © 2011-2022 走看看