如何在小数点后打印200位数。 [英] How to print 200 digits after decimal point.

查看:83
本文介绍了如何在小数点后打印200位数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.  Given a integer N, print the decimal form of 1/2n.
Example:
N=1, print 0.5
N=2, print 0.25
Adding leading/unsignificant zeroes will lead to wrong answer. Example, printing 0.50 instead of 0.5 in above case will lead to wrong answer.


So correct output of 100 will be 0.0000000000000000000000000000007888609052210118054117285652827862296732064351090230047702789306640625
Output for 50 : 0.00000000000000088817841970012523233890533447265625
Output for 200 : 0.0000000000000000000000000000000000000000000000000000000000006223015277861141016250579646224554209682103144689201443821087459887188032139105827696600570131548979236366311772064357280932527780316951302892084253444264507759697835354018025100231170654296875


$ B $。 b

我得到输出,直到小数点后28-29位数。请建议可以做些什么来获得准确的上述结果..



我的代码:





I am getting output till 28-29 digits after decimal point.Please suggest what can be done to get accurate above mentioned result..

My code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Divideby2
{
    class Program
    {
        static void Main(string[] args)
        {
            int t = Convert.ToInt32(Console.ReadLine());
            int[] a = new int[t];
            for (int i = 0; i < t; i++)
            {
                a[i] =Convert.ToInt32(Console.ReadLine());
            }
            for (int j = 0; j < t; j++)
            {
               double n = a[j];
               string res = (1 / (Convert.ToDouble(Math.Pow(2, n)))).ToString();
               decimal d = decimal.Parse(res, System.Globalization.NumberStyles.Any);
               Console.WriteLine(d);
            }
            Console.Read();
        }
    }
}

推荐答案

我认为你的意思是1 /(2<< ; n)。



嗯,这显然是你应该自己解决的问题,但我会告诉你两种可以解决的方法它。



1.构建自己的类来表示非常大的精度数。正如Stephen建议的那样,你可以使用字节数组,但我认为字符串可以使开发更容易。实现Math.Pow和division的方法。



2.使用C#,使用System.Numerics中内置的BigInt类型。这具有您需要的所有方法,但仅适用于整数。 (提示:将所有内容乘以10乘以200,并在之后坚持下去。)



需要六行代码....
I think you mean 1 / (2 << n).

Well, this is clearly something you are supposed to work out on your own, but I'll help by telling you two ways you could go about it.

1. Build your own class to represent very large precision numbers. As Stephen suggests, you could use byte arrays but I think a string would make development easier. Implement methods for Math.Pow and division.

2. Using C#, use the built in BigInt type in System.Numerics. This has all the methods you need, but only works with integers. (Hint: Multiply everything by 10 to the power of 200 and stick the point in afterwards).

It takes six lines of code....


此任务已设置,因为.Net框架中没有默认数据类型,小数为200位。



声音就像一个很棒的家庭作业。



我的建议是使用一系列的整数。小数点后每个位置一个。



实际上你每次除以2。那样做。



想想长分。你如何进行长期划分?



应用相同的过程,但使用整数数组。
This task has been set because there isn't a default data type in the .Net framework which will to 200 decimal places.

Sounds like an excellent homework assignment.

My advice would be to use an array of ints. One per position in the decimal.

Effectively you're dividing by two each time. So do that.

Think long division. How do you do long division?

Apply the same process but using an array of integers.


这篇关于如何在小数点后打印200位数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