Rounders
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 745, Accepted users: 687
Problem 10724 : No special judgement
Problem description
For a given number, if greater than ten, round it to the nearest ten, then (if that result is greater than 100) take the result and round it to the nearest hundred, then (if that result is greater than 1000) take that number and round it to the nearest thousand, and so on …
Input
Input to this problem will begin with a line containing a single integer n indicating the number of integers to round. The next n lines each contain a single integer x (0 <= x <= 99999999).
Output
For each integer in the input, display the rounded integer on its own line.
Note: Round up on fives.
Sample Input
9
15
14
4
5
99
12345678
44444445
1445
446
Sample Output
20
10
4
5
100
10000000
50000000
2000
500
分析
通过从个位向前不断进位判断最终的结果,用num记录进位的数目,从而决定输出多少个0
AC代码
#include<iostream>
using namespace std;
int main() {
int t, x,num,n;
cin>>t;
while(t--) {
cin>>n;
num=0;
while(n>=10){//如果n小于10,直接输出自己
x=n%10;//取个位
if(x>=5)//四舍五入
n=n/10+1;
else
n=n/10;
num++;//进位了加1
}
cout<<n;//如果n小于10,直接输出自己;
///如果大于n,即输出进位后的最高位的情况
while(num--){
cout<<"0";//输出0
}
cout<<endl;
}
}