Content:
The definition of an array is: data type [] variable name =new data type [n] variable name. Length indicates the value of the array;
36 selected 7 random number is not duplicated
Static voidMain (string[] args) { int[] A =New int[7];//1) Create a one-dimensional array first Random rand=NewRandom ();//2) defines stochastic computer generated random number; for(inti =0; I <7; i++//3) To remove the repeated numbers by screening {int n =Rand. Next (36);//randomly generate a number of 36 0-35;n++;// display 1-36 bool Cunzai = false;//mark can be either bool number or F or (int j = 0; J < A.length; J + +)//a.length means the length (value) of the array {if (A[j] = = N) {Cunzai = true; To skip when there are equal in the array; ; } } //existence does not exist? If it doesn't exist, put it in a array, and if it exists, regenerate it . if(Cunzai = =false)//{A[i]=n;//n into the array}Else{i--;//Because it does not conform to the operation to re-perform the random number, "i++" needs to be reduced before this;}} //Show for(inti =0; i < a.length; i++) {Console.Write (A[i]+"\ t"); Output number}}
Binary Take-over:
Static void Main33 (string[] args)
{ //Find int[] A =New int[8] {2,9, One, -, +, -, -, + }; //Enter the number you want to find intFind =Convert.ToInt32 (Console.ReadLine ()); // Subscript intKaishi=0, jieshu=a.length-1, Zhongjian; for (; ; ) {Zhongjian= (Kaishi + Jieshu)/2; if(A[zhongjian] = =find) {Console.WriteLine ("got it."); Break; } Else { if(Find >A[zhongjian]) {Kaishi= Zhongjian +1; } Else{Jieshu= Zhongjian-1; } // if(Jieshu <Kaishi) {Console.WriteLine ("didn't find"); Break; }}}} to find the number and determine the location Static voidMain2 (string[] args) { //Find int[] A =New int[8] {2,9, One, -, +, -, -, + }; //Enter the number you want to find intFind =Convert.ToInt32 (Console.ReadLine ()); //Find BOOLFound =false; for(inti =0; I <= a.length-1; i++) { if(A[i] = =find) { //OutputFound =true; Console.WriteLine ("The number of {0} is found.", i +1); Break; } } //output found or not found if(Found = =false) {Console.WriteLine ("not found"); } }
C # Learning Array 36 Select 7 Random number non-repetition/binary