考前复习 之 高精模板

part 1
高精度练习之加法

注意:位数 不同吧 还有可能比 位数最长的还要长

#include<cstdio>
#include<algorithm>
#include<cstring>
//by mars_ch
using namespace std;
char a[1005],b[1005];
int ans[1005],tot;
int main()
{
    scanf("%s",a);
    scanf("%s",b);
    int l1=strlen(a);
    int l2=strlen(b);
    int maxx=max(l1,l2);
    if(l2 == maxx)
    {
        swap(l1,l2);
        for(int i=0;i<maxx;i++)
        {
            swap(a[i],b[i]);
        }
    }
    for(int i=1;i<=maxx;i++)
    {
        int p1=l1-i;
        int p2=l2-i;
        //printf("%c %c\n",a[p1],b[p2]);
        if(p2>=0)
        {
            ans[++tot]+=a[p1]-'0'+b[p2]-'0';
        }
        else
        {
            ans[++tot]+=a[p1]-'0';
        }
        //printf("now number is %d\n",ans[tot]);
        if(ans[tot]>=10) ans[tot+1]++;
        ans[tot]%=10;

    }
    if(ans[tot+1]) tot++;
    for(int i=tot;i>=1;i--)
    {
        printf("%d",ans[i]); 
    }
    puts("");
    return 0;
}

part2
乘法
模拟数式运算

主要在于 乘完之后加在哪位写挂了。
a1-i+a2-j-1
这个东西吗

#include <cmath>

#include <cstdio>

#include <cstring>

#include <iostream>

using namespace std;



int a[10000];

string s1,s2;



void addd()

{

int a1=s1.size(),a2=s2.size();

for (int i=0;i<a1;i++)

 for (int j=0;j<a2;j++)

   a[a1-i+a2-j-1]+=(s1[i]-'0')*(s2[j]-'0');



for (int i=1;i<=a1+a2;i++) { a[i+1]+=a[i]/10; a[i]%=10; }

a[0]=a1+a2; while (a[a[0]]==0&&a[0]>1) a[0]--;

}



int main()

{

    cin>>s1>>s2;

    memset(a,0,sizeof(a));

    addd();

    for (int i=a[0];i>=1;i--) cout<<a[i];

    cout<<endl;


return 0;

}

暂时就这样吧 睡觉了
明天更新减除

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值