2017-05-19 3 views
-1

私はハッカーの質問を扱っています。私は正しい出力を表示することができない私はこのプログラムで使用するデータの種類を知らない。非常に大きな数値の正しいデータ型は何ですか?どのように使用されますか?

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
class Solution 
{ 
    static void Main(String[] args) 
    { 
     double n = int.Parse(Console.ReadLine()); 

     double factorial = 1; 
     for (int i = 1; i <= n; i++) 
     { 
      factorial *= i; 
     } 

     Console.WriteLine(factorial); 
    } 
} 

私は、この出力に

 1.5511210043331E+25 

を取得しています。しかし期待される出力が

 15511210043330985984000000 

である私は長い間使用することを考えていますが、私は方法がわかりません。それに対処する方法を教えてください。

ありがとうございます。

+1

私はあなたの代わりにdouble' 'の' BigInteger'を使用することをお勧め。なぜあなたの質問のタイトルが「long」について語っているのか、実際には 'double'を使っているのかは不明です。 –

+0

https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs)を見てください。 110).aspx – Lunyx

+0

私は長く倍以上の能力を持っていると思った – user8038446

答えて

-1

コメントに示唆されているように、BigIntegerを使用することもお勧めです。ここで

はBigIntegerの上のドキュメントです: https://msdn.microsoft.com/en-us/library/system.numerics.biginteger(v=vs.110).aspx

BigIntegerのソリューション:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Numerics; // This is important 

class Solution 
{ 
    static void Main(String[] args) 
    { 
     int n = int.Parse(Console.ReadLine()); // No reason to accept a double here 

     BigInteger factorial = 1; 

     for (int i = 1; i <= n; i++) 
     { 
      factorial *= i; 
     } 

     Console.WriteLine(factorial.ToString()); 
    } 
} 
+0

動作しません。この出力を取得する "7034535277573963776" – user8038446

+0

私は、より良いアプローチを使用して別のソリューションを追加しました。それを試してください。 – Clay07g

+0

その作業。なぜあなたはtostringを使いましたか?それなしで働いている – user8038446

関連する問題