Codeforces Round#451 Div.2 A题解析

题目:ide

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.rest

Inputcode

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

Outputio

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就是进位后的数了;di

原代码:思考

#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;
  }

别人更加简洁的代码:ant

#include <stdio.h>

  int main () {
      int d; scanf("%d", &d);
      printf("%d", (d+5)/10*10);
      return 0;
  }
相关文章
相关标签/搜索