算法题:A除以B

题目描述

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

输入描述:

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

输出描述:

在1行中依次输出Q和R,中间以1空格分隔。

输入例子:

123456789050987654321 7

输出例子:

17636684150141093474 3

#include <iostream>
#include<string.h>
#include <sstream>
using namespace std;

bool ComStr1Str2(string s1, string s2)
{
    int n1 = s1.size();
    int n2 = s2.size();
    if (n1 > n2)
    {
        return true;
    }
    else if (n1 < n2)
    {
        return false;
    }
    else
    {
        int i;
        for (i = 0; i < n1; i++)
        {
            if (s1[i] != s2[i])break;
        }
        if (i == n1)return true;
        else
            return false;
    }
}
void DEC(string &s1, string &s2,string &SsStr)
{
    int a = atoi(s1.c_str());
    int b = atoi(s2.c_str());

    int num1 = a / b;
    int num2 = a % b;
    char *buff = new char[3];
    //抵制itoa函数的使用。
    sprintf(buff,"%d",num2);
    s1 = buff;
    sprintf(buff, "%d", num1);
    SsStr += buff;
}
int main()
{

    char inputStr[1000];
    char ch[2];
    cin >> inputStr >> ch;
    if (ch[0] == '0')return 0;

    char *p = inputStr;
    string SaStr;
    string S1;
    string S2 = ch;

    while (*p != '\0' || ComStr1Str2(S1,S2))
    {
        if (!ComStr1Str2(S1, S2))
        {
            S1 += *p;
            p++;
        }
        else
        {
            DEC(S1,S2,SaStr);
        }
    }
    cout << SaStr.c_str() << " " << S1.c_str() << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值