题解 | #提取不重复的整数# 在牛客学习笔试代码

提取不重复的整数

https://www.nowcoder.com/practice/253986e66d114d378ae8de2e6c4577c1

import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException {
        InputStream in = System.in;
        int num = 0;
        int c;
        while ((c = in.read()) != '\n') {
            if (c >= '0' && c <= '9') {
                num = num * 10 + (c - '0'); // 将字符转换为数字
            }
        }

        int result = 0;
        boolean[] seen = new boolean[10]; // 记录数字是否出现过

        while (num > 0) {
            int digit = num % 10;
            if (!seen[digit]) {
                result = result * 10 + digit;
                seen[digit] = true;
            }
            num = num / 10;
        }

        System.out.println(result);
    }
}

全部评论

相关推荐

12-10 22:48
武汉大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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