I wrote quite a nice quick code:

using System; using System.Globalization; namespace OneProgram { class Program { static void Main(string[] args) { double num = double.Parse(Console.ReadLine()); Console.WriteLine(num.ToString("N0", new CultureInfo("en-us"))); } } } 

Everything would be fine, but if you enter a number that is larger than UInt64 (in the condition the number is not more than 10 ^ 100), it turns out Not what's needed

Can this be overcome without cycles, etc.?

Here is the whole task: http://informatics.mccme.ru/moodle/mod/statements/view.php?chapterid=1599

  • You have to use BigInteger - Andrew NOP
  • @AndreyNOP Forgot to indicate that the number on the tests may be slightly less than 10 ^ 100 (googol) - Arasfon
  • four
    BigInteger suits you perfectly, unlike any primitive types - Andrey NOP
  • 2
    Yes, what have the UInt64.Max ! BigInteger can represent any number. - Andrey NOP
  • one
    Connect the assembly System.Numerics - Andrew NOP

2 answers 2

Add System.Numerics Reference to the project and use the standard BigInteger

 using System; using System.Globalization; using System.Numerics; namespace OneProgram { class Program { static void Main(string[] args) { BigInteger num = BigInteger.Parse(Console.ReadLine()); Console.WriteLine(num.ToString("N0", new CultureInfo("en-us"))); } } } 

Test:

 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 10,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000 

    We do in lines

     string bign = "11212131342354364364656121"; StringBuilder sb = new StringBuilder(bign.Length * 4 / 3); int jj = bign.Length % 3; sb.Append(bign[0]); for(int i = 1; i < bign.Length; i++) { if(i % 3 == jj) { sb.Append(","); } sb.Append(bign[i]); } 

    Testing here - https://ideone.com/FDTl5Q

    Yes, and I will add that in the answer I will demonstrate the idea of ​​working with lines, and making the decision go through all the tests is your task.

    • one
      You can slightly optimize if you specify in the constructor capacity : bign.Length * 4 / 3 - Andrey NOP
    • @ AndreyNOP Thanks, I'll add it now - Dmitry Polyanin