1017 A除以B(字符串问题)

1017 A除以B(20 分)

本题要求计算 A/B,其中 A 是不超过 1000 位的正整数,B 是 1 位正整数。你需要输出商数 Q 和余数 R,使得 A=B×Q+R 成立。
输入格式:

输入在一行中依次给出 A 和 B,中间以 1 空格分隔。
输出格式:

在一行中依次输出 Q 和 R,中间以 1 空格分隔。
输入样例:

123456789050987654321 7

输出样例:

17636684150141093474 3

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    char ch[2000];
    int b,temp = 0,flag = 0;
    scanf("%s %d",ch,&b);
    int len = strlen(ch);
    for(int i = 0; i < len; i++)
    {
        temp = ch[i] - '0' + temp * 10;//逐位转化 
        if(temp>=b)//如果可以除,则除 
        {
            printf("%d",temp/b);
            flag = 1;
        }
        else if(flag)//如果不能则在该位补0 
            printf("0");
        temp = temp % b;//求余数 
    }
    if(!flag)//如果没能除以b,则代表此数与b无最大公因数 
        printf("0");
    printf(" %d",temp);
}

猜你喜欢

转载自blog.csdn.net/qiulianshaonv_wjm/article/details/82353719
今日推荐