C#练习题答案: 思维与测试:不完美?抛弃!【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

思维与测试:不完美?抛弃!【难度:2级】:

答案1:

namespace myjinxin
{
    using System;    
    public class Kata
    {
        public string Testit(string s){
          string result = "";
          for(int i = 2; i < s.Length; i ++)
          {
            if (IsPrime(i)) result += s[i];
          }
          return result;
        }
        public bool IsPrime(int number)
        {
          if (number < 2) return false;
          if (number % 2 == 0) return number == 2;
          int boundary = (int)Math.Sqrt(number);
          for(int i = 3; i <= boundary; i += 2)
          {
            if(number % i == 0) return false;
          }
          return true;
        }
    }
}

答案2:

namespace myjinxin
{
  using System;
  using System.Linq;
  
  public class Kata
  {
    public Boolean IsPerfect(Int32 Input){
      for (var index = 3; index <= Math.Sqrt(Input); index += 2){
        if (Input % index == 0){
          return false;
        }
      }
      return Input < 3 ? Input == 2 : Input % 2 == 1;
    }
    
    public String Testit(String Input){
      return String.Join("", Input.ToCharArray().Where((Element, Index) => IsPerfect(Index)));
    }
  }
}

答案3:

namespace myjinxin
{
  using System;
  using System.Linq;
  
  public class Kata
  {
    public String Testit(String Input){
      return String.Join("", Input.ToCharArray().Where((Element, Index) => {
        for (var index = 3; index <= Math.Sqrt(Index); index += 2){
          if (Index % index == 0){
            return false;
          }
        }
        return Index < 3 ? Index == 2 : Index % 2 == 1;
      }));
    }
  }
}

答案4:

namespace myjinxin
{
    using System;
    using System.Linq;
    
    public class Kata
    {
        public string Testit(string s){
          string res = "";
          for (int i = 2; i < s.Length; i += 1) {
            if (IsPrime(i)) {
              res += s[i];
            }
          }          
          return res;
        }
         private static bool IsPrime(int n)
  {
    if (n <= 1) return false;
    if (n <= 3) return true;
    if (n % 2 == 0 || n % 3 == 0) return false;
    for(int i = 5; i * i <= n; i += 6)
      if (n % i == 0 || n % (i + 2) == 0)
        return false;
    return true;
  }
}
}

答案5:

namespace myjinxin
{
    using System;
    using System.Linq;
    public class Kata
    {
        public string Testit(string s) => string.Concat(new[] {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157}.Select(x => x < s.Length ? $"{s[x]}" : ""));
    }
}

答案6:

namespace myjinxin
{
    using System;
    using System.Linq;
    using System.Collections;
    using System.Collections.Generic;

    public class Kata
    {
        public static IEnumerable<int> PrimesBelow(int n)
        {
            if (n > 2) yield return 2;
            if (n > 3) yield return 3;
            if (n <= 5) yield break;
            int n6 = n % 6;
            int c = n6 > 1 ? 1 : 0;
            n = n6 == 1 ? n - 1 : n + (6 - n6) % 6;
            BitArray s = new BitArray(n / 3, true) { [0] = false };
            int r = ((int)Math.Sqrt(n)) / 3 + 1;
            for (int i = 0; i < r; i++)
            {
                if (s[i])
                {
                    int k = (3 * i + 1) | 1;
                    for (int j = k * k / 3; j < n / 3; j += 2 * k) s[j] = false;
                    for (int j = (k * k + 4 * k - 2 *k* (i % 2)) / 3; j < n / 3; j += 2 * k) s[j] = false;
                    yield return k;
                }
            }
            for (int i = r; i < n / 3 - c; i++)
            {
                if (s[i]) yield return (3 * i + 1) | 1;
            }
        }

        public string Testit(string s) => string.Concat(PrimesBelow(s.Length).Select(i => s[i]));
    }
}

答案7:

using System.Linq;

namespace myjinxin
{
    public class Kata
    {
        public string Testit(string s)
        {
            return string.Concat(s.Where(IsPrimeIndex));
        }

        public bool IsPrimeIndex(char c, int n)
        {
            return n > 1 &amp;&amp;
                Enumerable
                .Range(2, n - 2)
                .All(candidate => n%candidate > 0);
        }
    }
}

答案8:

namespace myjinxin
{
    using System;
    using System.Linq;
    
    public class Kata
    {
        public static bool IsPrime(int candidate)
        {
            if ((candidate &amp; 1) == 0)
            {
                if (candidate == 2) return true;
                else return false;
            }
            
            for (int i = 3; (i * i) <= candidate; i += 2)
            {
                if ((candidate % i) == 0) return false;
            }
            return candidate != 1;
        }
    
        public string Testit(string s)
        {
          return String.Join("",s.ToCharArray().Where((x,i) => i>1 &amp;&amp; IsPrime(i)));
        }
    }
}

答案9:

namespace myjinxin
{
    using System;
    using System.Text;
    
    public class Kata
    {
        public string Testit(string input)
        {
            StringBuilder result = new StringBuilder();
            for(int i = 0; i < input.Length; i++)
            {
                if (IsPrimeNumber(i))
                {
                    result.Append(input[i]);
                }
            }
            
            return result.ToString();
        }
        
        private static bool IsPrimeNumber(int number)
        {
            if (number <= 2)
            {
                return number == 2;
            }
            
            if (number%2 == 0)
            {
                return false;
            }
            
            int limit = (int)Math.Sqrt(number);
            for(int i = 3; i <= limit; i++)
            {
                if (number%i == 0)
                {
                    return false;
                }
            }
            
            return true;
        }
    }
}

答案10:

namespace myjinxin
{
  using System;
    
  public class Kata
  {
    public string Testit(string s)
    {
      int i = 3, j = 0, l = s.Length;
      char[] chArr = new char[l];
      
      if(l > 2) chArr[j++] = s[2];
      
      while (i < l)
      {
        int n;
        for (n = 3; n < i; n += 2)
        {
          if (i % n == 0) break;  
        }
        
        if (i == n) chArr[j++] = s[i];
        i += 2;
      }   
      
      return new string(chArr).Substring(0, j);
    }
  }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值