算法题: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 != '' || 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; }

原文地址:https://www.cnblogs.com/zsychanpin/p/7093915.html