码迷,mamicode.com
首页 > 其他好文 > 详细

CF898A Rounding

时间:2018-02-11 12:39:47      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:can   target   strlen   color   reset   --   ima   article   cas   

题意翻译

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

题目描述

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

For example, if n=4722n=4722 answer is 47204720 . If n=5n=5 Vasya can round it to 00 or to 1010 . Both ways are correct.

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

输入输出格式

输入格式:

 

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

 

输出格式:

 

Print result of rounding nn . 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=5n=5 . Nearest integers, that ends up with zero are 00 and 1010 . Any of these answers is correct, so you can print 00 or 1010 .

技术分享图片

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int tot;
char n[20];
int num[20];
int main(){
    cin>>n;
    int len=strlen(n);
    for(int i=len-1;i>=0;i--)    num[len-i]=n[i]-0;
    if(num[1]>=5){ num[2]++;num[1]=0;if(len==1)    len++; }
    else num[1]=0;
    for(int i=1;i<=len;i++)
        if(num[i]>=10){
            if(i==len)    len++;
            num[i+1]+=1;
            num[i]%=10;
        }
    for(int i=len;i>=1;i--)    cout<<num[i];
}

 

CF898A Rounding

标签:can   target   strlen   color   reset   --   ima   article   cas   

原文地址:https://www.cnblogs.com/cangT-Tlan/p/8440782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!