大整数算法

本文主要整理了几个常用的大整数的算法:
大整数加法
大整数乘法
大整数阶乘
大整数幂
其实大体的思路都差不多,都是用数组来存储大整数。以下的代码仅仅实现功能,并没有充分详细的参数判断,在实际运用中,肯定是需要考虑的。

大整数相加

 1 #include 
 2 #include <string.h>
 3 #define N 1000
 4 void get_num(int *array)
 5 {
 6     char str[N] = {'\0'};
 7     int loop    = 0;
 8     int length  = 0;
 9
10     scanf("%s", str);
11     length = strlen(str);
12     for (loop = 0; loop < length; ++loop) {
13         array[loop] = str[length - loop - 1] - '0';
14     }
15     array[loop] = -1;
16 }
17
18 void big_plus(int *num1, int *num2, int *result)
19 {
20     int num1_index   = 0;
21     int num2_index   = 0;
22     int result_index = 0;
23     int tmp_carry    = 0;
24     int tmp_result   = 0;
25     while (num1[num1_index] != -1 && num2[num2_index] != -1) {
26         tmp_result = num1[num1_index++] + num2[num2_index++] + tmp_carry;
27         result[result_index++] = tmp_result % 10;
28         tmp_carry = tmp_result / 10;
29     }
30     while (num1[num1_index] != -1) {
31         tmp_result = num1[num1_index++] + tmp_carry;
32         result[result_index++] = tmp_result % 10;
33         tmp_carry = tmp_result / 10;
34     }
35     while (num2[num2_index] != -1) {
36         tmp_result = num2[num2_index++] + tmp_carry;
37         result[result_index++] = tmp_result % 10;
38         tmp_carry = tmp_result / 10;
39     }
40     if (tmp_carry) {
41         result[result_index++] = tmp_carry;
42     }
43     result_index[result_index] = -1;
44 }
45 void print_result(int *array)
46 {
47     int i = 0;
48     int len = 0;
49     for (i = 0; array[i] != -1; ++i);
50
51     len = i;
52     for (i = len - 1; i >= 0; --i) {
53         printf("%d", array[i]);
54     }
55     if (len == 0) {
56         printf("0");
57     }
58     printf("\n");
59 }
60 int main(int argc, char **argv)
61 {
62     int num1[N] = {0};
63     int num2[N] = {0};
64     int result[N] = {0};
65     //将数用数组来存,并从低位向高位存.
66     get_num(num1);
67     get_num(num2);
68     //执行相加操作
69     big_plus(num1, num2, result);
70     print_result(result);
71     return 0;
72 }

大整数相乘

 1 #include 
 2 #include <string.h>
 3 #define N 1000
 4
 5 void get_num(int *array)
 6 {
 7     char str[N] = {'\0'};
 8     int loop    = 0;
 9     int length  = 0;
10
11     scanf("%s", str);
12     length = strlen(str);
13     for (loop = 0; loop < length; ++loop) {
14         array[loop] = str[length - loop - 1] - '0';
15     }
16     array[length] = -1;
17 }
18 void big_multi(int *num1, int *num2, int *result)
19 {
20     int num1_index = 0;
21     int num2_index = 0;
22     int result_index = 0;
23     int tmp_carry = 0;
24     int tmp_result = 0;
25     for (num1_index = 0; num1[num1_index] != -1; ++num1_index) {
26         for (num2_index = 0; num2[num2_index] != -1; ++num2_index) {
27             tmp_result = num1[num1_index] * num2[num2_index] + result[num1_index + num2_index] + tmp_carry;
28             result[num1_index + num2_index] = tmp_result % 10;
29             tmp_carry = tmp_result / 10;
30         }
31         result_index = num1_index + num2_index - 1;
32         if (tmp_carry) {
33             result[++result_index] = tmp_carry;
34         }
35     }
36     result[++result_index] = -1;
37 }
38 void print_result(int *array)
39 {
40     int i = 0;
41     int len = 0;
42     for (i = 0; array[i] != -1; ++i);
43
44     len = i;
45     for (i = len - 1; i >= 0; --i) {
46         printf("%d", array[i]);
47     }
48     if (len == 0) {
49         printf("0");
50     }
51     printf("\n");
52 }
53 int main(int argc, char **argv)
54 {
55     int num1[N] = {0};
56     int num2[N] = {0};
57     int result[2 * N] = {0};
58     //将数用数组来存,并从低位向高位存.
59     get_num(num1);
60     get_num(num2);
61     //执行相乘操作
62     big_multi(num1, num2, result);
63     print_result(result);
64     return 0;
65 }

