加法,你会吗?

简介: 加法,你会吗?

题目

面试官:设计一个加法,满足100位的数字相加?

我:好的。(内心在想"有病吧!这面试官,问些没用的问题!怎么办,不会啊!")

面试官:20分钟哦,你先想,我倒杯水去。

我:(内心在想"水到慢点,一定要慢点啊!")

脑子里开始转:"字符串","拆散","倒叙","一位一位处理","长度不一样怎么处理?算最大位","进位怎么处理?","结果位数怎么定义?最大位数加1",啊啊啊啊,好烦,想回家!不管了,先按着这个路子一步一步走吧。

先打个比方:

整数A:8765876219

整数B:8882423

第一步先画出数据:

col 1 col 2 col 3 col 4 col 5 col 6 col 7 col 8 col 9 col 10 col 11 col 12
A: 9 1 2 6 7 8 5 6 7 8 0
col 1 col 2 col 3 col 4 col 5 col 6 col 7 col 8 col 9 col 10 col 11 col 12
B: 3 2 4 2 8 8 8 0 0 0 0

=

col 1 col 2 col 3 col 4 col 5 col 6 col 7 col 8 col 9 col 10 col 11 col 12
C: 0 0 0 0 0 0 0 0 0 0 0

结果先设定比最大一位多一位,还有如果位数小的,我们记为0,为了方便看我这边让格子数相等。

第一步先画出数据:

col 1 col 2 col 3 col 4 col 5 col 6 col 7 col 8 col 9 col 10 col 11 col 12
A: 9 1 2 6 7 8 5 6 7 8 0
col 1 col 2 col 3 col 4 col 5 col 6 col 7 col 8 col 9 col 10 col 11 col 12
B: 3 2 4 2 8 8 8 0 0 0 0

=

col 1 col 2 col 3 col 4 col 5 col 6 col 7 col 8 col 9 col 10 col 11 col 12
C: 0 0 0 0 0 0 0 0 0 0 0

结果先设定比最大一位多一位,还有如果位数小的,我们记为0,为了方便看我这边让格子数相等。

第三步计算:

col 1 col 2 col 3 col 4 col 5 col 6 col 7 col 8 col 9 col 10 col 11 col 12
A: 9 1 2 6 7 8 5 6 7 8 0
col 1 col 2 col 3 col 4 col 5 col 6 col 7 col 8 col 9 col 10 col 11 col 12
B: 3 2 4 2 8 8 8 0 0 0 0

=

col 1 col 2 col 3 col 4 col 5 col 6 col 7 col 8 col 9 col 10 col 11 col 12
C: 2 4 0 0 0 0 0 0 0 0 0

1+2+1=4,整数A加整数B再加进位的1是4,放入结果的第二的篮子中。

public static String bigNumSum(String bigNumA,String bigNumB) {
    //1. String转为char数组
    //bigNumA, bigNumB 翻转两个字符串后,个位十位相加比较方便
    char[] bigNumAChars = new StringBuffer(bigNumA).reverse().toString().toCharArray();
    char[] bigNumBChars = new StringBuffer(bigNumB).reverse().toString().toCharArray();
    //2. 在长的数组长度上加一来存结果
    int resultLength;
    if (bigNumAChars.length > bigNumBChars.length) {
        resultLength = bigNumAChars.length;
    }else {
        resultLength = bigNumBChars.length;
    }
    int[] result = new int[resultLength + 1];
    //3.对位相加
    for (int i = 0; i < resultLength; i++) {
        // 如果当前的i超过了某个数组的长度,就用0代替高位了,和另一个字符数组中的数字相加
        int Aint = i < bigNumAChars.length ? (bigNumAChars[i] - '0') : 0;
        int Bint = i < bigNumBChars.length ? (bigNumBChars[i] - '0') : 0;
        int temp = result[i];
        temp += Aint;
        temp += Bint;
        result[i] = temp;
        //判断是否进位
        if (temp >= 10) {
            result[i + 1] = temp / 10;
            result[i] = temp % 10;
        }
    }
    //4. 存储最后的结果
    StringBuffer sb = new StringBuffer();
    //判断最高位是0还是1, 0无需保存
    if (result[result.length - 1] == 1) {
        sb.append(1);
    }
    for (int i = result.length-2; i >= 0; i--) {
        sb.append(result[i]);
    }
    return sb.toString();
}
public static void main(String[] args) {
    System.out.println(bigNumSum("8765876219","8882423"));
}

640.png
敲完收工.....面试官怎么还不来?

相关文章
|
SQL XML Java
程序员都要懂的SQL防注入Mybatis框架SQL防注入
程序员都要懂的SQL防注入Mybatis框架SQL防注入
451 0
|
小程序 测试技术 uml
电商小程序01需求分析
电商小程序01需求分析
|
Linux C语言 C++
CentOS7安装gcc-5.4.0
CentoOS7 安装gcc
4464 0
CentOS7安装gcc-5.4.0
|
12月前
|
人工智能 数据可视化 API
从零开始搭建Qwen智能体:新手也能轻松上手指南
本文详细介绍了如何从零开始搭建Qwen-Agent智能体,涵盖环境配置、模型部署、RAG应用、工具调用、多Agent协作等内容,帮助开发者快速入门并构建自己的AI智能体。
|
人工智能 算法 API
掌握这6个要点,让AI从实验室应用到实际场景
三桥君分享AI产品经理如何将技术落地应用,涵盖找准痛点、数据策略、技术转化、价值体现等关键点,助力AI产品从实验室走向实际场景,实现真正的商业价值。
456 8
|
Linux 索引
linux命令—ls
`ls` 是 Linux 系统中用于列出目录内容的基础命令,功能强大且使用频率极高。它可以帮助用户查看文件、分析磁盘空间及检查权限等。常用选项如 `-l` 显示详细信息,`-a` 包含隐藏文件,`-h` 以易读格式展示大小,`-t` 按修改时间排序等。通过组合选项,可实现复杂需求,如递归遍历目录(`-R`)、显示 inode 号(`-i`)或结合正则过滤特定文件。注意权限限制、特殊字符处理及大规模目录操作可能带来的性能问题。掌握 `ls` 是高效使用 Linux 的关键一步。
|
C语言
【C语言篇】分支语句详解(超详细)
在 switch 语句中 case 语句和 default 语句是没有顺序要求的,只要顺序是满⾜实际需求的就可以。 不过我们通常是把 default ⼦句放在最后处理的。
959 2
|
存储 Ubuntu 算法
KNIME学习记录
KNIME学习记录
1341 0
|
消息中间件 NoSQL Redis
Redis Stream消息队列之基本语法与使用方式
这篇文章详细介绍了Redis Stream消息队列的基本语法和使用方式,包括消息的添加、读取、删除、修剪以及消费者组的使用和管理,强调了其在消息持久化和主备复制方面的优势。
977 0
|
存储 前端开发 JavaScript
【五子棋实战】第5章 开发五子棋前端页面
页面设计原则   1、可配置性。比如棋盘的大小可配置,棋盘边长可配置,黑白空期的值可配置;   2、响应式。各种屏幕大小下棋盘的布局要合理;   3、面向对象。棋子、棋盘的定义都用类来封装,代码要写的好看。
613 0