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

简单有趣#113:越聚越多【难度:1级】:

答案1:

namespace myjinxin
{
    using System;
    
    public class Kata
    {
        public bool NumberIncreasing(int n){
          
         if(n-59049>=0 &&((n-59049))%5==0)
          {
            return true;
          }
          else if(n-19683>0 &&((n-19683))%5==0)
          {
            return true;
          }
          else if(n-6561>0&&((n-6561))%5==0)
          {
            return true;
          }
          else if(n-2187>0&&((n-2187))%5==0)
          {
            return true;
          }
           else if(n-729>0&&((n-729))%5==0)
          {
            return true;
          }
          else if(n-243>0&&((n-243))%5==0)
          {
            return true;
          }
          else if(n-81>0&&((n-81))%5==0)
          {
            return true;
          }
          else if(n-27>0&&((n-27))%5==0)
          {
            return true;
          }
          else if(n-9>0&&((n-9))%5==0)
          {
            return true;
          }
          else if ((n-3)%5==0){
            return true;
          }
          
          else if ((n-1)%5==0){
            return true;
          }
          
          
          
          
          
         
          
          
          
          
          return false;
        }
        
    }
}

答案2:

namespace myjinxin
{
    using System;
    public class Kata
    {
        public bool NumberIncreasing(int n)
        {
            if (n % 5 == 0) return false;
            if (n > 22) return true;
            return !(n % 5 == 2 || n == 4);
        }
    }
}

答案3:

namespace myjinxin
{
    using System;
    public class Kata
    {
        public bool NumberIncreasing(int n)
        {
            if (n % 5 == 0) return false;
            if (n > 22) return true;
            return (n % 5 == 2 || n == 4) ? false : true;
        }
    }
}

答案4:

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

namespace myjinxin
{
    using System;
    
    public class Kata
    {
        public bool NumberIncreasing(int n){
           HashSet<int> A = new HashSet<int>(){2, 4, 7, 12, 17, 22};
           return (!(A.Contains(n)) &amp;&amp; (n % 5 != 0));
        }
    }
}

答案5:

namespace myjinxin
{
    using System;   
    public class Kata
    {
        public bool NumberIncreasing(int n)
        {
            if (n == 1 || n == 3) return true;
            if (n - 5 <= 0) return false;
            if ((n - 1) % 5 == 0) return true;
            int res = 1;
            while(res < n){res *= 3;if((n - res) % 5 == 0)return true;}
            if (res == n || res - 5 == n) return true;
            return false;
        }
    }
}

答案6:

namespace myjinxin
{
    using System;
    
    public class Kata
    {
        public bool NumberIncreasing(int n){

           while (n > 1)
            {
                if (n % 5 == 1) return true;
                if (n % 3 == 0) n /= 3; else n -= 5;
            }
            return (n == 1) ? true : false;
        }
    }
}

答案7:

namespace myjinxin
{
    using System;
    using System.Linq;
    public class Kata
    {
        public bool NumberIncreasing(int n) => n % 5 != 0 &amp;&amp; !new[] {2, 4, 7, 12, 17, 22}.Contains(n);
    }
}

答案8:

namespace myjinxin
{
    using System;
    
    public class Kata
    {
        public bool NumberIncreasing(int n){
            if(PowOf3(n)) return true;
            while(n-5>=1){
                n-=5;
                if(PowOf3(n)) return true;
            }
            return n==1;
        }
        bool PowOf3(int n)=>(double)Math.Log(n,2)/Math.Log(3,2)%1==0;
    }
}

答案9:

using System;
using System.Linq;
namespace myjinxin
{   
    public class Kata
    {
        public bool NumberIncreasing(int n)
        {
          bool result = false;
          if(n > 0)
          {
            if (n == 1)
              result = true;
            else if (((Math.Log(n) / Math.Log(3)) % 1 == 0) || ((n - 1) % 5 == 0))
              result = true;
            else if(n.ToString().Sum(c => c - '0') % 3 == 0)
              result = NumberIncreasing(n/3);
            else
              result = NumberIncreasing(n-5);
          }
          return result;          
        }
    }
}

答案10:

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

    public class Kata
    {
        private List<int> increased = new List<int> { 0, 1 };

        public bool NumberIncreasing(int n)
        {
            while (increased.Count <= n)
            {
                increased.Add(-1);
            }
            if (increased[n] == -1)
            {
                increased[n] = n % 3 == 0 &amp;&amp; NumberIncreasing(n / 3) || n > 5 &amp;&amp; NumberIncreasing(n - 5) ? 1 : 0;
            }
            return increased[n] != 0;
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值