PAT乙级 1017 A除以B (20分) 简单高精度除法

在这里插入图片描述

代码如下:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<string>
#include<vector>
#define MAX 500005
typedef long long ll;
using namespace std;

int a[1005],b,c[1005],cnt=0,m;//c记录的是商,m是最后的余数
string s;

int main(){
    cin>>s>>b;
    int len=s.length();
    for(int i=0;i<len;i++){
        a[i]=s[i]-'0';
    }
    int tmp=0;//当前的被除数
    for(int i=0;i<len;i++){
        tmp=tmp*10+a[i];
        if(tmp>=b){
            c[cnt++]=tmp/b;
            tmp%=b;
        }else{
            if(i!=0){//保证不出现高位的0
                c[cnt++]=0;
            }
        }
        if(i==len-1){
            m=tmp-b*(tmp/b);
        }
    }
    for(int i=0;i<cnt;i++){
        printf("%d",c[i]);
    }

    if(cnt==0){//注意2 7这组数据,商为0不能不输出
        printf("0");
    }
    printf(" %d",m);
    return 0;
}
发布了253 篇原创文章 · 获赞 15 · 访问量 7983

猜你喜欢

转载自blog.csdn.net/weixin_44123362/article/details/103913524