大数相乘
发布时间:2020-12-14 02:20:36 所属栏目:大数据 来源:网络整理
导读:题目:请使用代码计算1234567891011121314151617181920*2019181716151413121110987654321。 答: #include " stdafx.h " #include iostream #include string using namespace std; int _tmain( int argc,_TCHAR* argv[]){ string strOne; string strTwo; cou
题目:请使用代码计算1234567891011121314151617181920*2019181716151413121110987654321。 答: #include "stdafx.h" #include <iostream> #include <string> using namespace std; int _tmain(int argc,_TCHAR* argv[]) { string strOne; string strTwo; cout<<输入第一个乘数:"; cin>>strOne; cout<<输入第二个乘数:"; cin>>strTwo; int lengthOne = strOne.size(); int lengthTwo = strTwo.size(); int lengthResult = lengthOne * lengthTwo; int *result = new int[lengthResult]; memset(result,0,lengthResult * sizeof(int)); int count = 1; int i,j,k; for (i = lengthTwo - 1; i >= 0; i--) { k = lengthResult - count; for (j = lengthOne - 1; j >= 0; j--) { result[k--] += (strTwo[i] - '0') * (strOne[j] - '); } count++; } for (i = lengthResult - 1; i > 0; i--) { while(result[i] >= 10) { result[i] -= 10; result[i - 1]++; } } for (i = 0; i < lengthResult; i++) { if (0 != result[i]) { break; } } cout<<strOne<< * "<<strTwo<< = "<<endl; for (; i < lengthResult; i++) { cout<<result[i]; } delete [] result; cout<<endl; return 0; } 运行界面如下:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |