zoukankan      html  css  js  c++  java
  • 分数化小数(decimal)

    输入正整数a,b,c,输出a/b的小数形式,精确到小数点后c位. a, b <=10^6, c <= 100.输入包含多组数据,结束标记为a=b=c=0.

    样例输入:

    1 6 4

    0 0 0

    样例输出:

    Case 1: 0.1667

    分析: c 如果较大,就会超过double型的精度,不能简单地printf("%lf"). 需要自己来计算小数位的数字. 这样就应该用模拟竖式除法来计算了.

    #include <stdio.h>
    
    int main(void){
        int a, b,c;
        int result[101];
        while(scanf("%d%d%d",&a,&b,&c)!=EOF){
        if(a == b && b== c && c == 0){
            break;
        }
        int positive = a/b;
        while(a/b != 0){
            a = a%b;
        }
        for(int i=0; i <=c; i++){
            a = a * 10;
            result[i] = a/b;
            a = a%b;
        }
        if(result[c]>5){
            result[c-1]++;
        }
        for(int i = c-1; i >0; i--){
            if(result[i] >=9){
            result[i]=result[i]%10;
            result[i-1] +=(result[i]/10);
            }
        }
        if(result[0]>9){
            result[0] = result[0]%10;
            positive++;
        }
        printf("%d.",positive);
        for(int i=0; i<c;i++){
            printf("%d",result[i]);
        }
        printf("
    ");
        }
        return 0;
    }

    注意进位处理,以及四舍五入.

  • 相关阅读:
    python向mysql中插入数字、字符串、日期总结
    selenium鼠标事件
    iOS hook
    网络抓包篇
    frida IOS环境搭建
    git
    $emit
    better-scroll无法滚动的问题。
    this.$nextTick()作用
    better-scroll
  • 原文地址:https://www.cnblogs.com/yongjiuzhizhen/p/5900122.html
Copyright © 2011-2022 走看看