1121 -- 分数化小数

分数化小数

Time Limit:1000MS  Memory Limit:65536K
Total Submit:181 Accepted:58

Description

输入正整数a,b,c 0 < a, b < 10^6,c< 100输出a/b的小数形式,精确到小数点后的c位,例如a=1 b=6 c=4输出应该是0.1667

Input

输入正整数a,b,c

Output

输出a/b的小数形式,精确到小数点后的c位,例如a=1 b=6 c=4输出应该是0.1667

Sample Input

1 6 4
1 2 20

Sample Output

0.1667
0.50000000000000000000

Source

lrj程序入门

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace Ak1121 {
        class Program {
            static void Main(string[] args) {
                string sb;
                while ((sb = Console.ReadLine()) != null) {
                    string[] s = sb.Split();
                    int a = int.Parse(s[0]), b = int.Parse(s[1]), c = int.Parse(s[2]);
                    int integer = a / b;//整数部分
                    a = a % b;
                    int[] ans = new int[105];
                    for (int i = 0; i <= c; i++) {
                        ans[i] = a * 10 / b;
                        a = a * 10 % b;
                    }
                    int jt = 0;//用于四舍五入
                    if (ans[c] > 5) {
                        jt = 1;
                        for (int i = c - 1; i >= 0; i--)//处理后面来的的进位
                        {
                            ans[i] = (ans[i] + jt) % 10;
                            jt = ans[i] / 10;
                            if (jt == 0) break;
                        }
                    }
                    Console.Write(integer + ".");
                    for (int i = 0; i < c; i++)
                        Console.Write(ans[i]);
                    Console.WriteLine();
                }
            }
        }
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值