"Algorithmic Competition Primer Classic" Exercise 2-5 fractional Decimal (decimal)

Source: Internet
Author: User
Original Question

Test instructions: Input positive integer a,b,c, output A/b decimal form, accurate to C bit after decimal point. Among them a,b≤106, c≤100. The input contains multiple sets of data and the end tag is a=b=c=0.

Test data:

Sample input:
1 6 4
0 0 0

Sample output:
Case 1:0.1667 Problem solving thinking

This problem is integer rounding, take the remainder of the exercises.

This thought could be solved directly with C + + setprecision (c), but the test found that once C is greater than 16, the output of the floating-point number is followed by a total of 0, because the effective precision of double is only 16 bits.

So it is necessary to manually simulate the operation of division, with the elementary school when the study of the non-stop "multiply 10" method can be solved. First output the number before the decimal point, because the last one in the test sample needs to be rounded, so the first c-1 bit is output, and the last C bit is rounded based on the c+1 bit. AC Code

#include <iostream>
#include <cstdio>

using namespace std;

int main (int argc, char const *argv[])
{
    int a,b,c,kase = 0;
    while (~SCANF ("%d%d%d", &a, &b, &c))
    {
        if (!a &&!b &&!c) break;
        kase++;
        First output the digits before the decimal point
        printf ("Case:%d:%d.", Kase, A/b);
        a%= b;
        C-1 bits after the decimal point for
        (int i = 0; i < c-1; ++i) 
        {
            a *=
            ; printf ("%d", A/b);
            a%= b; A is the remainder of a c-1 bitwise operation when jumping out
        }
        //Consider the last one rounded
        int more = ((a*10)%b *)/b;//Observe the next bit of the C-bit
        if (more >= 5)
            p rintf ("%d\n", (a*10)/b + 1);
        else printf ("%d\n", (a*10)/b);
    }
    return 0;
}

The test is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.