C # common algorithm questions (interview preparation)

Source: Internet
Author: User

1. Write the bubble, select, and insert the sorting algorithm.

// Bubble sort
Public class bubblesorter
{

Public void sort (int [] list)
{
Int length = list. Length;
For (int I = 0; I <length; I ++)
{

For (int j = length-1; j> I; j --)
{
If (list [j-1]> list [j])
{
Int temp;
Temp = list [j];
List [j] = list [j-1];
List [j-1] = temp;
}
}
}
}

}
// Select sorting
Public class selectionsorter
{
Private int min;
Public void sort (int [] list)
{
For (int I = 0; I <list. Length-1; I ++)
{
Min = I;
For (int j = I + 1; j <list. Length; j ++)
{
If (list [j] <list [min])
Min = j;
}
Int t = list [min];
List [min] = list [I];
List [I] = t;
}
}
}
// Insert sorting
Public class insertionsorter
{
Public void sort (int [] list)
{
For (int I = 1; I <list. Length; I ++)
{
Int t = list [I];
Int j = I;
While (j> 0) & (list [j-1]> t ))
{
List [j] = list [j-1];
-- J;
}
List [j] = t;
}
}
}
2. The number of columns is 30th, 5.

Public class MainClass
{
Public static void Main ()
{
Console. WriteLine (Foo (30 ));
}
Public static int Foo (int I)
{
If (I <= 0)
Return 0;
Else if (I> 0 & I <= 2)
Return 1;
Else return Foo (I-1) + Foo (I-2 );
}
}

3. Program Design: when the cat shouted, all the mice started to escape and the host was awakened.

Public delegate void SubEventHandler ();
Public abstract class Subject
{
Public event SubEventHandler SubEvent;
Protected void FireAway ()
{
If (this. SubEvent! = Null)
This. SubEvent ();
}
}
Public class Cat: Subject
{
Public void Cry ()
{
Console. WriteLine (cat cryed .);
This. FireAway ();
}
}
Public abstract class Observer
{
Public Observer (Subject sub)
{
Sub. SubEvent + = new SubEventHandler (Response );
}
Public abstract void Response ();
}
Public class Mouse: Observer
{
Private string name;
Public Mouse (string name, Subject sub): base (sub)
{
This. name = name;
}
Public override void Response ()
{
Console. WriteLine (name + attempt to escape !);
}
}
Public class Master: Observer
{
Public Master (Subject sub): base (sub ){}
Public override void Response ()
{
Console. WriteLine (host waken );
}
}
Class Class1
{
Static void Main (string [] args)
{
Cat cat = new Cat ();
Mouse mouse1 = new Mouse (mouse1, cat );
Mouse mouse2 = new Mouse (mouse2, cat );
Master master = new Master (cat );
Cat. Cry ();
}

}
4. There is a string "I am a good man". Design a function and return "man good a am I ".

5. Five students A, B, C, D, and E may participate in computer competitions. Which of the following conditions can be determined?
Participants:

(1) When A participates, B also participates;

(2) B and C are attended by only one person;

(3) Both C and D participate, or none;

(4) At least one of D and E participates;

(5) If E participates, both A and D participate.

Static void Main (string [] args)
{

Char [] name =... {'A', 'B', 'C', 'D', 'E '};
Int [] value = new int [5];
For (value [0] = 0; value [0] <2; value [0] ++)
For (value [1] = 0; value [1] <2; value [1] ++)
For (value [2] = 0; value [2] <2; value [2] ++)
For (value [3] = 0; value [3] <2; value [3] ++)
For (value [4] = 0; value [4] <2; value [4] ++)
{
If (value [1]> = value [0]) & (value [1] + value [2] = 1) & (value [2] = value [3]) & (value [3] + value [4] = 1) & (value [4] = 0 | value [4] = 1 & value [0] = 1 & value [3] = 1 ))
{
For (int I = 0; I <5; I ++)
{
If (value [I] = 1)
{
Console. WriteLine ("{0} participate", name [I]);
}
Else
{
Console. WriteLine ("{0} do not participate", name [I]);
}
}
}
}
}
6. Question:
A user entered an integer value into a text box. Without using a buit-in library, convert the numeric string to its integer representation.

Static int StringTolnt (string s)
{
Int sum = 0;
For (int I = 0; I <s. Length; I ++)
Sum = sum * 10 + (s [I]-'0 ');
Return sum;
}

 

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.