Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >POJ 刷题系列:1503 Integer Inquiry

POJ 刷题系列:1503 Integer Inquiry

作者头像
用户1147447
发布于 2018-01-02 02:34:39
发布于 2018-01-02 02:34:39
52400
代码可运行
举报
文章被收录于专栏:机器学习入门机器学习入门
运行总次数:0
代码可运行

POJ 刷题系列:1503 Integer Inquiry

传送门:POJ 1503 Integer Inquiry

题意:

实现VeryLongIntegers两数相加算法。

思路: 因为字符长度可达100位,自然不能用long,int这些基本类型实现。所以采用字符串逐个相加进位来实现,熟悉进位法即可。

代码如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Map;
import java.util.StringTokenizer;

public class Main{

    String INPUT = "./data/judge/201712/P1503.txt";

    public static void main(String[] args) throws IOException {
        new Main().run();
    }

    String add(String s1, String s2) {
        char[] num1 = s1.toCharArray();
        char[] num2 = s2.toCharArray();

        int n1 = num1.length;
        int n2 = num2.length;

        int maxLen = Math.max(n1, n2);
        StringBuilder sb = new StringBuilder();
        int rem = 0;
        for (int i = 1; i <= maxLen; ++i) {
            int digit1 = n1 - i < 0 ? 0 : num1[n1 - i] - '0';
            int digit2 = n2 - i < 0 ? 0 : num2[n2 - i] - '0';
            int sum = digit1 + digit2 + rem;
            rem = sum / 10;
            sb.append(sum % 10);
        }
        if (rem != 0) sb.append(rem);
        return sb.reverse().toString();
    }

    void read() {
        String ans = "0";
        while (true) {
            String num = ns();
            if (num.equals("0")) break;
            ans = add(num, ans);
        }
        out.println(ans);
    }

    FastScanner in;
    PrintWriter out;

    void run() throws IOException {
        boolean oj;
        try {
            oj = ! System.getProperty("user.dir").equals("F:\\oxygen_workspace\\Algorithm");
        } catch (Exception e) {
            oj = System.getProperty("ONLINE_JUDGE") != null;
        }

        InputStream is = oj ? System.in : new FileInputStream(new File(INPUT));
        in = new FastScanner(is);
        out = new PrintWriter(System.out);
        long s = System.currentTimeMillis();
        read();
        out.flush();
        if (!oj){
            System.out.println("[" + (System.currentTimeMillis() - s) + "ms]");
        }
    }

    public boolean more(){
        return in.hasNext();
    }

    public int ni(){
        return in.nextInt();
    }

    public long nl(){
        return in.nextLong();
    }

    public double nd(){
        return in.nextDouble();
    }

    public String ns(){
        return in.nextString();
    }

