Collection of Exercises

Source: Internet
Author: User

Case: The procedure of sorting odd even-numbered is implemented by generics. Int[] nums={1,2,3,4,5,6,7,8,9}; Odd on the left even on the right
list<int> list = new List<int> () {1, 2, 3, 4, 5, 6, 7, 8, 9};
list<int> Odd = new list<int> ();//Odd
list<int> even = new list<int> ();//Even
for (int i = 0; i < list. Count; i++)
//{
If (List[i]% 2! = 0)
//    {
Even.add (List[i]);
//    }
Else
//    {
Odd.add (List[i]);
//    }
//}
List. AddRange (ODD);
List. AddRange (even);
StringBuilder sb = new StringBuilder (); Variable string
for (int i = 0; i < list. Count; i++)
//{
Sb. Append (List[i]);
//}
Console.WriteLine (SB);
Console.readkey ();
Exercise 1: Return the odd number in an int array to a new int array.
The odd number in the array is taken out into a collection, and the collection is eventually converted to a group.


Int[] Nums = {1, 2, 3, 4, 5, 6, 7, 8, 9};
list<int> listodd = new list<int> ();
for (int i = 0; i < Nums. Length; i++)
//{
If (Nums[i]% 2! = 0)
//    {
Listodd.add (Nums[i]);
//    }
//}
Then convert the collection to the appropriate array
int[] Newnums = Listodd.toarray ();
for (int i = 0; i < newnums.length; i++)
//{
Console.WriteLine (Newnums[i]);
//}
Console.readkey ();


Exercise 2: Remove the maximum number from the list<int> of an integer (find the maximum).
list<int> list = new List<int> () {1, 2, 3, 4, 5, 6, 3, 8, 7, 9};
int temp=list[0];
for (int i = 0; i < list. Count; i++)
//{
if (list[i]>temp)
//    {
Temp=list[i];
//    }
//}
Console.WriteLine ("The maximum value in this array is {0}", temp);
Console.readkey ();
Exercise: Convert 123 to: one and three. Dictionary<char,char>
"1.,122,334,455,667,79e,+17"


Console.WriteLine ("Please enter a number");
String number = Console.ReadLine ();
String str = "1 One 2 II 3 three 4 restaurant 5 Wu Lu 6 seven 7 BA 8 JIU 9";
Dictionary<int, char> dic = new Dictionary<int, char> ();
string[] parts = str. Split (new char[] {", StringSplitOptions.RemoveEmptyEntries.ToString ()});
for (int i = 0; i < parts. Length; i++)
////{
Dic. ADD (Parts[i][0], parts[i][1]);
////}
StringBuilder sb = new StringBuilder ();
for (int i = 0; i < number. Length; i++)
////{
Sb. Append (Dic[number[i]);
////}
Console.WriteLine (sb.) ToString ());
Console.readkey ();
Exercise: Calculate the number of occurrences of each character in a string (interview questions). "Welcome to Chinaworld", Case insensitive, print "W2" "E 2" "O 3" ...
Tip: There are many static methods for Dictionary<char,int>,char. Char. Isletter ()
String str = "Welcome to china! This was a beautiful county, I think you'll like it. Here is the great Wall ";
str = str. ToLower ();
Dictionary<char, int> dict = new Dictionary<char, int> ();
for (int i = 0; i < str. Length; i++)
//{
if (char. Isletter (Str[i]))
//    {
if (Dict. ContainsKey (Str[i]))
//        {
dict[str[i]]++;
//        }
Else
//        {
Dict. ADD (Str[i], 1);
//        }
//    }


//}
foreach (Keyvaluepair<char, int> kv in dict)
//{
Console.WriteLine ("Character {0}," {1} "appears, KV. Key, KV. Value);
//}
Console.readkey ();
Case: Two (List) set {"A", "B", "C", "D", "E"} and {"D", "E", "F", "G", "H"}, merge the two sets to remove duplicates into one.


List<char> List1 = new list<char> () {' A ', ' B ', ' C ', ' d ', ' e '};
list<char> list2 = new list<char> () {' d ', ' e ', ' f ', ' g ', ' H '};
for (int i = 0; i < List2. Count; i++)
{
If collection 2 is not included in collection 1
if (!list1. Contains (List2[i]))
{
Then add a collection of 2 items to the collection 1
List1. ADD (List2[i]);
}
}
Show the collection 1
for (int i = 0; i < List1. Count; i++)
{
Console.WriteLine (List1[i]);
}
Console.readkey ();

Collection of Exercises

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.