题解 | #数字颠倒#
数字颠倒
http://www.nowcoder.com/practice/ae809795fca34687a48b172186e3dafe
题目 数字颠倒
日期 2022/03/17
思路
用取余法将数字从后开始加入一个StringBuffer
代码
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println(numTostr(num));
}
public static String numTostr(int num){
StringBuffer sb = new StringBuffer();
if(num==0)
return "0";
while(num!=0){
int yu = num%10;
char ch = (char) ('0'+yu);
sb.append(ch);
num/=10;
//System.out.println("yu:"+yu+" char:"+('0'+yu)+" sb:"+sb+" num:"+num);
}
return sb.toString();
}
}