大整数阶乘

 1 #include 
 2 #include <string.h>
 3 #define N 10000
 4
 5 void print_result(int *array)
 6 {
 7     int i = 0;
 8     int len = 0;
 9     for (i = 0; array[i] != -1; ++i);
10
11     len = i;
12     for (i = len - 1; i >= 0; --i) {
13         printf("%d", array[i]);
14     }
15     if (len == 0) {
16         printf("0");
17     }
18     printf("\n");
19 }
20
21 void big_factor(int n, int *result)
22 {
23     int result_index = 0;
24     int carry = 0;
25     int tmp_result = 0;
26     int i = 0;
27     int j = 0;
28     result[0] = 1;
29     if (n 1) {
30         result[1] = -1;
31         return;
32     }
33     for (i = 1; i i) {
34         for (j = 0; j j) {
35             tmp_result = result[j] * i + carry;
36             result[j] = tmp_result % 10;
37             carry = tmp_result / 10;
38         }
39         while (carry) {
40             result[++result_index] = carry % 10;
41             carry = carry / 10;
42         }
43     }
44     result[++result_index] = -1;
45 }
46
47 int main(int argc, char **argv)
48 {
49     int n = 0;
50     int result[N] = {0};
51     scanf("%d", &n);
52     //将数用数组来存,并从低位向高位存.
53     big_factor(n, result);
54     print_result(result);
55     return 0;
56 }

大整数幂

 1 #include 
 2 #include <string.h>
 3 #define N 10000
 4
 5 void print_result(int *array)
 6 {
 7     int i = 0;
 8     int len = 0;
 9     for (i = 0; array[i] != -1; ++i);
10
11     len = i;
12     for (i = len - 1; i >= 0; --i) {
13         printf("%d", array[i]);
14     }
15     if (len == 0) {
16         printf("0");
17     }
18     printf("\n");
19 }
20
21 int big_pow(int base, int power, int *result)
22 {
23     int result_index = 0;
24     int carry = 0;
25     int tmp_result = 0;
26     int i = 0;
27     int j = 0;
28     result[0] = 1;
29     if (base == 1 || power == 0) {
30         result[1] = -1;
31     }
32     for (i = 0; i < power; ++i) {
33         for (j = 0; j j) {
34             tmp_result = result[j] * base + carry;
35             result[j] = tmp_result % 10;
36             carry = tmp_result / 10;
37         }
38         while (carry) {
39             result[++result_index] = carry % 10;
40             carry = carry / 10;
41         }
42     }
43     result[++result_index] = -1;
44 }
45
46 int main(int argc, char **argv)
47 {
48     int base  = 0;
49     int power = 0;
50     int result[N];
51     //输入底数和指数
52     scanf("%d%d", &base, &power);
53     big_pow(base, power, result);
54     print_result(result);
55     return 0;
56 }

