加法,你会吗?

简介: 加法,你会吗?

题目

面试官:设计一个加法,满足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
敲完收工.....面试官怎么还不来?

相关文章
|
数据可视化 前端开发 程序员
探索iVX:颠覆传统低代码平台的新潮流
探索iVX:颠覆传统低代码平台的新潮流
818 0
|
存储 Java 应用服务中间件
SpringBoot 存储图片 tomcat服务器存图片 数据库图片路径
SpringBoot 存储图片 tomcat服务器存图片 数据库图片路径
934 0
SpringBoot 存储图片 tomcat服务器存图片 数据库图片路径
|
6月前
|
人工智能 物联网 API
又又又上新啦!魔搭免费模型推理API支持DeepSeek-R1,Qwen2.5-VL,Flux.1 dev及Lora等
又又又上新啦!魔搭免费模型推理API支持DeepSeek-R1,Qwen2.5-VL,Flux.1 dev及Lora等
378 7
|
Android开发
Vscode中注释变成繁体的解决方法
这篇文章提供了两种解决Vscode中注释变成繁体字的方法:第一种是通过设置语言模式来更改显示语言;第二种是更改文件的编码格式,如在UTF-8和GBK之间切换,以解决乱码问题。
Vscode中注释变成繁体的解决方法
|
运维 监控 容灾
实现Java应用的高可用与自动化运维
实现Java应用的高可用与自动化运维
|
前端开发 Java 网络安全
解决docker中运行的jar包连不上前端程序
解决docker中运行的jar包连不上前端程序
|
Java Android开发 Kotlin
Android项目架构设计问题之要在Glide库中加载网络图片到ImageView如何解决
Android项目架构设计问题之要在Glide库中加载网络图片到ImageView如何解决
129 0
|
编解码 区块链 Windows
QT基础教程(QPalette和QIcon)
QT基础教程(QPalette和QIcon)
280 0

热门文章

最新文章