大数运算(大数加法and大数乘法)
2017-02-17 00:14
288 查看
大数模板 -- 万进制 为基础的 模板
#include <algorithm>
#include <stdlib.h>
#include <cstring>
#include <iostream>
#include <stdio.h>
#define ll long long
#define MAXN 10000
#define DELD 4
using namespace std;
int a[MAXN];
int b[MAXN];
struct Bignum{
int len;
int num[MAXN];
}x,y;
void Bigint_int(int b)//int ת»¯³É4λΪ»ù´¡µÄ int
{
int temp=b,c=0;
x.len=0;
memset(x.num,0,sizeof(x.num));
while(temp>=MAXN)
{
c=temp%MAXN;
temp/=MAXN;
x.num[x.len++]=c;
}
x.num[x.len++]=temp;
}
void Bigchar_int(char str[])
{
memset(y.num,0,sizeof(y.num));
int len=strlen(str);
int l=len/DELD;
if(len%DELD)
l++;
y.len=0;
for(int j=len-1;j>=0;j-=DELD)
{
int temp=0;
int k=j-DELD+1;
k=k<0?0:k;
for(int x=k;x<=j;x++)
{
temp=temp*10+str[x]-'0';
}
y.num[y.len++]=temp;
}
}
void Big_sum()
{
int len=x.len>y.len?x.len:y.len;
for(int j=0;j<=x.len;j++)
a[j]=x.num[j];
for(int j=0;j<=y.len;j++)
b[j]=y.num[j];
for(int i=0;i<len;i++)
{
a[i]+=b[i];
if(a[i]>=MAXN)
{
a[i+1]++;
a[i]-=MAXN;
}
}
int k=1000;
for(int i=0;i<4;i++,k/=10)
{
if(!a[len-1]/k)
continue;
printf("%d",a[len-1]/k);
a[len-1]%=k;
}
for(int i=len-2;i>=0;i--)
for(int j=0,k=1000;j<4;j++,k/=10)
{
printf("%d",a[i]/k);
a[i]%=k;
}
}
int main()
{
int num,n,i,j;
char str[100];
while(cin>>num)
{
if(num>=MAXN)
{
Bigint_int(num);
for(i=x.len-1;i>=0;i--)
{
printf("%d ",x.num[i]);
}
printf("\n");
}
cin>>str;
Bigchar_int(str);
for(i=y.len-1;i>=0;i--)
{
printf("%d ",y.num[i]);
}
Big_sum();
}
return 0;
}
大数 加法 和大数乘法 我只写了这两个 所以就分享这两个 吧
我认为大数加法 和大数乘法 用到了一个 算法 就是 进位操作 乘法是在 相乘的 基础上 进行加法
那么 现在 来看 核心的代码:
大数加法
大数乘法
原理是一样的
那么 我们开始写下 完整思路
1.首先 既然是大数运算 那么 我们用int 或者_64int 都会溢出 因此 我们用char【】 字符数组 输入 这样不会溢出
2.用char 数组 输入后 我们将其 转换成int 数组 把每一个都拆分存到数组里 (大数加法 需要逆序,我写的乘法没用逆序 ) 逆序的原因是 我们 要进行进位操作 但是 数组不能玩前 存 只能往后存 因此我们逆序后 就可以 进行进位操作 然后倒着输出结果 就可以了、
3. 进行加法或乘法运算
4. 输出 结果 ok
加法运算
![](https://img-blog.csdn.net/20170217005004659?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2l6YWlm/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
大数乘法
![](https://img-blog.csdn.net/20170217005236192?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2l6YWlm/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
就是这样 那让我们来实战一下 hdu1002 A+BII
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 343602 Accepted Submission(s): 66660
Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using
32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line
between two test cases.
Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
附上代码
#include <algorithm>
#include <stdlib.h>
#include <cstring>
#include <iostream>
#include <stdio.h>
#define ll long long
#define MAXN 10000
#define DELD 4
using namespace std;
int a[MAXN];
int b[MAXN];
struct Bignum{
int len;
int num[MAXN];
}x,y;
void Bigint_int(int b)//int ת»¯³É4λΪ»ù´¡µÄ int
{
int temp=b,c=0;
x.len=0;
memset(x.num,0,sizeof(x.num));
while(temp>=MAXN)
{
c=temp%MAXN;
temp/=MAXN;
x.num[x.len++]=c;
}
x.num[x.len++]=temp;
}
void Bigchar_int(char str[])
{
memset(y.num,0,sizeof(y.num));
int len=strlen(str);
int l=len/DELD;
if(len%DELD)
l++;
y.len=0;
for(int j=len-1;j>=0;j-=DELD)
{
int temp=0;
int k=j-DELD+1;
k=k<0?0:k;
for(int x=k;x<=j;x++)
{
temp=temp*10+str[x]-'0';
}
y.num[y.len++]=temp;
}
}
void Big_sum()
{
int len=x.len>y.len?x.len:y.len;
for(int j=0;j<=x.len;j++)
a[j]=x.num[j];
for(int j=0;j<=y.len;j++)
b[j]=y.num[j];
for(int i=0;i<len;i++)
{
a[i]+=b[i];
if(a[i]>=MAXN)
{
a[i+1]++;
a[i]-=MAXN;
}
}
int k=1000;
for(int i=0;i<4;i++,k/=10)
{
if(!a[len-1]/k)
continue;
printf("%d",a[len-1]/k);
a[len-1]%=k;
}
for(int i=len-2;i>=0;i--)
for(int j=0,k=1000;j<4;j++,k/=10)
{
printf("%d",a[i]/k);
a[i]%=k;
}
}
int main()
{
int num,n,i,j;
char str[100];
while(cin>>num)
{
if(num>=MAXN)
{
Bigint_int(num);
for(i=x.len-1;i>=0;i--)
{
printf("%d ",x.num[i]);
}
printf("\n");
}
cin>>str;
Bigchar_int(str);
for(i=y.len-1;i>=0;i--)
{
printf("%d ",y.num[i]);
}
Big_sum();
}
return 0;
}
大数 加法 和大数乘法 我只写了这两个 所以就分享这两个 吧
我认为大数加法 和大数乘法 用到了一个 算法 就是 进位操作 乘法是在 相乘的 基础上 进行加法
那么 现在 来看 核心的代码:
大数加法
for(i=0,l=0;i<j+1;i++) // 运算x+y { k=x[i]+y[i]; //这是原始加法 c[i]=(k+l)%10;//将余数 赋给c【i】 l=(k+l)/10;// 进几位 }
大数乘法
int temp=0,ll=0; int i,j; for(i=len2;i>=0;i--) { for(j=len1,ll=0;j>=0;j--) { temp=b[i]*a[j]; c[i][i+j+1]=(temp+ll)%10; ll=(temp+ll)/10; } c[i][i+j+1]=ll; } for(i=len1+len2,ll=0;i>=0;i--)//每一项的 结果加起来 类似于加法运算 { temp=0; for(j=0;j<len2;j++) { temp+=c[j][i]; } c[len2][i]=(temp+ll)%10; //答案放在 len2 行里 ll=(ll+temp)/10; }
原理是一样的
那么 我们开始写下 完整思路
1.首先 既然是大数运算 那么 我们用int 或者_64int 都会溢出 因此 我们用char【】 字符数组 输入 这样不会溢出
2.用char 数组 输入后 我们将其 转换成int 数组 把每一个都拆分存到数组里 (大数加法 需要逆序,我写的乘法没用逆序 ) 逆序的原因是 我们 要进行进位操作 但是 数组不能玩前 存 只能往后存 因此我们逆序后 就可以 进行进位操作 然后倒着输出结果 就可以了、
3. 进行加法或乘法运算
4. 输出 结果 ok
加法运算
#include<stdio.h> #include<string.h> char str1[100],str2[100]; int x[100],y[100],z[100],c[100]; int len1,len2,m; void mmeset() { memset(str1,0,sizeof(str1));//必须要进行清零操作否则为乱码!! memset(str2,0,sizeof(str2)); memset(x,0,sizeof(x)); memset(y,0,sizeof(y)); memset(z,0,sizeof(z)); memset(c,0,sizeof(c)); } void input() { gets(str1); gets(str2); len1=strlen(str1); len2=strlen(str2); } void change_int() { int i,j; //开始进行逆转操作 for(i=len1-1,j=0;i>=0;i--)//必须要从len-1开始 下标为0结束 { x[j]=str1[i]-'0'; // printf("%d\n",x[j]); j++; } for(i=len2-1,j=0;i>=0;i--)//必须要从len-1开始 下标为0结束 { y[j]=str2[i]-'0'; // printf("%d f,",y[j]); j++; } m=len1>len2?len1:len2; } void addition() { //开始进行 加法运算,满10进1 // flag=0;//标志0 // for(i=0;i<m;i++) // { // if(flag==0) // { // z[i]=x[i]+y[i];//不满10 原样 // } // if(z[i]>=10) //满10时取余,进1 // { // z[i]=z[i]%10; // flag=1; //printf("~~%d~~",z[i]); // } // else // flag=0; // printf("!!%d!!",z[i]); // if(flag) // { // z[i+1]=x[i+1]+y[i+1]+1;//进1操作 // } int kk,ll,i; for(i=0,ll=0;i<m;i++) { kk=x[i]+y[i]; z[i]=(kk+ll)%10; ll=(kk+ll)/10; } for(i--,ll=0;i>=0;i--,ll++)//倒逆回来 c[ll]=z[i]; for(i=0;i<ll;i++)//输出 { if(i==0&&z[i]==0) i++; printf("%d",c[i]); } putchar('\n'); } int main() { int j,i,flag; while(printf("输入两个要求的数:\n")) { mmeset(); input(); change_int(); addition(); } return 0; }
大数乘法
//大数乘法运算 #include<stdio.h> #include<string.h> #define MAXNUM 100000 char str1[MAXNUM],str2[MAXNUM]; int a[MAXNUM],b[MAXNUM]; int c[100][MAXNUM]; int len1,len2; void mmeset() { memset(str1,0,sizeof(str1)); memset(str2,0,sizeof(str2)); memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); } void input() { gets(str1); gets(str2); len1=strlen(str1); len2=strlen(str2); } void change_int(int a[],int b[],char str1[],char str2[]) { int i,j; /* 逆序 for(i=0,j=len1-1;j>=0;i++,j--){ a[i]=str1[j]-'0'; } for(i=0,j=len2-1;j>=0;i++,j--){ b[i]=str2[j]-'0'; }*/ for(i=0;i<len1;i++){ a[i]=str1[i]-'0'; } for(i=0;i<len2;i++){ b[i]=str2[i]-'0'; } } void multiplication() { int temp=0,ll=0; int i,j; for(i=len2;i>=0;i--) { for(j=len1,ll=0;j>=0;j--) { temp=b[i]*a[j]; // printf("%d Temp=%d ",j,temp); c[i][i+j+1]=(temp+ll)%10; // printf("%d i+j=%d ",c[i][i+j+1],i+j); ll=(temp+ll)/10; // printf("%d\n",ll); } c[i][i+j+1]=ll; } for(i=len1+len2,ll=0;i>=0;i--)//每一项的 结果加起来 类似于加法运算 { temp=0; for(j=0;j<len2;j++) { temp+=c[j][i]; } c[len2][i]=(temp+ll)%10; //答案放在 len2 行里 ll=(ll+temp)/10; } } void output() { int i; for(i=0;i<len1+len2+1;i++) { if(c[len2][i]==0&&i==0)//如果第一项是0 跳过去 continue; printf("%d",c[len2][i]);// 答案在len2 行 } printf("\n"); } int main() { int i,j,k; while(printf("输入 A and B\n")) { mmeset();//清零操作 input();//输入 change_int(a,b,str1,str2);// 输入后 将 char--> int multiplication();//乘法运算 // for(i=0;i<len2+1;i++) 这里是 把 二维数组输出一下更直观 // { // for(j=0;j<=len1+len2+1;j++) // printf("%d,",c[i][j]); // printf("\n"); // } output();// 输出 } }
就是这样 那让我们来实战一下 hdu1002 A+BII
A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 343602 Accepted Submission(s): 66660
Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using
32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line
between two test cases.
Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
附上代码
#include<stdio.h> #include<string.h> char a[100000]; char b[100000]; int c[1000000]; int x[100000]; int y[100000]; int z[100000]; int main() { int T,i,j,k,l,p,len1,len2; while(~scanf("%d",&T)) { p=1; while(T--) { i=0,j=0; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); memset(x,0,sizeof(x)); memset(y,0,sizeof(y)); memset(z,0,sizeof(z)); scanf("%s",a); len1=strlen(a); scanf("%s",b); len2=strlen(b); j=len1>len2? len1:len2; for(k=0,len1--;len1>=0;len1--) // a ,b 倒 过来 x[k++]=a[len1]-'0'; for(l=0,len2--;len2>=0;len2--) y[l++]=b[len2]-'0'; // 计算 // for(l=0;l<k;l++) // printf("%d",x[l]); for(i=0,l=0;i<j+1;i++) // 运算x+y { k=x[i]+y[i]; c[i]=(k+l)%10; l=(k+l)/10; } printf("Case %d:\n",p); printf("%s + %s = ",a,b); for(i--,l=0;i>=0;i--,l++) z[l]=c[i]; for(i=0;i<l;i++) { if(i==0&&z[i]==0) i++; printf("%d",z[i]); } // for(i--;i>=0;i--) // { // if(c[i]==0) // i--; // printf("%d",c[i]); // } if(T==0) printf("\n"); else printf("\n\n"); p++; } } return 0; }
相关文章推荐
- 大数加法.减法.乘法.除法
- 大数运算(二) —— 大数加法
- vector、string实现大数加法乘法
- 大数加法与乘法
- [acm 1001] c++ 大数加法 乘法 幂
- 大数乘法与大数加法 java实现
- 大数运算(2)——大数加法
- Java常用数字工具类 大数乘法、加法、减法运算(2)
- 大数的加法,乘法,全排列实现
- 大数加法,乘法.
- uva 465 - Overflow 大数加法 大数乘法
- 基础算法,大数加法和乘法的实现
- 剑指offer编程题Java实现——面试题12相关题大数的加法、减法、乘法问题的实现
- 大数加法、减法、乘法、除法、求余、比较、乘方(代码简短(总计150)、易懂)
- 大数加法、乘法
- 大数加法乘法
- 大数运算 (正整型)(三)—— 大数乘法
- Very Easy !!! (大数模拟加法乘法)
- 大数乘法包含大数加法(分治法)
- Java大数模板——加法、减法、乘法、除法、开方、求余