Frog Frog Recommended: Demonstrate Usage (C #)

zhaozj2021-02-16  82

// Frog Frog Recommendation: Demonstrates the Usage of the Corpled Indicator (C #) // This demo tests the number of prime numbers in the continuous natural number, the number is that in addition to 1 and it itself can be kept, it cannot be used by others ./ Sorry I didn't understand how the mathematics was not good. I didn't understand how to judge the rigid number, but this does not affect the establishment and use of the index indicator. Who knows, I know, I will tell you, if you have more comments, if you are more Remove all comments, code is still very compact, no way, now you have to do some very basic practice .using system; // Import Namespace

Class BitArray // Defines a class BitATATATARRAY {Int [] bits; // Defines an INT type INT page; / / Defines an INT variable, note: The variable without modifiers is default the private variable public bitArray (int lay "{/ / Define this type of constructor, use the same name function in C # to use the same name function, in VB.NET define if (Length <0) throw new argumentException (); // If the length is less than 0 Out a argumentException exception Bits = new int = ((length-1) >> 5) 1]; // assign a value to the BITS array, minus Length 1 to right shift 5 final plus 1 this.length = length // use the parameter length to assign a value, pay attention to the use of the THIS keyword, which means the class being referenced, that is, Class BitArray, this is another usage}

Public int length {// Set a read-only attribute, get {return length;} // Attributes return the value of private variables Length, pay attention to the case, }呱.

Public Bool this [int index] {// Build a readable write index indicator GET {// Setting the GET indicator if (INDEX <0 || index> = length) {// If the transmitted index value is less than 0 or greater than The array boundaries trigger a indexoutofrangeException exception throw new indexoutofrangeException ();} return (bits [index >> 5] & 1 << index)! = 0; //, if it is a magple number, return 1, if not the number, return 1, don't Ask me this algorithm and the rigor relationship, I don't know the bottom of the assembly and the assembly.} Set {// Set the set indicator if (Index <0 | INDEX> = Length) {// and get access Like, check whether the input index is within the appropriate range, in the actual programming should also be responsible for these potential errors, which enhances the robustness of the program throw new indexoutofrangeException ();} if (value) {bits [index >> 5] | = 1 << Index;} else {bits [index >> 5] & = ~ (1 << index);}}}}

Class sample // Create a Sample class {static int count (int max) {// Define a static int type method BitArtArray flags = New BitArray (MAX 1); / / Institute to instantiate the BitArray class, and initialize with the MAX parameter INT count = 1; // Define an int type count variable and initializes 1 for (int i = 2; i <= max; i ) {// Define a loop, range from 2-max because each The number will definitely remove 1, so IF (! Flags [i]) {// Call the index indicator of the instance Flags of the BitArray class, for (int J = i * 2; j <= max; j = i) Flags [j] = true; // Do not understand count ; // every loop once, let Count plus 1,}} return count; // Return Count} static void main (String [] args) {// Set the main function, Note that the array is used as a parameter int max = int.parse (ARGS [0]); / / Get the first value INT count = count (max) of array parameters; // Call the count method to process max, console.writeline "There is a total of {1} quality", max, count); / / to the screen print processing result.}}

转载请注明原文地址:https://www.9cbs.com/read-15643.html

New Post(0)