Codeforces Round#451 Div.2 A题解析

题目:

A. Rounding
Vasya has a non-negative integer n. He wants to round it to nearest integer, which ends up with 0. If n already ends up with 0, Vasya considers it already rounded.
For example, if n = 4722 answer is 4720. If n = 5 Vasya can round it to 0 or to 10. Both ways are correct.
For given n find out to which integer will Vasya round it.

Input

The first line contains single integer n (0 ≤ n ≤ 109) — number that Vasya has.

Output

Print result of rounding n. Pay attention that in some cases answer isn't unique. In that case print any correct answer.

例子:
Input
5
Output
0
Input
113
Output
110
Input
1000000000
Output
1000000000
Input
5432359
Output
5432360

理解:当初刚看到的时候以为找离的最近的是10的倍数的整数,所以当初第一个相反就是先判断其最近的那个10的倍数的整数是哪一个,因为题目中说5这种到大的那个10的倍数的整数和小的距离一样,所以输出哪一个都行,所以这时候把其随便归到一个情况就行,然后在进行是将这个数加到大的还是减到小的;所以我还算了距离,于是就把代码弄的很麻烦;
后来再思考的话,总结一下这类情况的判断:就是如果是类似进位(将末尾给变成0),就是四舍五入这种的感觉的,这个就是将个位给进位,就可以采用四舍五入的方法,即对最后一位加5,0.5,0.05这样的,然后除以10就是舍去,再乘10就是进位后的数了;

原代码:

  #include<stdio.h>
  
  int main()
  {
      long long  n,a;
      scanf("%lld",&n);
      if(n%10==0)
          printf("%lld\n",n);
      else
      {
          a=n%10;
          if(a<=5)
              printf("%lld\n",n-a);
          else
              printf("%lld\n",n+10-a);
      }

      return 0;
  }

别人更加简洁的代码:

  #include <stdio.h>

  int main () {
      int d; scanf("%d", &d);
      printf("%d", (d+5)/10*10);
      return 0;
  }

评论

暂无评论