Original: https://www.cnblogs.com/0x12345678/p/5372254.html
Author: Hackergin
Title: 大整数算法

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/644025/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • wsl2环境搭建

    我电脑配置不高,开虚拟机跑linux总觉得太卡。最近才了解到windows早就上了wsl2——一款较为轻量的虚拟机软件。所以本篇博客偏笔记向,存粹记录以便多次使用。 WSL2安装 …

    技术杂谈 2023年6月21日
    0119
  • GMM-HMM声学模型实例详解(标贝科技)

    GMM-HMM声学模型实例详解 GMM-HMM为经典的声学模型,基于深度神经网络的语音识别技术,其实就是神经网络代替了GMM来对HMM的观察概率进行建模,建模解码等识别流程的格个模…

    技术杂谈 2023年7月24日
    076
  • python练习题:请利用Python内置的hex()函数把一个整数转换成十六进制表示的字符串

    python;gutter:true;-*- coding: utf-8 -*-请利用Python内置的hex()函数把一个整数转换成十六进制表示的字符串n1 = 255n2 = …

    技术杂谈 2023年7月24日
    081
  • 网络中冗余备份

    冗余备份的重要性 如今社会,网络是各个产业的新的血脉,网络的稳定性至关重要,一旦网络出现故障,导致断网、延迟丢包等很可能会导致生产作业停滞,造成较经济损失,为此冗余备份至关重要,从…

    技术杂谈 2023年6月21日
    0120
  • 可视化全链路日志追踪

    可视化全链路日志追踪 https://mp.weixin.qq.com/s/Er4-X8q5MKZZUgAUHyeLwA 收录于合集 后台29 个 大众点评3 个 日志1 个 可视…

    技术杂谈 2023年5月31日
    0137
  • chrome 浏览器的使用技巧

    前端工程师大部分工作成果需要在浏览器中查看,使用 chrome 浏览器的频率非常高。更好更优雅地使用 chrome,将 chrome 配置成趁手的浏览器,将极大提升编程效率。本文将…

    技术杂谈 2023年5月30日
    091
  • Connection reset by peer的常见原因及解决办法

    转自:https://blog.csdn.net/xc_zhou/article/details/80950753 1,如果一端的Socket被关闭(或主动关闭,或因为异常退出而 …

    技术杂谈 2023年6月1日
    0125
  • elasticsearch通用工具类

    这几天写了一个关于es的工具类,主要封装了业务中常用es的常用方法。 本文中使用到的elasticsearch版本6.7,但实际上也支持es7.x以上版本,因为主要是对spring…

    技术杂谈 2023年7月10日
    081
  • 文本操作find cut sort wc sed awk

    文本操作 查找文件: # find 大概位置 以名字查找 名字 find /etc/ -name i18n find /etc/ -name 70* find /etc/ -nam…

    技术杂谈 2023年7月11日
    085
  • mysql(DQL)

    MYSQL(康老师-DQL ) 1:基本的SELECT语句 1.1:基本的SELECT语句的课后练习 2:运算符 2.1:运算符课后练习 3.1排序 3.2分页 4.多表查询 4….

    技术杂谈 2023年7月25日
    071
  • 导出putty配置

    原文链接:http://downloadsquad.switched.com/2007/02/01/howto-transfer-your-putty-settings-betwe…

    技术杂谈 2023年6月1日
    0116
  • MySQL备份与恢复

    MySQL备份与恢复 备份是数据安全的最后一道防线,对于任何数据丢失的场景,备份虽然不一定能恢复百分之百的数据(取决于备份周期),但至少能将损失降到最低。 数据丢失的场景举例: 人…

    技术杂谈 2023年6月21日
    096
  • java学习之动态代理

    在后面的漏洞研究的学习中,必须要会的几个知识点。反射机制和动态代理机制。至于反射的前面已经讲到过了,这里就不做更多的赘述了。反射是通过class文件去获取对象对象的方法. &amp…

    技术杂谈 2023年6月21日
    093
  • 一,Flink快速上手

    1.依赖配置 1.1 pom文件 8 8 1.13.0 1.8 2.12 1.7.30 org.apache.flink flink-java ${flink.version} o…

    技术杂谈 2023年7月10日
    084
  • Redis基础

    Redis Redis介绍和安装 redis 是一个非关系型数据库(区别于mysql关系型数据库,关联关系,外键,表),nosql数据库(not only sql:不仅仅是SQL)…

    技术杂谈 2023年6月21日
    0119
  • SSM配置文件的连接

    使用ssm框架配置数据库连接时的问题 如果MySQL数据库版本是8.0.11, url配置成了MySql5.0以上版本需要的驱动类名(com.mysql. cj.jdbc.Driv…

    技术杂谈 2023年6月21日
    0113
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球