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;
  }
相關文章
相關標籤/搜索