国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

大數(shù)運(yùn)算(大數(shù)加法and大數(shù)乘法)

2019-11-08 18:20:55
字體:
供稿:網(wǎng)友

大數(shù) 加法 和大數(shù)乘法    我只寫了這兩個(gè) 所以就分享這兩個(gè) 吧

我認(rèn)為大數(shù)加法 和大數(shù)乘法  用到了一個(gè) 算法  就是 進(jìn)位操作 乘法是在 相乘的 基礎(chǔ)上 進(jìn)行加法

那么 現(xiàn)在 來看 核心的代碼:

大數(shù)加法

for(i=0,l=0;i<j+1;i++)  // 運(yùn)算x+y {	k=x[i]+y[i];  //這是原始加法 	c[i]=(k+l)%10;//將余數(shù) 賦給c【i】	l=(k+l)/10;// 進(jìn)幾位}

大數(shù)乘法

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--)//每一項(xiàng)的 結(jié)果加起來 類似于加法運(yùn)算  {	temp=0;	for(j=0;j<len2;j++)	{		temp+=c[j][i];	}			c[len2][i]=(temp+ll)%10;  //答案放在 len2  行里 	ll=(ll+temp)/10;}

原理是一樣的

那么  我們開始寫下 完整思路 

1.首先 既然是大數(shù)運(yùn)算 那么 我們用int  或者_(dá)64int 都會(huì)溢出  因此 我們用char【】 字符數(shù)組 輸入 這樣不會(huì)溢出 

2.用char 數(shù)組 輸入后 我們將其 轉(zhuǎn)換成int 數(shù)組 把每一個(gè)都拆分存到數(shù)組里  (大數(shù)加法 需要逆序,我寫的乘法沒用逆序 )  逆序的原因是 我們 要進(jìn)行進(jìn)位操作 但是 數(shù)組不能玩前 存 只能往后存 因此我們逆序后   就可以 進(jìn)行進(jìn)位操作  然后倒著輸出結(jié)果   就可以了、

3. 進(jìn)行加法或乘法運(yùn)算

4. 輸出 結(jié)果 ok

加法運(yùn)算

#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));//必須要進(jìn)行清零操作否則為亂碼!! 	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;	//開始進(jìn)行逆轉(zhuǎn)操作 	for(i=len1-1,j=0;i>=0;i--)//必須要從len-1開始  下標(biāo)為0結(jié)束 	{		x[j]=str1[i]-'0';	//	PRintf("%d/n",x[j]);		j++;		}	for(i=len2-1,j=0;i>=0;i--)//必須要從len-1開始  下標(biāo)為0結(jié)束 	{		y[j]=str2[i]-'0';	//	printf("%d f,",y[j]);		j++;		}	m=len1>len2?len1:len2;}void addition(){		//開始進(jìn)行 加法運(yùn)算,滿10進(jìn)1 //		flag=0;//標(biāo)志0 //		for(i=0;i<m;i++)//		{//			 if(flag==0)//			 {//			 	z[i]=x[i]+y[i];//不滿10 原樣 //			 }//			 if(z[i]>=10)        //滿10時(shí)取余,進(jìn)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;//進(jìn)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("輸入兩個(gè)要求的數(shù):/n"))	{		mmeset();		input();		change_int();		addition();	}	return 0;} 

大數(shù)乘法

//大數(shù)乘法運(yùn)算#include<stdio.h>#include<string.h>#define MAXNUM 100000char 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--)//每一項(xiàng)的 結(jié)果加起來 類似于加法運(yùn)算  	{		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)//如果第一項(xiàng)是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();//乘法運(yùn)算 //		for(i=0;i<len2+1;i++)   這里是  把 二維數(shù)組輸出一下更直觀//		{//			for(j=0;j<=len1+len2+1;j++)//				printf("%d,",c[i][j]);//			printf("/n");//		}		output();//	輸出 	}} 

就是這樣    那讓我們來實(shí)戰(zhàn)一下  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): 66660Problem DescriptionI have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. InputThe 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. OutputFor 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
21 2112233445566778899 998877665544332211 Sample Output
Case 1:1 + 2 = 3Case 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';			// 計(jì)算//			for(l=0;l<k;l++)//				printf("%d",x[l]);			for(i=0,l=0;i<j+1;i++)  // 運(yùn)算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;}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 桐乡市| 卢龙县| 互助| 晴隆县| 苍溪县| 河间市| 行唐县| 西畴县| 综艺| 长宁区| 宁陕县| 北京市| 永清县| 三门县| 澎湖县| 常山县| 成安县| 孙吴县| 昆山市| 涡阳县| 且末县| 德清县| 汨罗市| 淮南市| 广西| 蚌埠市| 上林县| 时尚| 万山特区| 门头沟区| 陆良县| 安陆市| 荔浦县| 当阳市| 开化县| 郸城县| 同德县| 阿拉尔市| 宁武县| 河源市| 庆元县|