码迷,mamicode.com
首页 > 编程语言 > 详细

大整数算法

时间:2016-04-09 18:44:45      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:

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

大整数相加

 1 #include <stdio.h>
 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 <stdio.h>
 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 <stdio.h>
 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 <= n; ++i) {
34         for (j = 0; j <= result_index; ++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 <stdio.h>
 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 <= result_index; ++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 }

 

大整数算法

标签:

原文地址:http://www.cnblogs.com/0x12345678/p/5372254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!