    public char nc(){
        return in.nextChar();
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;
        boolean hasNext;

        public FastScanner(InputStream is) throws IOException {
            br = new BufferedReader(new InputStreamReader(is));
            hasNext = true;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    hasNext = false;
                    return "##";
                }
            }
            return st.nextToken();
        }

        String next = null;
        public boolean hasNext(){
            next = nextToken();
            return hasNext;
        }

        public int nextInt() {
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return Integer.parseInt(more);
        }

        public long nextLong() {
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return Long.parseLong(more);
        }

        public double nextDouble() {
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return Double.parseDouble(more);
        }

        public String nextString(){
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return more;
        }

        public char nextChar(){
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return more.charAt(0);
        }
    }

    static class D{

        public static void pp(int[][] board, int row, int col) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < row; ++i) {
                for (int j = 0; j < col; ++j) {
                    sb.append(board[i][j] + (j + 1 == col ? "\n" : " "));
                }
            }
            System.out.println(sb.toString());
        }

        public static void pp(char[][] board, int row, int col) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < row; ++i) {
                for (int j = 0; j < col; ++j) {
                    sb.append(board[i][j] + (j + 1 == col ? "\n" : " "));
                }
            }
            System.out.println(sb.toString());
        }
    }

    static class ArrayUtils {

        public static void fill(int[][] f, int value) {
            for (int i = 0; i < f.length; ++i) {
                Arrays.fill(f[i], value);
            }
        }

        public static void fill(int[][][] f, int value) {
            for (int i = 0; i < f.length; ++i) {
                fill(f[i], value);
            }
        }

        public static void fill(int[][][][] f, int value) {
            for (int i = 0; i < f.length; ++i) {
                fill(f[i], value);
            }
        }
    }

    static class Num{
        public static <K> void inc(Map<K, Integer> mem, K k) {
            if (!mem.containsKey(k)) mem.put(k, 0);
            mem.put(k, mem.get(k) + 1);
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-12-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
POJ 刷题系列:3295. Tautology
该文介绍了计算几何的一些基本概念和算法,包括线段、三角形、四面体、立方体、圆柱体和球体等。文章还介绍了图的遍历算法,包括深度优先搜索、广度优先搜索和迪杰斯特拉算法等。此外,文章还介绍了几个著名的计算机算法,如快速排序、归并排序和堆排序等。
用户1147447
2017/12/29
5270
POJ 刷题系列:3295. Tautology
POJ 刷题系列:2262. Goldbach's Conjecture
用户1147447
2018/01/02
4380
POJ 刷题系列:2262. Goldbach's Conjecture
POJ 刷题系列:1840. Eqs
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/79089056
用户1147447
2019/05/26
3340
POJ 刷题系列:3080. Blue Jeans
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/79057958
用户1147447
2019/05/26
3850
POJ 刷题系列:1068. Parencodings
用户1147447
2017/12/29
4770
POJ 刷题系列:1068. Parencodings
POJ 刷题系列:2993. Emag eht htiw Em Pleh
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/78866516
用户1147447
2019/05/26
2970
POJ 刷题系列:2632. Crashing Robots
题意: 在一张地图(A x B)中给出N个robot的位置和初始移动方向,以及M条指令,任意两个robot不能同时移动,问是否存在非法的crash状态(撞墙,撞其他robot)。 思路:
用户1147447
2017/12/29
4870
POJ 刷题系列:2632. Crashing Robots
POJ 刷题系列:3299. Humidex
本文介绍了在 Java 并发编程中,通过使用 CountDownLatch、CyclicBarrier、Semaphore 三个类来实现线程同步等待和同步解除等待的机制。同时,还介绍了在 Spring Boot 中如何利用 AQS (AbstractQueuedSynchronizer) 实现线程安全的 WebClient 多任务并发执行。通过这些技术,可以有效地提高 Java 程序的性能和稳定性。
用户1147447
2018/01/02
5080
POJ 刷题系列:3299. Humidex
POJ 刷题系列:1573. Robot Motion
该文介绍了在Java中如何实现深度优先搜索算法。首先介绍了深度优先搜索算法的基本概念,然后详细讲解了如何在Java中实现该算法。最后,通过实例演示了如何使用深度优先搜索算法解决迷宫问题。
用户1147447
2017/12/29
4650
POJ 刷题系列:1573. Robot Motion
POJ 刷题系列:2965. The Pilots Brothers' refrigerator
根据给定的文章内容,撰写摘要总结。
用户1147447
2018/01/02
4940
POJ 刷题系列:2965. The Pilots Brothers' refrigerator
POJ 刷题系列:2109. Power of Cryptography
本文通过分析题目2109,讨论了Power of Cryptography在解题中的应用,并提供了具体的代码实现。通过枚举题目中给出的矩阵,将整数数组转化为二叉树结构,利用二叉树的性质简化求解过程,并使用递归算法完成填充。最终通过实例演示了该方法的可行性。
用户1147447
2017/12/29
4050
POJ 刷题系列:2109. Power of Cryptography
POJ 刷题系列:2299. Ultra-QuickSort
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/79059221
用户1147447
2019/05/26
3100
POJ 刷题系列:1753. Flip Game
用户1147447
2018/01/02
7420
POJ 刷题系列:1753. Flip Game
POJ 刷题系列:3006. Dirichlet's Theorem on Arithmetic Progressions
用户1147447
2018/01/02
5320
POJ 刷题系列:3006. Dirichlet's Theorem on Arithmetic Progressions
挑战程序竞赛系列(79):4.3 2-SAT(3)
摘要总结:本文主要介绍了一种简化版的有限状态机实现,它使用Java语言实现,基于一个自定义的Util类进行状态转换,支持基于关键字驱动的状态机执行。该实现方式简化了有限状态机在实际问题中的应用,提高了开发效率和代码质量。
用户1147447
2018/01/02
5420
挑战程序竞赛系列(79):4.3  2-SAT(3)
2240. Arbitrage
思路: 在图模型中找负环即可。此处符合负环的特征如下:在负环上的顶点会不断更新最大值,所以在N轮没有停止更新就说明存在了负环。
用户1147447
2019/05/26
3600
POJ 刷题系列:2996. Help Me with the Game
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/78864960
用户1147447
2019/05/26
3570
POJ 刷题系列:2255. Tree Recovery
该文介绍了如何通过递归思想,使用一个递归函数构建一棵树,并利用一个数组来记录每个节点的值。在遍历树的过程中,每当遍历到一个节点时,都会将它的值写入数组中。最后,将数组转换为字符串,即可得到一棵完整的二叉树。
用户1147447
2018/01/02
6330
POJ 刷题系列:2255. Tree Recovery
相关推荐
POJ 刷题系列:3295. Tautology
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档