Xiaobai vs. PAT: Grade B B017

Write at the top:

A few days ago, I got two important ways to communicate with my classmates:

1. Do not write ready-made functions, such as qsort, which will become a deduction point (unless the title requires you to write);

2. Give priority to the boundary value, and then consider the general situation, so that it is not easy to make mistakes; (this question)


1. Topic information

This problem requires the calculation of A/B, where A is a positive integer with no more than 1000 digits and B is a 1-digit positive integer. You need to output the quotient Q and the remainder R such that A = B * Q + R holds.

Input format:

The input gives A and B sequentially in 1 line, separated by 1 space.

Output format:

Output Q and R sequentially in 1 line, separated by 1 space.

2. Basic ideas

Because it is a 1000-digit number, only an array can solve this problem, then the first method to consider is to divide every two bits (i, i+1) by B, output the quotient, and assign the remainder to i+1, and perform next operation.

Third, the specific implementation code and the problems encountered

#include<stdio.h>

#include<string.h>

intmain ()

{

    char number[1000];

    int n,i,sum=0;

    unsigned long m;

    scanf("%s %d",number,&n);

    m = strlen (number);  /*m represents the number of digits in the input number*/

    

    if(m == 1)

        printf("%d %d\n",(number[0]-48)/n,(number[0]-48)%n);

    

    for(i=0,number[0] -= 48;i<m-1;i++)     /*没减干净,继续*/

    {

        number[i+1] -= 48;

        sum=(number[i]) * 10 + (number[i+1]);/*前两位的和,商存入Q,余数存入number[i+1]*/

        if(i==m-2)

            printf("%d %d\n",sum/n,sum-(sum/n)*n);

        else

        {

            printf("%d",sum/n);

            number[i+1] = sum % n;

        }

    }

}

评测结果

时间 结果 得分 题目 语言 用时(ms) 内存(kB) 用户
5月08日 15:19 答案正确 20 1017 C (gcc 4.7.2) 2 264 chauncyyoung

测试点

测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 1 264 12/12
1 答案正确 2 264 2/2
2 答案正确 2 264 2/2
3 答案正确 2 256 2/2
4 答案正确 2 264 2/2
这是最终版代码,与我的最初思路相比,最大的差异就是对于输入为个位数时的考察。个位数如果不单独讨论会因为i+1位没有赋值而出错(陈姥姥好坏的说~)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326661700&siteId=291194637