PAT Basic 1017. A除以B (20)(C语言实现)

本文提供了一个解决PAT竞赛中大数除法问题的方法。该问题要求计算一个不超过1000位的大整数与一位数相除的商和余数。通过模拟手动除法过程,使用C语言实现了一种有效算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我的PAT系列文章更新重心已移至Github,欢迎来看PAT题解的小伙伴请到Github Pages浏览最新内容。此处文章目前已更新至与Github Pages同步。欢迎star我的repo

题目

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

输入格式:

输入在一行中依次给出 AB ,中间以 1 空格分隔。

输出格式:

在一行中依次输出 QR ,中间以 1 空格分隔。

输入样例:

123456789050987654321 7

输出样例:

17636684150141093474 3

思路

就是模拟一个手算除法的过程。

代码

最新代码@github,欢迎交流

#include <stdio.h>

/* read 2 digits from highest digit of A, do manual division, get the quotient
 and remainder. Read one more digit, combine this with the last remainder to
get a new 2-digits number. Do this until read to the end of A */

int main()
{
    int B;
    char A[1001], *p = A;
    scanf("%s %d", A, &B);

    /* the results are stored in A and B instead of printed out on-the-fly */
    int twodigit, remainder = 0;
    for(int i = 0; A[i]; i ++)
    {
        twodigit = remainder * 10 + (A[i] - '0');
        A[i] = twodigit / B + '0';
        remainder = twodigit % B;
    }
    B = remainder;

    /* print */
    if(A[0] == '0' && A[1] != '\0') p++;
    printf("%s %d", p, B);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值