回答編集履歴

1

rewrite answer

2017/09/05 02:35

投稿

yohhoy
yohhoy

スコア6191

test CHANGED
@@ -1,8 +1,8 @@
1
- 正値に限れば `(int)(n + 0.5)` で整数への切り上げ計算ます。
1
+ 整数`n`の20%、つまり`n / 5`の切り上げは`(n + 4) / 5`(全てint型で計算)ります。
2
2
 
3
3
 
4
4
 
5
- ```C
5
+ ```
6
6
 
7
7
  #include <assert.h>
8
8
 
@@ -16,7 +16,9 @@
16
16
 
17
17
  assert(0 <= n);
18
18
 
19
+ int r = (n + 4) / 5; // ceil(n * 20%)
20
+
19
- return (int)(n * 1.2 + 0.5);
21
+ return n + r;
20
22
 
21
23
  }
22
24
 
@@ -28,7 +30,7 @@
28
30
 
29
31
  for (int i = 1; i <= 100; i++)
30
32
 
31
- printf("%d->%d\n", i, ceil20up(i));
33
+ printf("%d -> %.1f -> %d\n", i, i * 1.2, ceil20up(i));
32
34
 
33
35
  }
34
36