Problem Statement(
Given a non-negative integer XX, perform the following operation for i=1,2,\dots,Ki=1,2,…,K in this order and find the resulting XX.
[K*i*=1,2,…,*K*] 注意这里要重复操作k次昂;
-
Round
XX
off to the nearest
10^i10i
.
-
Formally, replace XX with YY that is "the largest multiple of 10^i10i that minimizes |Y-X|∣Y−X∣."
这里要求找的结果Y,要和原数据X的差最小
-
Here are some examples:
-
Rounding 273273 off to the nearest 10^2102 yields 300300.
-
Rounding 999999 off to the nearest 10^3103 yields 10001000.
-
Rounding 100100 off to the nearest 10^{10}1010 yields 00.
-
Rounding 10151015 off to the nearest 10^1101 yields 10201020.
-
-
Constraints
-
XX and KK are integers.
-
0 \le X < 10^{15}0≤X<1015
-
1 \le K \le 151≤K≤15
Input
The input is given from Standard Input in the following format:
XX KK
Output
Print the answer as an integer.
Sample 1
Inputcopy | Outputcopy |
---|---|
2048 2 | 2100 |
XX changes as 2048 \rightarrow 2050 \rightarrow 21002048→2050→2100 by the operations.
Sample 2
Inputcopy | Outputcopy |
---|---|
1 15 | 0 |
Sample 3
Inputcopy | Outputcopy |
---|---|
999 3 | 1000 |
Sample 4
Inputcopy | Outputcopy |
---|---|
314159265358979 12 | 314000000000000 |
XX may not fit into a 3232-bit integer type.
题意www:
输入k,对x进行x次操作,跟四舍五入差不多,找到离x最近的一个整十的数。
代码www:
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
long long x,k;
cin>>x>>k;
long long c=1;
for(int i=0; i<k; i++) { //题目当中有说昂,要执行k次哇(读题读题读题);
c*=10;
long long a=x/c; //这里的取整相当于四舍五入当中的舍啦;
long long b=ceil(1.0*x/c); //这里的向上取整,相当于四舍五入当中的入啦(注意ceil函数的操作对象是小数昂);
a*=c;
b*=c;
if(abs(a-x)<abs(b-x)) { //仔细读题昂,问的是哪个跟X相差更小噢;
x=a;
} else if(abs(a-x)>=abs(b-x)) {
x=b;
}
}
cout<<x<<'\n';
return 0;
}
就是酱紫(今天被这道题折腾的贼拉惨┭┮﹏┭┮ 没想到看懂了题意之后竟是这么简单/(ㄒoㄒ)/~~)