[C #] machine on the second experiment

 

Experiment 1:

Solution 1/1 + 1/2 + 1/3 + 1/4 ...... + 1 / i =?

To ensure the accuracy of 1e-6.

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace MyProject1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             double eps = Math.Pow(10, -6);
13             double Eps = 1e-6;
14             double ans = 0.0;
15             int Last = 0;
16             for (int i = 1; 1.0 / i >= Eps; i++)
17             {
18                 ans = ans + 1.0 / i;
19                 Last = i;
20             }
21             double C = 0.57721566490153286060651209;
22             Console.WriteLine( "Last = {0}",Last);
23             Console.WriteLine( "1/1 + 1/2 + .... + 1/i  = {0} " , ans );
24             Console.WriteLine( "check : ln({0}) = {1}" ,  Last+1,Math.Log(Last,Math.E)+C );
25         }
26     }
27 }
Solving cycle

 


 

 

Experiment 2:

Practice using the following method:

Array-based operation, Sort, Reverse, IndexOf, Contains ()

Generating a random number range: Next (A, B) [A, B)

 

. 1  the using the System;
 2  the using the System.Collections.Generic;
 . 3  the using the System.Linq;
 . 4  the using the System.Text;
 . 5  
. 6  namespace MyProject2
 . 7  {
 . 8      class Program
 . 9      {
 10          static  void the Main ( String [] args)
 . 11          {
 12 is              / * 
13              define a one-dimensional array, which is inputted from the keyboard number of elements, the elements having the value [100, 200] of the random integer.
14              of each member (1) the value of the output array
 15              (2) members of the array elements of the array in ascending order, sorting output
 16             (3) an input from the keyboard integer, to find the presence or absence of an integer, if the output of the index in its presence, if there is given the message "This data does not exist."
17              (4) against the opposite array, and outputs the sorted array elements.
18              * / 
. 19  
20 is              / * Step 1: Enter a value of n * / 
21 is              Console.WriteLine ( " Please enter an integer n: " );
 22 is              int n = Convert.ToInt32 (Console.ReadLine ());
 23 is              int [ ] a = new new  int [n-];
 24  
25              / * Step: randomly generating [100, 200] of the random integer * / 
26 is              the random = R & lt new new the random ();
 27              for ( int I =0 ; I <n-; I ++ )
 28              {
 29                  A [I] = R.Next ( 100 , 201 );
 30              }
 31 is  
32              / * Each member of value (1) of the output array * / 
33 is              Console.WriteLine ( " ( 1) of each member of the array output value " );
 34 is              the foreach ( var Item in A)
 35              {
 36                  Console.Write ( " {0} \ T " , Item);
 37 [              }
 38 is             Console.WriteLine ( " \ n-______________________________________ " );
 39  
40              / * (2) members of the array in ascending order, the array element outputting sorted * / 
41 is              Console.WriteLine ( " (2) array in ascending order " );
 42              Array.Sort (A);
 43 is              the foreach ( var Item in A)
 44 is              {
 45                  Console.Write ( " {0} \ T " , Item);
 46 is              }
 47              Console.WriteLine ( "\ n-______________________________________ " );
 48  
49              / * (. 3) input from the keyboard an integer, that integer to find whether there is, if the output of it in the presence of the subscript,
 50               * if there is given the message" This data does not exist. " * / 
51 is  
52 is              Console.WriteLine ( " (. 3) Please enter a number to be the search: \ n- " );
 53 is              int X = int .Parse (Console.ReadLine ());
 54 is  
55              IF (a.Contains (X) )
 56 is              {
 57 is                  Console.WriteLine ( " \ n-looking subscript number is: {0} (subscripts starting with 0) " , the Array.indexOf (a, X));
 58              }
 59             the else 
60              {
 61 is                  Console.WriteLine ( " \ n-no data exists " );
 62 is              }
 63 is              Console.WriteLine ( " \ n-______________________________________ " ;)
 64  
65              / * array element (4) is set to the inverse array, and outputs the ordering . * / 
66              Console.WriteLine ( " (. 4) Reverse array: " );
 67              Array.reverse (A);
 68              the foreach ( var Item in A)
 69              {
 70                 Console.Write("{0}\t", item);
71             }
72             Console.WriteLine("\n______________________________________");
73         }
74     }
75 }
Array class methods practice

 


 

 

Experiment 3:

Exercise string split

Split and its corresponding parameters StringSplitOptions.RemoveEmptyEntries

 

. 1  the using the System;
 2  the using the System.Collections.Generic;
 . 3  the using the System.Linq;
 . 4  the using the System.Text;
 . 5  
. 6  namespace MyProject3
 . 7  {
 . 8      class Program
 . 9      {
 10          static  void the Main ( String [] args)
 . 11          {
 12 is              / * 
13              given string "at the Quick Brown Box jumped over lazy at the Dog. An keeps the Apple a Day at the Doctor Away. of Can a Dog BE a Fox and Friends?"
 14              statistical word "the" number of occurrences in the string.
15             */
16             string S = "The quick brown box jumped over the lazy dog. An apple a day keeps the doctor away. Can a fox and a dog be friends?";
17 
18             string[] str = S.Split(new char[]{' ','.','?'},StringSplitOptions.RemoveEmptyEntries);
19             const string text = "the";
20 
21             int cnt = 0; 
22             foreach (var Item in STR)
 23 is              {
 24                  IF (item.ToLower () == text)
 25                  {
 26 is                      CNT ++ ;
 27                  }
 28              }
 29              Console.WriteLine ( " Statistical word" the "number of occurrences in the string is: {0 } times " , CNT);
 30          }
 31 is      }
 32 }
Split string of

 

Guess you like

Origin www.cnblogs.com/Osea/p/11610990.html