题解 | #数字颠倒#

数字颠倒

http://www.nowcoder.com/practice/ae809795fca34687a48b172186e3dafe

/*
JAVA
两种解法:
1,对这个整数取模,依次得到个位数的值,拼接成字符串
2,把整数转成字符串,反向遍历。
*/
import java.util.Scanner;
//解法1
public class Main {
   public static void main(String[] args){
       Scanner in = new Scanner(System.in);
       int a = in.nextInt();
       StringBuffer sb = new StringBuffer();
       String result = "";
       while(a!=0){
           sb.append(a%10);
           a /= 10;
       }

       System.out.print(sb.toString());
   }
}

// //解法2
// public class Main {
//    public static void main(String[] args){
//        Scanner in = new Scanner(System.in);
//        int a = in.nextInt();
//        String str = String.valueOf(a);
//        for(int i= str.length() -1;i>=0;i--){
//         System.out.print(str.charAt(i));
//        }
//    }
// }
全部评论

相关推荐

06-07 19:59
门头沟学院 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务