C#练习题答案: 简单有趣#399:请升序序列【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

简单有趣#399:请升序序列【难度:1级】:

答案1:

public class Kata
{
  public static int MakeSequences(int n)
  {
      var arr = new int[n/2 + 1];
      arr[0] = 1; 

      return msHelper(n, ref arr);
  }
  
  private static int msHelper(int n, ref int[] arr)
  {
      if (arr[n/2] == 0)
      {
          arr[n/2] = msHelper(n - 2, ref arr) + msHelper(n / 2, ref arr);
      }
      
      return arr[n/2];
  }
}

答案2:

using System.Collections.Generic;
public class Kata
{
  private static List<int> cache = new List<int>() { 1, 1 };
  public static int MakeSequences(int n)
  {
    for(int i = cache.Count, j = i - 1; j < n; ++i, ++j)
    {
      cache.Add(cache[j] + ((j &amp; 1) * cache[i>>1]));
    }
    return cache[n];
  }
}

答案3:

using System.Collections.Generic; 
public class Kata
{
  private static List<int> cache = new List<int>{ 1 };
  public static int MakeSequences(int n)
  {
      if (cache.Count <= n/2)
          cache.Add( MakeSequences(n - 2) + MakeSequences(n / 2) );
      return cache[n/2];
  }
}

答案4:

using System.Collections.Generic;
using System.Linq;

public class Kata
{
  private static List<int> cache = new List<int>() { 1, 1 };
  public static int MakeSequences(int n)
  {
    for(int i = cache.Count, j = i - 1; j < n; ++i, ++j)
    {
      cache.Add(cache[j] + ((j &amp; 1) * cache[i>>1]));
    }
    return cache[n];
  }
}

答案5:

using System;

public class Kata
{
  public static int MakeSequences(int n)
  {
    var values = new int[n];
    values[0] = 1;
    for (var i = 1; i < n; i++) values[i] = values[i - 1] + (i % 2 == 0 ? 0 : values[(i + 1) / 2 - 1]);
    return values[n - 1];
  }
}

答案6:

using System.Collections.Generic;
using System.Linq;
public class Kata
    {
        public static List<int> zestaw = new List<int> { 1, 1, 2, 2, 4, 4, 6, 6, 10, 10, 14, 14, 20, 20, };
        public static int MakeSequences(int n)
        {
            int temp = zestaw.Count;
            while(temp <= n)
            {
                int check = zestaw.Take(temp / 2 + 1).Sum();
                zestaw.Add(check);
                zestaw.Add(check);
                temp += 2;
            }
            return zestaw[n];
        }
    }

答案7:

public class Kata
{
        private static int[] seqs = new int[1100];
        static Kata()
        {
            seqs[0] = 1;
            seqs[1] = 1;
            for (int i = 1; i < 550; i++)
            {
                seqs[2 * i] = seqs[2 * i - 1] + seqs[i];
                seqs[2 * i + 1] = seqs[2 * i];
            }
        }
        public static int MakeSequences(int n) => seqs[n];
}

答案8:

using System.Collections.Generic;
public class Kata
{
  public static int MakeSequences(int n)
  {
    var dp=new List<int>(){1,1};
    for(int i=2;i<=n;i++){
      if(i%2==0) dp.Add(dp[i-1]+dp[i/2]);
      else dp.Add(dp[i-1]);
    }
    return dp[n];
  }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值