Codeforces Round #340 (Div. 2) A. Elephant

http://codeforces.com/problemset/problem/617/A

题意:
大象步长可为1,2,3,4,5。到朋友家的距离为x。
求最少走几步。
能走几个5步走几个,剩下的无论是1,2,3,4,都只需一步

代码:

1
2
3
4
5
6
7
8
9
10
11
#include <bits/stdc++.h>
using namespace std;

int main() {
int x;
while (cin >> x) {
int res = (x % 5 == 0 ? x / 5 : x / 5 + 1);
printf("%d\n", res);
}
return 0;
};
1
2
int res = (x + 4) / 5;
int res = (x % 5 == 0 ? x / 5 : x / 5 + 1);

下面一句等于上面。但上面一句更优雅。
因为1-4加5除了后都是1。
如果不能理解就这么考虑,如果本来能整除+4不影响,
不能整除,无论余数是多少,结果都会比原来大1。

代码:

1
2
3
4
5
6
7
8
9
10
11
#include <bits/stdc++.h>
using namespace std;

int main() {
int x;
while (cin >> x) {
int res = (x + 4) / 5;
printf("%d\n", res);
}
return 0;
};