高精度加法与高精度乘法详解

目录

1.两数之和(高精度)

2.两数相乘(高精度)


1.两数之和(高精度)

题目描述

高精度加法,相当于a+b problem,不用考虑负数.

输入格式

分两行输入。a,b≤10^{500}

输出格式

输出只有一行,代表a+b的值

#include<bits/stdc++.h>
using namespace std;
int main(){
    char a[100001], b[100001];//将数字以字符数组的形式输入
    cin>>a>>b;
    int jw = 0, n1, n2, maxn;//maxn为输入的两个数中位数更大的位数
    int aa[100001], bb[100001];
    n1 = strlen(a);//n1为输入的第一个数的位数
    n2 = strlen(b);//n2为输入的第二个数的位数
    for(int i = 0;i<n1;i++){
        aa[i] = a[n1-i-1]-'0';//将存入的第一个数以整形数组的形式存储,各个数位对应整型数组的各个值
    }
    for(int i = 0;i<n2;i++){
        bb[i] = b[n2-i-1]-'0';//将存入的第二个数以整型数组的形式存储,各个数位对应整型数组的各个值
    }
    if(n1>n2)
        maxn = n1;
    else
        maxn = n2;
    int sum[maxn];
    for(int i = 0;i<maxn;i++){
        sum[i] = aa[i]+bb[i]+jw;
        jw = sum[i]/10;
        sum[i] = sum[i]%10;
    }
    if(jw==1)
        cout<<jw;//若最高位有进位,单独数位最高的进位值,及和的最高位
    for(int i = maxn-1;i>=0;i--){
        cout<<sum[i];
    }
}

2.两数相乘(高精度)

题目描述

求两数的积。

输入格式

两行,两个整数。每个数字不超过 10^{2000},需用高精。

输出格式

一行一个整数表示乘积。

找规律,当相乘各数位不存在进位时:

 当数位存在进位时:

 c[k] = a[i]*b[j],其中k= i+j,用二重for循环实现遍历

#include<bits/stdc++.h>
using namespace std;
int main(){
    char a[100001], b[100001];//将数字以字符数组的形式输入
    cin>>a>>b;
    int aa[100001],bb[100001];
    int jw = 0, n1, n2, flag = 0;
    n1 = strlen(a);//n1为输入的第一个数的位数
    n2 = strlen(b);//n2为输入的第一个数的位数
    for(int i = 0;i<n1;i++){
        aa[i] = a[n1-i-1]-'0';//将存入的第一个数以整形数组的形式存储,各个数位对应整型数组的各个值
    }
    for(int i = 0;i<n2;i++){
        bb[i] = b[n2-i-1]-'0';//将存入的第二个数以整形数组的形式存储,各个数位对应整型数组的各个值
    }
    int maxn = n1+n2-1;
    int c[maxn];
    memset(c,0,sizeof(c));//将数组值全部初始化为0
    for(int i = 0;i<n1;i++){
        for(int j = 0;j<n2;j++){
            c[i+j] = c[i+j]+aa[i]*bb[j];//不进位时乘积各个位数的值
        }
    }
    for(int i = 0;i<maxn;i++){//进位操作
            c[i] = c[i]+jw;
            jw = c[i]/10;
            c[i] = c[i]%10;
    }
    if(jw!=0)
        cout<<jw;
    for(int i = maxn-1;i>=0;i--){
        if(c[i]!=0||i==0)//计算出来的乘积最高数位不能为0,除非最后一位为0
            flag = 1;
        if(flag == 1)
            cout<<c[i];
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems. This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25. 输入说明 The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9. 输出说明 The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer. 输入样例 95.123 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12 输出样例 548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.126825030131969720661201 小提示 If you don't know how to determine wheather encounted the end of input: s is a string and n is an integer C++ while(cin>>s>>n) { ... } c while(scanf("%s%d",s,&n)==2) //to see if the scanf read in as many items as you want /*while(scanf(%s%d",s,&n)!=EOF) //this also work */ { ... } 来源 East Central North America 1988 北大OJ平台(代理

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值