The legendary hundred-degree question uses C # For binary calculation to obtain the O (n) algorithm that does not contain multiple words in 0.25 billion digits.

Source: Internet
Author: User
Tags net bug

Using system;
Using system. IO;
Using system. collections;

Namespace bucketsort
{
Class Program
{
Public const int maxcount = 25*1000*10000; // The value of 0.25 billion is not big and has not exceeded the Integer Range.
Public const string filename = @ "C:/test.txt ";

Public static void main (string [] ARGs)
{

// Write a file for experimentation. You can write it once;
// Writefile ();
// Console. Readline ();
// Return;

Filestream filereader = new filestream (filename, filemode. Open );

// Read all positive integers cyclically. If one integer is read each time, one bit is generated and the mark at the end of reading is not read;

Int Index = 0;
// Create two bitarrays long enough
// A. net bug was found, that is, bitarray cannot be constructed according to int. maxvalue. Therefore, it restricts the number range and is expected to be modified by Microsoft.
// Even in the maximum Integer Range,-2,147,483,648 to 2,147,483,647 is 0.4 billion bits and more than 0.1 billion bytes. The new containers should be 2 containers of more than 100 m, within the range of M.
// An array indicates whether the number exists.

Bitarray countorsingle = new bitarray (250000000 + 1 );

// The second array indicates whether the number appears multiple times.
Bitarray countormore = new bitarray (250000000 + 1 );

// Cyclic detection. The complexity is O (n)
While (true)
{
Byte [] tempbyte = new byte [4];
Index + = filereader. Read (tempbyte, 0, 4 );
Int tempint = system. bitconverter. toint32 (tempbyte, 0 );

If (tempint =-1)
{
Break;
}
Else
{
If (countorsingle. Get (tempint ))
{
// Appears multiple times. Set this value. The complexity is O (1)
Countormore. Set (tempint, true );
}
Else
{
//// When this occurs, set this value. The complexity is O (1)
Countorsingle. Set (tempint, true );
}
}
}
Filereader. Close ();

// Remove countormore from countorsingle and use logic XOR. The complexity is O (n)
// If the XOR operation has the same value, the value is false, = 1; = 1; = 0; 0, 0 = 0, and the first side has ruled out the possibility.
Countorsingle = countorsingle. XOR (countormore); // complexity is O (n)
// Read the negative number file again.

// The key moment has arrived.
Int sum = 0;

// Complexity is O (n)
Foreach (bool isexists in countorsingle)
{
If (isexists)
{
Sum ++;
}
}

Console. writeline ("the total computing complexity is O (n), and the result is:" + sum );
Console. Readline ();
// A few minutes later, the screen shows that the total computing complexity is O (n) and the result is: 91836109.

}
Public static long getindex (int x)
{
// The subscript of Int. minvalue is 0, and the subscript of negative integer N is 0-(long) int. minvalue;
// The subscript of 0 is located in 1-(long) int. minvalue. The subscript of positive integer N is n + 1-(long) int. minvalue;
If (x> = 0)
{
Return (x + 1-(long) int. minvalue)/4;
}
Else
{
Return (0-x)/4;
}
}
Public static void writefile ()
{
// Write a file for experimentation. You can write it once. The positive and zero values are written to the positive number file, and the negative values are written to the negative number file.
// Write two files because only the constructor of the. NET bitarray class is int. If both are written together, the processing range is exceeded.

Filestream filewriter = new filestream (filename, filemode. openorcreate );

System. Random Rand = new random ();
Int I = 0;
For (; I <maxcount; I ++)
{
// Write 0.25 billion integers. One integer is 4 byte [] and is random within the range of 250000000;
// A. net bug is found, that is, bitarray cannot be constructed according to int. maxvalue.
// Therefore, we have to limit it to 250000000. First, we only consider positive numbers and 0,
// You can change the negative number and put it in another file for calculation.
Int tempint = Rand. Next (0, 250000000 );
Filewriter. Write (system. bitconverter. getbytes (tempint), 0, 4 );

}
// Write a-1 mark at the end.
Filewriter. Write (system. bitconverter. getbytes (-1), 0, 4 );
Filewriter. Close ();
// Depends on more than 970 MB of files. Delete the experiment.
Console. writeline ("file writing is complete! ");
}

}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.