面试会遇到的几个小程序
面试会遇到的几个小程序:
1.有数列如下:1,1,2,3,5,8,13,。。。,请第N位?(请用两种方法得到第N位是多少,并可查询任意一位是多少)
//斐波那契数 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) { //方法一: List<long> resultArry = GetNum(10); foreach (long result in resultArry) { Console.Write(result+","); } //方法二 //ReturnNum(10); //resultArry.Insert(0, 1); //foreach (long result in resultArry) // { // Console.Write(result+","); //} Console.Read(); }
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · 数据库 面试记录(面试题)
- · JavaScript 开发者都应该知道的十个概念
- · 程序员面试注意事项
- · Emby全平台开心版、认证服务器搭建方法(自建认证服务器)
- · 企业微信开发之代开发应用
- · vue3 html2canvas导出透明png图片
- · The instance of entity type ‘Customer’ cannot be tracked because another instance with the same key value for {‘Id’} is already being tracked.
- · .NET8实时更新nginx ip地址归属地
- · 成都共享办公室推荐
- · 解决.NET Blazor子组件不刷新问题
- · .NET8如何在普通类库中引用 Microsoft.AspNetCore
- · 群晖web station设置wordpress 伪静态