CF898A Rounding

it2022-05-07  3

 

 

题意翻译

给你一个数字,将其“四舍六入”,末尾为5舍去或进位都可,求最终的数字。

题目描述

Vasya has a non-negative integer n n n . He wants to round it to nearest integer, which ends up with 0 0 0 . If n n n already ends up with 0 0 0 , Vasya considers it already rounded.

For example, if n=4722 n=4722 n=4722 answer is 4720 4720 4720 . If n=5 n=5 n=5 Vasya can round it to 0 0 0 or to 10 10 10 . Both ways are correct.

For given n n n find out to which integer will Vasya round it.

输入输出格式

输入格式:

The first line contains single integer n n n ( 0<=n<=109 0<=n<=10^{9} 0<=n<=109 ) — number that Vasya has.

输出格式:

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

输入输出样例

输入样例#1:  5 输出样例#1:  0 输入样例#2:  113 输出样例#2:  110 输入样例#3:  1000000000 输出样例#3:  1000000000 输入样例#4:  5432359输出样例#4:  5432360

说明

In the first example n=5 n=5 n=5 . Nearest integers, that ends up with zero are 0 0 0 and 10 10 10 . Any of these answers is correct, so you can print 0 0 0 or 10 10 10 .

虽然只是一道四舍五入的水题我没有看数据范围...于是字符串处理+模拟 来写233思路(c++)    1.首先读入字符串     2.将字符串倒序存入另一个数组    3.此时最后一位数即新数组的第一位    4.判断是否大于等于五(ps:我计算的四舍五入)    5.如果大于五的话...        a.先判断是否为一位数(防止“9”这种数据)         b.将最后一位数字变为'0';        c.再进行循环(判断下一位是否为9?是 则变为0再继续  否 则这位加一然后退出循环);         d.倒序输出;    6.如果小于五...就只把末位变为0,然后倒序输出; 1 #include<bits/stdc++.h> 2 using namespace std; 3 int main(){ 4 char s[10000]; 5 char ss[10000]; 6 cin>>s; 7 int len=strlen(s); 8 for(int i=len-1,j=0;i>=0;i--,j++){ 9 ss[j]=s[i]; 10 } 11 if(ss[0]>='5') { 12 ss[0]='0'; 13 if(len==1) { 14 ss[len+1]='1'; 15 cout<<ss[len+1]; 16 } 17 for(int i=1;i<=len-1;i++){ 18 if (ss[i]=='9') { 19 ss[i]='0'; 20 continue; 21 } 22 if(ss[i]!='9') { 23 ss[i]+=1; 24 break; 25 } 26 } 27 for(int i=len-1;i>=0;i--){ 28 cout<<ss[i]; 29 } 30 return 0; 31 } 32 if(ss[0]<='4') { 33 ss[0]='0'; 34 for(int i=len-1;i>=0;i--){ 35 cout<<ss[i]; 36 } 37 return 0; 38 } 39 40 return 0; 41 }

 

 

转载于:https://www.cnblogs.com/luv-letters/p/8464966.html


最新回复(0)