[Operation] of large numbers of large numbers of Math

The addition of large numbers

For addition, the factorial of ideas and the same, that is, to turn every every point of view.

The first two numbers are put into the array, while the comparison length , (len1 longer than len2 then sequentially added only after len1 length), if by the addition result> = 10, then let m = 1, or m = 0, next let a recall after added another + m

Calm my heart via Code

#include<stdio.h>
#include<string.h>
int x[100]={0},y[100]={0},z[105]={0};//将数组元素全部初始化为0 
int main()
{
	char a[100],b[100];//通过字符串对大数进行输入并储存 
	int len1,len2,len;
	while(scanf("%s %s",a,b))
	{
		int i,j=0,k=0;
		len1=strlen(a);
		len2=strlen(b);
		for(i=len1-1;i>=0;i--)//将两个字符串中的字符转化为数字,并倒序储存到数组中,即字符串为123456,则数组为654321 
		{
			x[j]=a[i]-'0';
			j++;
		}
		for(i=len2-1;i>=0;i--)
		{
			y[k]=b[i]-'0';
			k++;
		}
		if(len1>len2)
            len=len1;
        else
            len=len2;
		i=0;//从最低位(个位)开始进行计算
		int m=0;
		for(i=0;i<len;i++)
		{
			z[i]=(x[i]+y[i]+m)%10;//将所得数的个位存到数组z[i]中去 
			if((x[i]+y[i]+m)>=10)
				m=1;
			else
				m=0;
		}
		if((x[i-1]+y[i-1]+m)>=10)//判断最高位还需不需要进位
			z[i]=1;
		else
			i=i-1;
		for(;i>=0;i--)//到序输出数组 
			printf("%d",z[i]);
		printf("\n");
	}
	return 0; 
}

 

Subtraction of large numbers

To contact the addition of large numbers, noting that in the programming design point:

① regardless of addition and subtraction, it is to be followed by two numbers seen in the median length of the longest up. For example, the results of 6012-12 still have to go see the fourth

addition to carry, borrow bit subtraction. The possible addition carry into the highest level but also do not know how many go out, so to take this into account. Subtract but if the default ab of a> b, then there is no need to borrow highest position, so that a lot of calculation can be simple, only need to call the function go Analyzing a> b can, if a <b is a first negative output and then exchange a, b to the order parameter.

③ no need to consider the addition result preamble 0 , and to consider such as subtraction 6012-6010 = 0002 this case, when the output of the output leading not 0.

AC Code:

#include<iostream> 
#include<bits/stdc++.h>
using namespace std;

int x,y; //要求x-y
int a[500],b[500];

void sub(int a[],int b[],int len) //这里默认a是大于b的,具体在主函数里进行case判断了 
{
	int jie = 0;
	//从低位到高位依次对应相减,如果减不过就借位
	for(int i=1;i<=len;i++)
	{
		int temp;
		if(a[i]-jie<b[i])  
		{
			jie = 1; //需要借位,给下一位的jie赋为1 
			temp = a[i]+10-b[i];
		}
		else
		{
			temp = a[i] - jie - b[i]; //注意,如果我用这种方法,则要先-jie,再jie=0 
			jie = 0; 
		}
		a[i] = temp;
	} 
	//注意这里不用像加法一样还要去考虑最高位有进位,因为已经默认a>b了,所以不存在最高位还需要借位的情况
	 
	//输出的时候要注意:有可能有前导0!!!!(而加法无需考虑 
	for(int i=len;i>=1;i--)
	{
		if(a[i]==0)
			len--;
		else
			break;
	} 
	for(int i=len;i>=1;i--)
		cout<<a[i];
	cout<<endl;
}



int main()
{
	char sa[500],sb[500]; //建立字符数组,这样就可以方便直接调用函数来求长度
	cin>>sa>>sb; //当作字符串输入 
	int len1 = strlen(sa);
	int len2 = strlen(sb);
	//char数组转换成int数组,方便运算
	for(int i=len1;i>=1;i--) //从下标为1开始存储,方便看。注意从低位->高位的顺序存 
		a[i] = sa[len1-i] - '0';
	for(int i=len2;i>=1;i--)
		b[i] = sb[len2-i] - '0';
	
	if(len1>len2) //被减数更大
		sub(a,b,len1);
	else if(len1<len2) //减数更大,结果为负数
	{
		cout<<"-";
		sub(b,a,len2);
	}
	else if(len1==len2) //两个数相同长度,还是应该比较大小,不然不好确定符号
	{
		int a_bigger = 0;
		for(int i=len1;i>=1;i--)
		{
			if(a[i]>b[i])
			{
				a_bigger = 1;
				break;
			}
		}
		if(a_bigger==1)//说明被减数更大	
			sub(a,b,len1);
		else //说明减数更大
		{
			cout<<"-";
			sub(b,a,len2);
		} 
	} 
		 
	return 0;
}

 

Multiplication of large numbers

Factorial and practices much the same, but note that factorial because each bit can take on a very large number, so num to remain and pass on, and multiplying the corresponding bit for multiplication, up to 9 * 9 = 81, so only the need to value the extra second is added to the next, standard retention value to bits.

To get to a point where: the value at position 5 is a, the actual meaning of a * 10000. Therefore, the i-th bit and the j-th bit of the number is multiplied, in essence, i * j bits bits bits = i * j, and therefore, the results should be on i * j bits.

for(i=0;i<len1;i++)//将因数各个位上的数字与另一个各个位上的数字相乘 
		{
			for(j=0;j<len2;j++)
				z[i+j]=z[i+j]+x[i]*y[j];//先乘起来,后面统一进行进位。这里要理解!!!
		}
		for(i=0;i<MAX*2;i++)//进行进位  MAX是二者位数最长长度
		{
			if(z[i]>=10)  //若>=10 
			{
				z[i+1]=z[i+1]+z[i]/10;  //将十位上数字进位 
				z[i]=z[i]%10;  //将个位上的数字留下
			}
		}
		for(i=MAX*2;i>0;i--)  //删除0的前缀 因为用的位数是MAX*2,不一定真能达到这么长
		{
			if(z[i]==0)
				continue;
			else
				break;
		}
		for(;i>=0;i--)  //倒序输出 
			printf("%d",z[i]);

--------------------- 
作者:静之吾心 
来源:CSDN 
原文:https://blog.csdn.net/lisp1995/article/details/52316466 
版权声明:本文为博主原创文章,转载请附上博文链接!

Integer Division

Code logic is not so difficult to imagine. In fact the "quotient" is the dividend minus multiples of the divisor, a multiple of 10 decrease until the last remaining number is less than the divisor, for the remainder .

28536 divided by 23 to look at an example: start business to zero.

First subtracting 23 times 1000 (to complement bits == 0 until dividend divisor bits) is 23,000, it was found possible to reduce a time, the remaining 5536, so the value of the quotient increases 1000;

5536 then subtracts 2300, he found enough minus 2 times, the remaining 936, so the value of the quotient by 200, i.e., 1200;

936 and then subtracted 230, enough minus 4 times, the remaining 16, 40 so quotient value increases, i.e., 1240.

Finally, the remaining number found more than 23 small, that is, the remainder, ie 28536/23 was more than 124,016.

 

 

Guess you like

Origin blog.csdn.net/m0_38033475/article/details/91952967