您的位置:首页 > 其它

hdu 1266 Reverse Number

2015-07-20 21:58 337 查看
题意:将一个正整数颠倒,如后输出这个数。

注意:整数中间的0是合法的。

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		while (n-- > 0) {
			String s = sc.next();
			String result = "";
			char c;
			int index = s.length() - 1;// 扩大作用域
			for (int i = index; i > 0; i--) {// 第一个不扫描
				c = s.charAt(i);// c只是一个零时变量,方便编程
				if (c != '0') {
					index = i;
					break;
				}
			}
			for (int i = index; i > 0; i--) {// 第一个不扫描
				result += s.charAt(i);
			}
			c = s.charAt(0);
			if (c == '-') {
				result = "-" + result;// 负数符号放前面
			} else {
				result += c;// 整数放后面
			}
			// 后面加0
			for (int j = index; j < s.length() - 1; j++) {
				result += '0';
			}
			System.out.println(result);
		}
	}

}


Reverse Number

颠倒数字

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6078 Accepted Submission(s): 2774



Problem Description
Welcome to 2006'4 computer college programming contest!
欢迎来到2006年4月大学生计算机程序设计竞赛!

Specially, I give my best regards to all freshmen!
特别的,我给每一位大一新生给予真诚的致谢!
You are the future of HDU ACM!
也许你将改变HDU ACM的未来!
And now, I must tell you that ACM problems are always not so easy, but, except this one... Ha-Ha!

现在,我必须告诉你 HDU ACM的题目并不简单,但是,这题一定简单。。。哈哈!

Give you an integer; your task is to output its reverse number. Here, reverse number is defined as follows:
给你一个整数,你的任务是颠倒这个数字,然后输出。颠倒规则如下:

1. The reverse number of a positive integer ending without 0 is general reverse, for example, reverse (12) = 21;
一般颠倒方式:把一个尾部没有0的正整数颠倒,例如12颠倒后就是21;

2. The reverse number of a negative integer is negative, for example, reverse (-12) = -21;
负数颠倒:例如-12颠倒后就是-21;

3. The reverse number of an integer ending with 0 is described as example, reverse (1200) = 2100.

尾部有0:例如1200颠倒2100。

Input
Input file contains multiple test cases.
输入文件将包含多组测试事件。
There is a positive integer n (n<100) in the first line, which means the number of test cases,
输入的第一行是一个正整数,小于100,表示测试事件的个数,
and then n 32-bit integers follow.

然后如如32位地整数(c语言中的int)。

Output
For each test case, you should output its reverse number, one case per line.

对于每一个测试事件,你应该输出颠倒后的结果,一行一个答案。

Sample Input
3
12
-12
1200




Sample Output
21
-21
2100




Author
lcy
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: