题解 | #整型存储#

整型存储

https://www.nowcoder.com/practice/5391e3118a5f419d8198264c5a8c805a

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Reverse reverse = new Reverse();
        while (sc.hasNextInt()) {
            int num = sc.nextInt();
            if (num == 0 || !reverse.add(num)) {
                reverse.print();
                break;
            }
        }
    }
    private static class Reverse {
        private int[] nums, reverse_nums;
        private int size;
        private Reverse() {
            nums = new int[10];
            reverse_nums = new int[10];
        }
        private boolean add(int num) {
            if (size == 10) {
                return false;
            }
            nums[size] = num;
            reverse_nums[size] = reverse(num);
            size++;
            return true;
        }
        private int reverse(int num) {
            int reverse_num = 0;
            while (num != 0) {
                reverse_num = reverse_num * 10 + num % 10;
                num /= 10;
            }
            return reverse_num;
        }
        private void print() {
            for (int i = 0; i < size; i++) {
                System.out.println(nums[i] + " " + reverse_nums[i]);
            }
        }
    }
}

全部评论

相关推荐

我也曾抱有希望:说的好直白
点赞 评论 收藏
分享
10-25 12:05
已编辑
湖南科技大学 Java
若梦难了:我有你这简历,已经大厂乱杀了
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务