How to simplify fractions?

前端 未结 4 434
时光取名叫无心
时光取名叫无心 2021-01-13 02:05

How to simplify a fraction in C#? For example, given 1 11/6, I need it simplified to 2 5/6.

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-13 02:41

    int num = 11;
    int denom = 6;
    int unit = 1;
    while (num >= denom)
    {
      num -= denom;
      unit++;
    }
    

    Sorry I didn't fully understand that part about keeping track of the unit values.

提交回复
热议问题