2013
11-12

# Rounders

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

For each integer in the input, display the rounded integer on its own line.

Note: Round up on fives.

9
15
14
4
5
99
12345678
44444445
1445
446

20
10
4
5
100
10000000
50000000
2000
500



方法一:
import java.math.BigInteger;
import java.util.*;

public class Main
{

public static void main(String args[])
{
int n, s, i;
Scanner cin = new Scanner( System.in );
n = cin.nextInt();
while( n-- != 0 ) {
s = cin.nextInt();
for( i=10; i<=100000000; i*=10 )
if( s >= i ) {
s = (s+i/2)/i*i;
}
System.out.println( s );
}
return;
}
}

//* @author 洪晓鹏<[email protected]/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */>
import java.util.Scanner;

public class Main{

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
for (int i = 0; i < n; i++) {
String s = in.nextLine();
int len = s.length();
int carry = 0;
if (len == 1) {
System.out.println(s);
continue;
}
for (int j = len - 1; j > 0; j--) {
if ((s.charAt(j) - '0' + carry) >= 5)
carry = 1;
else
carry = 0;
}
int f = s.charAt(0) - '0' + carry;
String result = "" + f;
for (int j = 0; j < len - 1; j++) {
result += "0";
}
System.out.println(result);
}
}

}