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

简单有趣#38:家猫【难度:1级】:

答案1:

namespace myjinxin
{
    using System.Linq;
    
    public class Kata
    {
        public int[] HouseOfCats(int legs){
          int start = (legs&2)>>1;
          return Enumerable.Range(0, (legs>>2)+1).Select(n => (n<<1) + start).ToArray();
        }
    }
}

答案2:

using System.Linq;

namespace myjinxin
{
    public class Kata
    {
        public int[] HouseOfCats(int legs) => Enumerable.Range(0, legs / 4 + 1).Select(i => 2 * i + legs % 4 / 2).ToArray();
    }
}

答案3:

namespace myjinxin {
    using System.Collections.Generic;

    public class Kata {
        public int[] HouseOfCats( int legs ) {
            var catLegs = legs - ( legs/2 )*2;
            var people = legs/2;
            var result = new Stack<int>( );
            result.Push( people );
            while ( people > 0 ) {
                people -= 1;
                catLegs += 2;
                if ( catLegs >= 4 ) {
                    catLegs -= 4;
                    result.Push( people );
                }
            }
            return result.ToArray( );
        }
    }
}

答案4:

namespace myjinxin
{
    using System;
    using System.Linq;
    using System.Collections.Generic;
    
    public class Kata
    {
        public int[] HouseOfCats(int legs){
            List<int> list = new List<int>();
            for (int i = 0; i <= legs / 2; i++)
            {
                if ((legs - i * 2) % 4 == 0) list.Add(i);
            }
            return list.ToArray();
          
          
        }
    }
}

答案5:

namespace myjinxin
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public class Kata
    {
        public int[] HouseOfCats(int legs)
        {
          var people = new List<int>();
          for(var i=0; (i*4)<=legs; i++)
          {
            people.Add((legs - i*4)/2);
          }
          return people.OrderBy(p => p).ToArray();
        }
    }
}

答案6:

namespace myjinxin
{
    using System;
    
    public class Kata
    {
        public int[] HouseOfCats(int legs){
          //coding and coding..
          int [] ret = new int[legs/4 +1];
          int index = 0;
          for(int i=legs; i>0; i-=4){
            // Example: legs = 8 implies
            // i = 8->4->0
            // index = 0->1->2
            ret[index] = i/2;
            index++;
          }
          
          Array.Reverse(ret);
          return ret;
        }
    }
}

答案7:

namespace myjinxin
{
    using System;
    using System.Collections.Generic;
    
    public class Kata
    {
        public int[] HouseOfCats(int legs){
            List<int> result = new List<int>();
            int minPeople = (legs % 4) / 2;
            int maxPeople = legs / 2;
            
            for (int people = minPeople; people <= maxPeople; people += 2)
            {
                result.Add(people);
            }

            return result.ToArray();
          
          
        }
    }
}

答案8:

using System.Collections.Generic;

namespace myjinxin
{
    using System;
    
    public class Kata
    {
        public int[] HouseOfCats(int legs)
        {
          int num = 0;
          List<int> product = new List<int>();
          
          while(num <= legs)
          {
            if(num % 2 == 0 &amp;&amp; (legs - num) % 4 == 0)
              product.Add(num / 2);
            num += 2;
          }
          return product.ToArray();
        }
    }
}

答案9:

namespace myjinxin
{
    using System;
     using System.Linq;   
     
    public class Kata
    {
        public int[] HouseOfCats(int legs)
        {
            var numbers = Enumerable.Range(0, legs / 2 +1);
            var query = from number in Enumerable.Range(0, legs / 2 +1)
                where ((legs - number * 2) %4 ==0)
                select number;
            return query.ToArray();
        }
    }
}

答案10:

namespace myjinxin
{
    using System;
    using System.Linq;
    
    public class Kata
    {
        public int[] HouseOfCats(int legs){
          //coding and coding..
          
          var max = legs / 2;
      return Enumerable.Range(0, max+1).Where(n => n % 2 == max % 2).ToArray();
        }
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值