这道题目的本质还是对于四舍五入的理解。

对于一个位上的数 $x$,若 $0 \le x \le 4$,则会舍到 $0$;若 $5 \le x \le 9$,则会进一位到 $10$。而题目中的零数 $k$ 为 $0-9$,分别代表个、十、百 $\cdots$。

因此可以为一个数的一半为界进行判断,四舍只要抹去余数,五入则需加上除余数不足的部分,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#define init(x) memset (x,0,sizeof (x))
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = 1e5 + 5;
const int MOD = 1e9 + 7;
ll read ();
ll a[15] = {1,10,100,1e3,1e4,1e5,1e6,1e7,1e8,1e9};//先存入题目中的数
int main ()
{
//freopen (".in","r",stdin);
//freopen (".out","w",stdout);
ll c = read ();
ll k = read ();
if (c % a[k] < (a[k] >> 1)) printf ("%d\n",c - c % a[k]);//减去余数
else printf ("%d\n",c - c % a[k] + a[k]);//加上剩余部分
return 0;
}
ll read ()
{
ll s = 0;int f = 1;
char ch = getchar ();
while ((ch < '0' || ch > '9') && ch != EOF)
{
if (ch == '-') f = -1;
ch = getchar ();
}
while (ch >= '0' && ch <= '9')
{
s = s * 10 + ch - '0';
ch = getchar ();
}
return s * f;
}