代码之家  ›  专栏  ›  技术社区  ›  Bruno

c一个时间跨度内的时间跨度计数

  •  3
  • Bruno  · 技术社区  · 14 年前

    我想知道用C语言做这个的方法#

    假设我有两个时间段:ts1是3h,ts2是12h。

    计算TS1在TS2中可以进入多少次的最快方法是什么? 在这种情况下,输出将是4。

    如果ts1是8天,ts2是32天,它也会返回4天。

    4 回复  |  直到 14 年前
        1
  •  8
  •   Hans Passant    14 年前

    是的,使用整数除法。但关键在于细节,一定要使用TimeSpan的整数属性来避免溢出和舍入问题:

     int periods = (int)(TS1.Ticks / TS2.Ticks);
    
        2
  •  8
  •   Matthew Flaschen    14 年前

    整数除法?

    (int) TS1.TotalMilliseconds/(int) TS2.TotalMilliseconds;
    
        3
  •  4
  •   Jim Mischel    14 年前

    你可以把 TotalMilliseconds 从一个到另一个。即:

    double times = TS2.TotalMilliseconds / TS1.TotalMilliseconds
    
        4
  •  2
  •   BrokenGlass    14 年前

    int count=(int)(ts2.totalseconds/ts1.totalseconds);