Several small programs for the interview

Source: Internet
Author: User

There are several small programs for the interview:

1. There is a series of numbers:,..., please nth? (Use two methods to obtain the nth bit and query the number of any bit)

// Fibonacci count
Private static List <long> GetNum (long n)
{
Long a = 1, B = 1, num = 0;
List <long> resultArry = new List <long> ();
Try
{
For (int I = 0; I <n; I ++)
{
Num = a + B;
A = B;
B = num;
ResultArry. Add (num );
}
}
Catch (Exception ex)
{
Throw new Exception (ex. Message );
}
Return resultArry;
}

  

static long i = 0, j = 1, result = 0;
static List<long> resultArry = new List<long>();
private static long ReturnNum(long n)
{
if (n <= 2)
{
result = i + j;
}
else
{
result = ReturnNum(n - 1) + ReturnNum(n - 2);
}

if (!resultArry.Contains(result))
{
resultArry.Add(result);
}
return result;
}

  

Static void Main (string [] args)
{
// Method 1:
List <long> resultArry = GetNum (10 );
Foreach (long result in resultArry)
{
Console. Write (result + ",");
}

// Method 2
// ReturnNum (10 );
// ResultArry. Insert (0, 1 );
// Foreach (long result in resultArry)
//{
// Console. Write (result + ",");
//}

Console. Read ();
}

  

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.