HDOJ 1266 Reverse Number(数字反向输出题)

Problem Description
Welcome to 2006’4 computer college programming contest!

Specially, I give my best regards to all freshmen! You are the future of HDU ACM! And now, I must tell you that ACM problems are always not so easy, but, except this one… Ha-Ha!

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;
2. The reverse number of a negative integer is negative, for example, reverse (-12) = -21;
3. The reverse number of an integer ending with 0 is described as example, reverse (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, and then n 32-bit integers follow.

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

注意:前导0的情况!
例:
输入:
3
-0012560020
00000
00205
输出为:
-2006521
0
502

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            String str = sc.next();
            int instr = Integer.parseInt(str);
            //System.out.println(instr);
            str = Integer.toString(instr);

            //System.out.println(str);
            if (str.charAt(0) == '-') {
                System.out.print("-");
                int k = 0;
                boolean isOne=false;

                //System.out.println(str.length()+"aaa");

                for (int i = str.length() - 1; i >= 1; i--) {
                    //System.out.println("a:  "+str.charAt(i));
                    if(str.charAt(i)!='0'&&!isOne){
                        //System.out.println("++ "+str.charAt(i));
                        isOne=true;
                    }

                    if (isOne) {
                        System.out.print(str.charAt(i));
                        k++;
                    }
                }
                for (int i = 1; i < str.length() - k; i++) {
                    System.out.print(0);
                }
                System.out.println();
            } else {
                int k = 0;
                boolean isOne=false;
                for (int i = str.length() - 1; i >= 0; i--) {
                    if(str.charAt(i)!='0'&&!isOne){
                        isOne=true;
                    }

                    if (isOne) {
                        System.out.print(str.charAt(i));
                        k++;

                    }
                }

                for (int i = 0; i < str.length() - k; i++) {
                    System.out.print(0);
                }
                System.out.println();

            }

        }
    }

}
时间: 2024-10-23 16:32:10

HDOJ 1266 Reverse Number(数字反向输出题)的相关文章

HDU 1266 Reverse Number (water ver.)

http://acm.hdu.edu.cn/showproblem.php?pid=1266 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Description Welcome to 2006'4 computer college programming contest! Specially, I give my best regards to all fresh

JavaScript中的Number数字类型学习笔记_基础知识

使用IEEE754格式来表示整数和浮点数值.浮点数值:该数值中必须包含一个小数点,并且小数点后面必须至少有一位数字.浮点数值需要内存空间是保存整数值的两倍.最高精度是17为小数,但在进行算术运算时其精度远远不如整数. 各种数值类型:十进制,八进制(在严格模式下无效),十六进制 八进制字面量的第一位必须是0,然后是八进制数字序列(0~7).如果字面值中的数值超出了范围,那么前导0将被忽略,后面的数值将被当做十进制数来解析 070//56 079//79 十六进制字面值的前两位必须是0x,后跟十六进

HDOJ 1018 Big Number(大数位数公式)

Problem Description In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of

HDOJ 1212 Big Number

Problem Description As we know, Big Number is always troublesome. But it's really important in our ACM. And today, your task is to write a program to calculate A mod B. To make the problem easier, I promise that B will be smaller than 100000. Is it t

HDOJ(HDU) 2178 猜数字(题意有点难理解、、、)

Problem Description A有1数m,B来猜.B每猜一次,A就说"太大","太小"或"对了" . 问B猜n次可以猜到的最大数. Input 第1行是整数T,表示有T组数据,下面有T行 每行一个整数n (1 ≤ n ≤ 30) Output 猜n次可以猜到的最大数 Sample Input 2 1 3 Sample Output 1 7 这个题目我总感觉题意没说明白,没办法.参考了一下网上的题意. 题意就是: 最多猜n次一定可以猜到1

javascript学习笔记(四) Number 数字类型_基础知识

toFixed() 方法指定小数位个数 toExponential() 方法 用科学计数法表示数,参数指定小数位个数 toPrecision() 方法自动判断调用toFixed()或toExponential()方法,参数指定所有数的位数 复制代码 代码如下: var num = 99; alert(toFixed(2)); //99.00 alert(toExponential(1)); //9.0e+1 alert(toPrecision(1)); //9.0e+1 alert(toPrec

PL/SQL Number数字类型函数_oracle

ABS(x) 函数,此函数用来返回一个数的绝对值. ACOS(x)函数,返回X的反余弦值.X范围从1到-1,输入值从0到派,以弧度为单位. ASIN(x)函数,返回X的反正弦值.X范围从1到-1,输入值从-PI/2到PI/2,以弧度为单位. ATAN(x)函数,返回X的反正切值.输入值从-PI/2到PI/2,以弧度为单位. BITAND(x,y)函数,返回X和Y的与结果.X和Y必须为非负整数.注意没有BITOR函数,但是在UTL_RAW包中有用      于RAW值的位操作符. CEIL(x)函

js 限制文本框只能输入数字 汉字 字母与禁止输空格代码

1.文本框只能输入数字代码(小数点也不能输入)  代码如下 复制代码     <input onkeyup="this.value=this.value.replace(/D/g,'')" onafterpaste="this.value=this.value.replace(/D/g,'')"> 2.只能输入数字,能输小数点.  代码如下 复制代码     <input onkeyup="if(isNaN(value))execComm

php短网址和数字之间相互转换的方法

 这篇文章主要介绍了php短网址和数字之间相互转换的方法,涉及php操作字符串的技巧,具有一定参考借鉴价值,需要的朋友可以参考下     本文实例讲述了php短网址和数字之间相互转换的方法.分享给大家供大家参考.具体实现方法如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 <?php /** * 将数字转为短网址代码 * * @param