POJ 2389
发布日期:2021-06-30 15:30:55 浏览次数:2 分类:技术文章

本文共 1613 字,大约阅读时间需要 5 分钟。

Bull Math

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16175   Accepted: 8268

Description

Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help him check the bulls' answers. Read in two positive integers (no more than 40 digits each) and compute their product. Output it as a normal number (with no extra leading zeros). 

FJ asks that you do this yourself; don't use a special library function for the multiplication.

Input

* Lines 1..2: Each line contains a single decimal number.

Output

* Line 1: The exact product of the two input lines

Sample Input

111111111111111111111111

Sample Output

12345679011110987654321

Source

代码:

#include
#include
#include
using namespace std;int sum[2001];int s1[50] , s2[50];string str1 , str2;void multiply(){ int i , j , k; int l1 , l2 , len; memset(sum,0,sizeof(sum)); memset(s1,0,sizeof(s1)); memset(s2,0,sizeof(s2)); l1 = str1.size(); l2 = str2.size(); for(i = l1-1 , k = 1;i >= 0;i-- , k++) s1[k] = str1[i]-48; for(i = l2-1 , k = 1;i >= 0;i-- , k++) s2[k] = str2[i]-48; for(i = 1;i <= l1;i++ ) { for(j = 1;j <= l2;j++) { sum[i+j-1] += s1[i]*s2[j]; sum[i+j] += sum[i+j-1]/10; sum[i+j-1] %= 10; } } len = 2000; while(len--) { if(sum[len] != 0) break; } for(;len >= 1;len--) cout<
>str1>>str2) { multiply(); } return 0;}

 

转载地址:https://joycez.blog.csdn.net/article/details/82822226 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:POJ 2602
下一篇:POJ 2109

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月18日 00时47分50秒