bLue的除法算术题

来源:互联网 发布:手机淘宝怎么清理缓存 编辑:程序博客网 时间:2024/04/29 09:50

bLue的除法算术题

Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic

Problem Description

bLue 最近接了个重活,需要帮助小学生手算大量的除法算术题,这可把他累坏了。

但是,机智的 bLue 一想,写个 “printf("%f", (double)a/b);” 不就完事了嘛。可是他最近忙得都没空开电脑啦,你能帮他写一下吗?

Input

输入数据有多组(数据组数不超过 20),到 EOF 结束。

每组输入两个整数 a, b (1 < = a, b < = 2^16) 且保证运算结果一定是有限小数。a, b 同时为 0 时输入结束。

Output

 输出 a/b 的运算结果,bLue 想要的格式参见示例。

Example Input

1 22 41 88 40 0

Example Output

0.50.50.1252

Hint

Author

「2016级ACM集训队第一次选拔赛」bLue



#include <stdio.h>


int main()
{
    int a, b;
    while(scanf("%d %d", &a, &b), a|b)
    {
        int integer = a/b; // 整数部分
        int decimal[20]; // 存储每一位小数
        int cnt = 0; // 小数位数
        a = (a%b)*10; // 已计算出整数部分,故除一次,开始计算小数部分
        while(a)   // 余数不为 0 时不断地除
        {
            decimal[cnt++] = a/b;
            a = (a%b)*10;
        }
        printf("%d", integer);   //cent=22 时,跳出循环
        if(cnt > 0)   // 仅当有小数部分时才输出
        {
            printf(".");
            for(int i=0; i<cnt; ++i)
            {
                printf("%d", decimal[i]);
            }
        }
        printf("\n");
    }


    return 0;
}
//手动除法,当C语言的常规方法不适应时。尝试回到数学;
0 0
原创粉丝点击