2022.10.23 ACM-ICPC个人训练赛题解Problem.B

 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|∣YX∣."

      这里要求找的结果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

InputcopyOutputcopy
2048 22100

XX changes as 2048 \rightarrow 2050 \rightarrow 21002048→2050→2100 by the operations.

Sample 2

InputcopyOutputcopy
1 150

Sample 3

InputcopyOutputcopy
999 31000

Sample 4

InputcopyOutputcopy
314159265358979 12314000000000000

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ㄒ)/~~)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值