c #programe数据类型问题 [英] c# programe datatypes problem

查看:129
本文介绍了c #programe数据类型问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好每一个..

i我试图在c#中制作一个控制台程序,其中我要计算一个数字除数的乘积。

例如数字是12然后它的除数是2,3,4,6。那么输出应该是2 * 3 * 4 * 6 = 144.

这是我的代码..

 使用系统; 
使用 System.Text;
命名空间 ConsoleApplication1
{
class 程序
{
静态 void Main( string [] args)
{
Console.Clear();
ulong b = 0 ; ulong c = 1 ;
b = Convert.ToUInt32(Console.ReadLine());
for ulong i = 2 ; i < = b / 2; i ++)
{
if (b%i == 0
{
c = c * i;
}
}
Console.WriteLine(c);
}
}
}



现在我的问题是我想为10000或100000这样的大数字麻木运行这个程序或者2-3万卢比可能是。

但它只为小数字提供正确的输出。它没有为10000这样的大数量提供正确的输出,其除数的乘积会非常大。

i试图使用高数据类型,但它没有给出正确的输出。

<发生了什么问题......

请programersss帮助我........... !!!

解决方案

请参阅此类型: http://msdn.microsoft。 com / en-us / library / system.numerics.biginteger.aspx [ ^ ]。



注意这种类型:创建需要的数字太简单了内存比系统中所有可用内存多。顺便说一句,这是一个有用的简单小学数学和编程练习。



-SA


< blockquote>问题是你开始非常非常快地开始购买非常非常大的产品:

看看512的简单明显的除数:

 256,128,64,32,16,8,4,2 



将它们相乘:

 256 * 128 * 64 * 32 * 16 * 8 * 4 * 2 = 68719476736 
或0x1000000000

其中 已经 不适合32位整数!

查看除数表: http://www.positiveintegers.org/IntegerTables/ 501-600 [ ^ ],您将看到这些值中的一些可以有20个或更多的除数!

这些数字很快就会 然后只会达到10,000或者更差的100,000花费相当多的时间并产生一些令人难以置信的大数字。



是的,你可以做到 - 但你需要 BigInteger 而不是 ulong 并且一旦你生成了数字,你就不会对这些数字做多少了!



我不知道你为什么要这样做,但说实话:我不打扰!


hello every one..
i am trying to make a console programe in c# in which i want to calculate the product of divisors of a number.
for example the number is 12 then its divisors are 2, 3, 4 ,6. then output should be 2*3*4*6=144.
Here is my code..

using System;
using System.Text;
namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.Clear();
      ulong b=0; ulong c = 1;
      b = Convert.ToUInt32(Console.ReadLine());
      for (ulong i = 2; i <=b/2; i++)
      {
        if (b % i == 0)
        {
          c = c * i;
        }
      }
      Console.WriteLine(c);
    }
  }
}


Now my problem is that i want to run this programe for large numeric numbrs like 10000 or 100000 or 2-3 lakhs may be.
but it gives correct output only for small numbers. it does not give correct output for large number like 10000 whose product of divisors will be very large.
i tried to use high data types but its not giving correct output.

what is going wrong...
Please programersss Help me...........!!!

解决方案

Please see this type: http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx[^].

Be careful with this type: it's way too easy to create a number which needs more memory than all available memory in your system. By the way, this is a useful simple exercises in elementary mathematics and programming.

—SA


The problem is that you start getting to very, very large products, very, very quickly:
Look at the simple and obvious divisors of 512:

256, 128, 64, 32, 16, 8, 4, 2


Multiply them together:

256 * 128 * 64 * 32 * 16 * 8 * 4 * 2 =   68719476736
                                     or 0x1000000000

Which alreadydoesn't fit into a 32 bit integer!
Have a look at the divisors table: http://www.positiveintegers.org/IntegerTables/501-600[^] and you will see that some of these values can have 20 or more divisors!
These numbers get big quickly and just getting to 10,000 or worse 100,000 is going to take considerable time and generate some incredibly large numbers.

Yes, you can do it - but you will need BigInteger rather than ulong and there isn't going to be much you can do with the numbers once you have generated them!

I don't know why you are doing this, but to be honest: I wouldn't bother!


这篇关于c #programe数据类型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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