C # -- 2nd week experiment -- Task 11 -- compile a console application -- The rule of 1 columns is as follows:, 34...

Source: Internet
Author: User

/* (Start of program header annotation)
* Copyright and version Declaration of the program
* Copyright (c) 2011, a student from the computer College of Yantai University
* All rights reserved.
* File name: the rule for the number of columns is as follows: 30th, 34? (Implemented using recursive algorithms)
* Author: Lei hengxin
* Completion date: January 1, September 09, 2012
* Version No.: V1.0
* Description of tasks and Solutions
* Input description:
* Problem description:
* Program output:
* End the comment in the program Header
*/
 
[Csharp]
<Span style = "font-size: 24px;"> using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
 
Namespace ConsoleApplication_do_while
{
Class Program
{
Static void Main (string [] args)
{
Console. WriteLine ("this is a" Number of Columns with the following rules: 30th, 34... What is the-digit number ?" Program ");
Console. Write ("which number of digits do you want to calculate? ");
Int number = int. Parse (Console. ReadLine ());
Int m = f (number); // Recursive Function
Console. WriteLine ("{0} digits: {1}", number, m );
Console. ReadKey ();
}
 
Static int f (int number) // recursion is to call itself in a process or function.
{
Int fact;
If (number = 0 | number = 1) // when using a recursive policy, there must be a clear recursive termination condition, called the recursive exit.
{
If (number = 0) // This occurs when number is equal to 2.
{
Fact = 0;
}
Else
{
Fact = 1;
}
}
Else
{
Fact = f (number-1) + f (number-2 );
}
 
Return fact;
}
}
}


 
</Span>

Running result:
 

 

 

 
 
Experience Accumulation:
1.
Recursion is an important method in computer science.
Algorithms that can use recursive descriptions usually have the following features: To solve the N-scale problem, we try to break it down into smaller-scale problems, then, we can easily construct solutions to big problems from the solutions to these small problems, and these smaller problems can also be decomposed into smaller problems using the same decomposition and synthesis methods, then, we can deconstruct these smaller problems to solve large-scale problems. In particular, when the scale N is 1, it can be directly solved.
 

Related Article

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.