文章

2

粉丝

89

获赞

2

访问

1.1k

头像
A除以B 题解:
P1851 北京师范大学2019年机试题
发布于2023年7月10日 15:37
阅读数 497

// Created by JZhang
#include <bits/stdc++.h>
using namespace std;

void divide(int a[], int b, int Q[], int len){
    int t=0, r, temp=a[0];
    for(int i=0; i<len; i++){
        Q[i]=temp/b;
        r=temp%b;
        temp=r;
        if(temp<b) temp=r*10+a[i+1]; //每次相除的余数小于b的话需要再补一位 
    }
    //计算前导0的数量 
    for(int i=0; i<len; i++){
    	if(Q[i]==0) t++;
    	else break;
	}
    for(int i=t; i<len; i++)
        cout<<Q[i];
    cout<<" "<<r<<endl;
}

int main(){
    string A;
    int R, b;
    while(cin>>A>>b){	
    	int len=A.length();
    	int* a=new int[len];
    	int* Q=new int[len];
    	for(int i=0; i<len; i++)
        	a[i]=A[i]-'0';      //数组a存放被除数的每一位 
    	divide(a, b, Q, len);
	}
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发