1. 问题描述
反转一个只有3位数的整数。
2. 示例
输入num=123,输出321,输入num=100,输出1.
3. 代码示例
3.1 python
1 class Solution: 2 def reverseInt(self,num): 3 if isinstance(num,int) and num < 999 and num > 99: 4 hundreds = int(num/100) # 百位 5 tens= int(num%100/10) # 十位 6 individual = int(num%10) 7 return individual * 100 + tens * 10 + hundreds 8 else: 9 return "" 10 if __name__ == '__main__': 11 solution = Solution() 12 while True: 13 num = int(input("请输入一个3位整数: ")) 14 print(solution.reverseInt(num))
运行效果:
3.2 Java
1 import java.util.Scanner; 2 3 public class Days001 { 4 5 public int reverseInt(int num) { 6 if (num > 99 && num < 1000) { 7 int hundreds = num / 100; // 百位 8 int tens = num % 100 / 10; // 十位 9 int individual = num % 10; // 个位 10 return individual * 100 + tens * 10 + hundreds; 11 } else { 12 System.out.println("输入的数据超范围"); 13 return 0; 14 } 15 16 } 17 18 public static void main(String[] args) { 19 Days001 solutionReverse3Int001 = new Days001(); 20 while (true) { 21 System.out.print("输入一个3位整数:"); 22 Scanner scanner = new Scanner(System.in); 23 int num = scanner.nextInt(); 24 int reverseNum = solutionReverse3Int001.reverseInt(num); 25 System.out.println(reverseNum); 26 } 27 } 28 }
标签:__,10,return,int,反转,整数,001,num,100 From: https://www.cnblogs.com/allenxx/p/17855002.html