C#练习题答案: 简单有趣#66:获取最大数量【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

简单有趣#66:获取最大数量【难度:2级】:

答案1:

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

    public class Kata {
        public int ObtainMaxNumber( int[] arr ) {
            var dic = ToDictionary( arr );
            while ( true ) {
                var sizes = dic.Keys.ToArray( );
                var b = true;
                foreach ( var size in sizes ) {
                    if ( dic[ size ] > 1 ) {
                        var newCandy = size*2;
                        dic[ size ] -= 2;
                        if ( !dic.ContainsKey( newCandy ) ) {
                            dic[ newCandy ] = 0;
                        }
                        dic[ newCandy ] += 1;
                        b = false;
                        break;
                    }
                }
                if ( b ) {
                    break;
                }
            }
            return dic.Keys.Max( );
        }

        private Dictionary<int, int> ToDictionary( int[] arr ) {
            var dic = new Dictionary<int, int>( );
            foreach ( var i in arr ) {
                if ( !dic.ContainsKey( i ) ) {
                    dic[ i ] = 0;
                }
                dic[ i ] += 1;
            }
            return dic;
        }

    }
}

答案2:

namespace myjinxin
{
    using System;
    using System.Linq;
    public class Kata
    {
        public int ObtainMaxNumber(int[] arr)
        {
            arr = arr.OrderBy(x => x).ToArray();
            for(int i = 0; i < arr.Length - 1; i++)
            {
                if (arr[i] == arr[i + 1])
                {
                    arr[i + 1] = 2 * arr[i];
                    for (int j = i + 1; j < arr.Length - 1; j++)
                    {
                        if (arr[j] > arr[j + 1])
                        {
                            int temp = arr[j];
                            arr[j] = arr[j + 1];
                            arr[j + 1] = temp;
                        }
                        else break;
                    }
                }
            }
            return arr[arr.Length - 1];
        }
    }
}

答案3:

namespace myjinxin
{
    using System;
    using System.Linq;
    public class Kata
    {
        public int ObtainMaxNumber(int[] arr)
        {
            var list = arr.ToList();
            var max = list.Max();
            var min = list.Min();
            for (int i = min; i <=max; i++)
            {
                var begin = list.FindIndex(x => x == i);
                var end = list.FindLastIndex(x => x == i);
                if (begin >= 0 &amp;&amp; end >= 0 &amp;&amp; begin != end)
                {
                    var add = list[begin] * 2;
                    if (add > max) max = add;
                    list.RemoveAt(begin);
                    list.RemoveAt(end-1);
                    list.Add(add);
                    i--;
                }
            }
            return max;
        }
    }
}

答案4:

namespace myjinxin
{
    using System;
    using System.Linq;
    
    public class Kata
    {
        public int ObtainMaxNumber(int[] arr){
            arr = arr.GroupBy(x=>x).SelectMany(x=>Enumerable.Repeat(x.Key*2,x.Count()/2).Concat(Enumerable.Repeat(x.Key,x.Count()%2))).ToArray();
            return arr.GroupBy(x=>x).All(x=>x.Count()<2)?arr.Max():ObtainMaxNumber(arr);
        }
    }
}

答案5:

namespace myjinxin
{
    using System;
    using System.Linq;
    
    public class Kata
    {
        public int ObtainMaxNumber(int[] arr)
        {
            bool isReady = false; // not finished joining equal elements
            while (!isReady) 
            {
                isReady = true;
                Array.Sort(arr); // sort the array so that equal elements are next to each other
                for (int i = 0; i < arr.Length - 1; i++)
                {
                    if (arr[i] == 0) // skip the zeroes
                    {
                        continue;
                    }

                    if (arr[i] == arr[i + 1]) // found 2 equal elements
                    {
                        isReady = false;
                        arr[i] = 0; // remove the first
                        arr[i + 1] *= 2; // the second doubles
                    }
                }
            }
            return arr.Max();
        }
    }
}

答案6:

namespace myjinxin
{
    using System;
    
    public class Kata
    {
        public int ObtainMaxNumber(int[] arr){
          Array.Sort(arr);

          for (int i = 0; i < arr.Length-1; i++)
          {
              if (arr[i] > 0 &amp;&amp; arr[i] == arr[i + 1])
              {
                  arr[i + 1] = arr[i]*2;
                  arr[i] = 0;
                  Array.Sort(arr);
                  //i=-1;
              }
          }
          
          return arr[arr.Length-1];
        }
    }
}

答案7:

namespace myjinxin
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    public class Kata
    {
        public List<int> list;
        public int ObtainMaxNumber(int[] arr){
          list = arr.ToList();
          while (list.Count > 2)
          {
            list.Sort();
            if (list[0] == list[1])
            {
              list[1] += list[1];
            }
            list.RemoveAt(0);
          }
          if (list[0] == list[1])
          {
            return list[0] * 2;
          }
          else if (list[0] < list[1])
          {
            return list[1];
          }
          else
          {
            return list[0];
          }
        }
    }
}

答案8:

namespace myjinxin
{
    using System;
    using System.Linq;
    using System.Collections.Generic;
    
    public class Kata
    {
        public int ObtainMaxNumber(int[] arr){
           while(arr.Length > 2)
            {
                arr = arr.OrderBy(x=>x).ToArray();
                
                if(arr.Count(x=>x == arr.First())>1)
                {
                    var lst = arr.ToList();
                    var temp = arr.First();

                    lst.RemoveRange(0, 2);
                    lst.Add(temp * 2);
                    arr = lst.ToArray();
                }
                else
                {
                    arr = arr.Skip(1).ToArray();
                }
            }
            if (arr[0] == arr[1])
            {
                return arr[0] * 2;
            }
            else
            {
                return arr.Max();
            }
        }
    }
}

答案9:

namespace myjinxin
{
    using System;
    using System.Collections.Generic;
    
    public class Kata
    {
        public int ObtainMaxNumber(int[] arr){
          List<int> list = new List<int>(arr);
          list.Sort();
          bool isPacked = false;
          while (!isPacked)
          {
            isPacked = true;
            for (int i = 1; i < list.Count; i++)
            {
              if (list[i] == list[i - 1])
              {
                list[i] *= 2;
                list.RemoveAt(i - 1);
                list.Sort();
                isPacked = false;
                break;
              }
            }
          }
          return list[list.Count - 1];
        }
    }
}

答案10:

namespace myjinxin
{
    using System;
    using System.Linq;
    using System.Collections.Generic;
    public class Kata
    {
public int ObtainMaxNumber(int[] arr){
  var rec=new Dictionary<int,int>();
  foreach(int x in arr){
    int p=1,xx=x;
    while(xx%2==0) {
      xx/=2;
      p*=2;
    }
    if(!rec.ContainsKey(xx)) rec.Add(xx,p);
    else rec[xx]+=p;
  }
  var max=0;
  foreach(int x in rec.Keys){
    var t=(int)(x*Math.Pow(2,Math.Floor(Math.Log(rec[x])/Math.Log(2))));
    if(t>max) max=t;
  }
  return max;
}  

    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值