zoukankan      html  css  js  c++  java
  • 大整数乘法

    //大整数乘法
    #include<stdio.h>
    #include<string.h>
    
    #define MAX_LEN 200
    unsigned an1[MAX_LEN+10];
    unsigned an2[MAX_LEN+10];
    
    unsigned aResult[MAX_LEN*2+10];
    
    char szLine1[MAX_LEN+10];
    char szLine2[MAX_LEN+10];
    int main()
    {
    	gets(szLine1);
    	gets(szLine2);
    	memset(an1,0,sizeof(an1));
    	memset(an2,0,sizeof(an2));
    	memset(aResult,0,sizeof(aResult));
    	int i,j;
    	int nLen1=strlen(szLine1);
    	j=0;
    	for(i=nLen1-1;i>=0;i--)
    		an1[j++]=szLine1[i]-'0';
    	int nLen2=strlen(szLine2);
    	j=0;
    	for(i=nLen2-1;i>=0;i--)
    		an2[j++]=szLine2[i]-'0';
    	for(i=0;i<nLen2;i++)
    	{
    		for(j=0;j<nLen1;j++)
    			aResult[i+j]+=an1[j]*an2[i];
    	}
    	for(i=0;i<MAX_LEN*2;i++)
    	{
    		if(aResult[i]>=10)
    		{
    			aResult[i+1]+=aResult[i]/10;
    			aResult[i]=aResult[i]%10;
    		}
    	}
    	bool bStartOutput=false;
    	for(i=MAX_LEN*2;i>=0;i--)
    	{
    		if(bStartOutput)
    			printf("%d",aResult[i]);
    		else if(aResult[i])
    		{
    			printf("%d",aResult[i]);
    			bStartOutput=true;
    		}
    	}
    	if(!bStartOutput)
    		printf("0");
    	printf("
    ");
    	return 0;
    }

  • 相关阅读:
    1052. 卖个萌 (20)
    1051. 复数乘法 (15)
    1050. 螺旋矩阵(25)
    1049. 数列的片段和(20)
    1048. 数字加密(20)
    1047. 编程团体赛(20)
    1046. 划拳(15)
    怎么用js代码改变单选框的选中状态
    Dom操作--全选反选
    Scoket简介
  • 原文地址:https://www.cnblogs.com/javafly/p/6037188.html
Copyright © 2011-2022 走看看