大数乘法(C++)
发布时间:2020-12-14 02:40:15 所属栏目:大数据 来源:网络整理
导读:题目:POJ 2398 Bull Math Time Limit: ?1000MS ? Memory Limit: ?65536K Total Submissions: ?13410 ? Accepted: ?6903 Description Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise
题目:POJ 2398
#include<iostream> #include<cstring> #include<cstdio> #include<string> using namespace std; const int maxn = 100; void reverse(char a[]) { int len = strlen(a); for(int i = 0 ; i < len / 2; i++) { int temp = a[i]; a[i] = a[len - i - 1]; a[len - i -1] = temp; } } int main() { char a[maxn],b[maxn]; int t[100] = {0}; //printf("Please enter 2 numbers: "); scanf("%s%s",a,b); reverse(a); reverse(b); if(strcmp(a,"0")==0||strcmp(b,"0")==0) cout<<"0"<<endl; else { int i,j; for(i = 0; i <strlen(b); i++) { int cnt = 0; for(j = 0; j < strlen(a); j++) { int temp = (b[i] - '0') * (a[j] - '0'); int tt= t[i+j] + temp + cnt; t[j+i] = tt % 10; cnt = tt / 10; } while(cnt != 0) { t[j+i] = cnt % 10; cnt = cnt / 10; j++; } } for(int k = i + j - 2; k >= 0; k--) { cout<<t[k]; } } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |