码迷,mamicode.com
首页 > 其他好文 > 详细

大数加减乘模板

时间:2018-04-19 23:57:21      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:大数   import   ati   code   ima   while   cin   a+b   void   

加法:

 1 char * add(char *s, char *ss) {
 2     memset(str3, 0, sizeof(str3));
 3     int len1 = strlen(s)-1;
 4     int len2 = strlen(ss)-1;
 5     int len3 = 0, x, y, flag = 0;
 6     while(len1 >= 0 || len2 >= 0) {
 7         if(len1 >= 0) x = s[len1] - 0;
 8         else x = 0;
 9         if(len2 >= 0) y = ss[len2] - 0;
10         else y = 0;
11         str3[len3++] = (x+y+flag)%10 + 0;
12         flag = (x+y+flag)/10;
13         len2--;len1--;
14     }
15     for(int i = 0; i < len3/2; i ++) {
16         swap(str3[i],str3[len3-i-1]);
17     }
18     return str3;
19 }

减法:

 1 char *sub(char *s, char *ss) {
 2     memset(str3, 0, sizeof(str3));
 3     int len1 = strlen(s)-1, len2 = strlen(ss)-1;
 4     int flag = 1, len3 = 0, x, y, cnt = 0;
 5     if(strcmp(s,ss) == 0){
 6         str3[len3++] = 0;
 7         return str3;
 8     }
 9     if(len1 < len2 || (len1==len2&&strcmp(s,ss)<0)) {
10         swap(len1,len2);
11         swap(s,ss);
12         flag = 0;
13     }
14     while(len1 >= 0 || len2 >= 0) {
15         if(len1 >= 0) x = s[len1--] - 0;
16         else x = 0;
17         if(len2 >= 0) y = ss[len2--] - 0;
18         else y = 0;
19         int ans = x-y+cnt;
20         if(ans < 0) {
21             str3[len3++] = ans+10+0;
22             cnt = -1;
23         } else {
24             str3[len3++] = ans+0;
25             cnt = 0;
26         }
27     }
28     if(!flag) str3[len3++] = -;
29     for(int i = 0; i < len3/2; i ++) swap(str3[i], str3[len3-i-1]);
30     return str3;
31 }

乘法:

 1 int mul(char *s, char *ss,int *num) {
 2     int len1 = strlen(s);
 3     int len2 = strlen(ss);
 4     int len3 = len1+len2;
 5     for(int i = 0; i < len1/2; i ++) swap(s[i],s[len1-i-1]);
 6     for(int i = 0; i < len2/2; i ++) swap(ss[i],ss[len2-i-1]);
 7     for(int i = 0; i < len1; i ++) {
 8         for(int j = 0; j < len2; j ++) {
 9             num[i+j] += (s[i]-0)*(ss[j]-0);
10         }
11     }
12     for(int i = 0; i < len3; i ++) {
13         if(num[i] >= 10) {
14             num[i+1] += num[i]/10;
15             num[i] %= 10;
16         }
17     }
18     while(num[len3] == 0 && len3 >= 0) len3--;
19     len3++;
20     for(int i = 0; i < len3/2; i ++) {
21         swap(num[i], num[len3-i-1]);
22     }
23     if(!len3) len3++;
24     return len3;
25 }

 

Java的加减乘法:

 1 import java.util.Scanner;
 2 import java.math.BigDecimal;
 3 import java.math.BigInteger;
 4 
 5 public class Main{
 6     public static void main(String[] args) {
 7         Scanner cin = new Scanner(System.in);
 8         BigDecimal a = cin.nextBigDecimal();
 9         BigDecimal b = cin.nextBigDecimal();
10         BigInteger aa = cin.nextBigInteger();
11         BigInteger bb = cin.nextBigInteger();
12         System.out.println("a+b:"+a.add(b));
13         System.out.println("a-b:"+a.subtract(b));
14         System.out.println("a*b:"+a.multiply(b));
15         System.out.println("a/b:"+aa.divide(bb));
16         System.out.println("a^10:"+a.pow(10));
17     }
18 }

 

大数加减乘模板

标签:大数   import   ati   code   ima   while   cin   a+b   void   

原文地址:https://www.cnblogs.com/xingkongyihao/p/8886441.html

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