C # Recursive Algorithms

Source: Internet
Author: User

The first question I encountered was: Calculate the Array {, 5, 8 .......} the 30th-bit value does not require recursion. I wrote the following code:

 

Static void Main (string [] args)
...{

Int [] num = new int [30];
Num [0] = 1;
Num [1] = 1;
Int first = num [0];
Int second = num [1];
For (int I = 2; I <num. Length; I ++)
...{
Num [I] = first + second;
First = second;
Second = num [I];
}
Console. WriteLine (num [29]);
Console. ReadLine ();

}

It is very cumbersome to write, so it is easy to understand and understand. The following code is used:

 

Static void Main (string [] args)
...{

Console. WriteLine (Process1 (30 ));
Console. ReadLine ();
}
Public static int Process1 (int I)
...{

 

// Calculate the {30th, 5, 8 ......} bits of the array.
If (I = 0) return 0;
If (I = 1) return 1;
Else
Return Process1 (I-1) + Process1 (I-2 );
}
I have done some exercises:

1. Calculate the value of 1 + 2 + 3 + 4 +... + 100

 

Static void Main (string [] args)
...{
Console. WriteLine (Process2 (100 ));
Console. ReadLine ();
}
Public static int Process2 (int I)
...{
// Calculates the value of 1 + 2 + 3 + 4 +... + 100
If (I = 0) return 0;
Return Process2 (I-1) + I;

}
2. Calculate the value of 1-2 + 3 +-4 + 5-6 + 7-8 + 9

 

Static void Main (string [] args)
...{

Console. WriteLine (Process3 (9)-Process3 (8 ));
Console. ReadLine ();
}

Public static int Process3 (int I)
...{
// Calculate the value of 1-2 + 3 +-4 + 5-6 + 7-8 + 9
If (I = 0) return 1;
If (I = 1) return 2;
Else return Process3 (I-2) + I;
}
3. Tower of Hanoi Problems

 

Static void Main (string [] args)
...{
Hanoi (5, 'A', 'B', 'C ');
Console. ReadLine ();
}
Public static void Hanoi (int n, char A, char B, char C)
...{
// Tower of Hanoi Problems
// Move n plates from seat a with seat B to seat C
If (n = 1) Move (A, C );
Else
...{
Hanoi (n-1, A, C, B );
Move (A, C );
Hanoi (n-1, B, A, C );
}

}
Public static void Move (char startPlace, char endPlace)
...{
Console. WriteLine ("Move {0} To {1}", startPlace, endPlace );
}
4. Use recursion to convert an integer n into a string. For example, if you input 483, the number of digits of the output string "483". n is uncertain. It can be an integer of any digits.

 

Static void Main (string [] args)
...{
IntToString (483 ,"");
Console. ReadLine ();
}
Public static void IntToString (int input, String output)
...{
// Use recursion to convert an integer n into a string. For example, if you input 483, the number of digits of the output string "483". n is uncertain. It can be an integer of any digits.
// String output = "";
Output = input % 10 + output;
If (input/10! = 0)
...{
IntToString (input/10, output );
}
Else Console. WriteLine (output );
}

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/inkstone2006/archive/2008/01/22/2057853.aspx

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.