BZOJ 1754: [Usaco2005 qua]Bull Math

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

题解:

高精度乘法即可。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
//by zrt
//problem:
using namespace std;
char a[105],b[105];
int c[105];
int la,lb;
int cc;
int stk[105],top;
int main(){
    #ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
    scanf("%s%s",a,b);
    la=strlen(a);
    lb=strlen(b);
    for(int i=0;i<la;i++) a[i]-=‘0‘;
    for(int i=0;i<lb;i++) b[i]-=‘0‘;
    for(int i=0;i<la/2;i++) swap(a[i],a[la-i-1]);
    for(int i=0;i<lb/2;i++) swap(b[i],b[lb-i-1]);
    for(int i=0;i<la;i++){
        for(int j=0;j<lb;j++){
            c[i+j]+=a[i]*b[j];
        }
    }
    for(int i=0;i<=100;i++){
        stk[top++]=(c[i]+cc)%10;
        cc=(c[i]+cc)/10;
    }
    while(stk[top-1]==0) top--;
    while(top){
        printf("%d",stk[--top]);
    }
    puts("");
    return 0;
}
时间: 2024-08-08 01:43:53

BZOJ 1754: [Usaco2005 qua]Bull Math的相关文章

bzoj 1754: [Usaco2005 qua]Bull Math【高精乘法】

高精乘法板子 然而WA了两次也是没救了 #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N=105; int la,lb,lc,a[N],b[N],c[N],tot; char ch[N]; int main() { scanf("%s",ch+1); la=strlen(ch+1); for(int i=1;i<=la;i

【BZOJ】1754: [Usaco2005 qua]Bull Math

[算法]高精度乘法 #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int maxn=100; char s1[maxn],s2[maxn]; int a[maxn],b[maxn],c[maxn],lena,lenb,lenc; int main(){ scanf("%s%s",s1,s2); lena=strlen(s1);lenb

bzoj1754[Usaco2005 qua]Bull Math*

bzoj1754[Usaco2005 qua]Bull Math 题意: 求两个正整数的积,每个数≤40位. 题解: 为什么C++不能支持高精度呢…… 代码: 1 a=int(raw_input()) 2 b=int(raw_input()) 3 print a*b 20160831

bzoj 1755: [Usaco2005 qua]Bank Interest【模拟】

原来强行转int可以避免四舍五入啊 #include<iostream> #include<cstdio> using namespace std; int r,y; double m; int main() { scanf("%d%lf%d",&r,&m,&y); double l=1.0+(double)r/100.0; for(int i=1;i<=y;i++) m*=l; printf("%d\n",(i

1751: [Usaco2005 qua]Lake Counting

1751: [Usaco2005 qua]Lake Counting Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 190  Solved: 150[Submit][Status][Discuss] Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle

POJ 2389 Bull Math(大数乘法,还是Java好)

Bull Math Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14252   Accepted: 7350 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. F

POJ 2389 Bull Math(大数相乘)

Bull Math Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13920   Accepted: 7192 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. F

Poj OpenJudge 百练 2389 Bull Math

1.Link: http://poj.org/problem?id=2389 http://bailian.openjudge.cn/practice/2389/ 2.Content: Bull Math Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13067   Accepted: 6736 Description Bulls are so much better at math than the cows. The

BZOJ1753: [Usaco2005 qua]Who&#39;s in the Middle

问n<=10000个点的中位数. 水题必有玄机!(然后浪费了半天在怎么O(n)求中位数并且最后放弃了) 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include<cstdlib> 5 //#include<iostream> 6 using namespace std; 7 8 int n; 9 #define maxn 10011 10 int a[