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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_45444821/article/details/102737403

简单有趣#136:缺少值【难度:1级】:

答案1:

using System.Linq;

public class Kata
{
    public int MissingValues(int[] arr)
    {
      var grouped = arr.GroupBy(i => i);
      var x = grouped.FirstOrDefault(g => g.Count() == 1).Key;      
      var y = grouped.FirstOrDefault(g => g.Count() == 2).Key;
      
      return x *x* y;      
    }
}

答案2:

namespace myjinxin
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public class Kata
    {
        public int MissingValues(int[] arr)
        {
          var unique = arr.Where(x=>arr.Count(y=>y==x)==1).ToList();
           var twice = arr.Where(x=>arr.Count(y=>y==x)==2).ToList();
           return unique.First() * unique.First() * twice.First();         
        }
    }
}

答案3:

namespace myjinxin
{
    using System;
    using System.Linq;
    public class Kata
    {
        public int MissingValues(int[] arr){
          var query = arr.GroupBy(r => r)
                .Select(grp => new
                {
                    Value = grp.Key,
                    Count = grp.Count()
                });
          var x = query.First(item => item.Count == 1).Value;
          var y = query.First(item => item.Count == 2).Value;
          return x*x*y;
        }
    }
}

答案4:

namespace myjinxin
{
    using System;
    using System.Linq;
    
    public class Kata
    {
        public int MissingValues(int[] arr){
            int res = 1;

            var gr = arr.GroupBy(x => x);
            foreach(var g in gr)
            {
                if(g.Count() == 2)
                {
                    res *= g.Key;
                }
                else if(g.Count() == 1)
                {
                    res *= (int)Math.Pow(g.Key, 2);
                }
            }
            
            return res;
        }
    }
}

答案5:

namespace myjinxin {
    using System.Linq;

    public class Kata {
        public int MissingValues( int[] arr ) {
            var groups = arr.GroupBy( i => i ).ToArray( );
            var x = groups.First( g => g.Count( ) == 1 ).Key;
            var y = groups.First( g => g.Count( ) == 2 ).Key;
            return x*x*y;
        }
    }
}

答案6:

namespace myjinxin
{
    using System;
    using System.Linq;
    using System.Collections.Generic;
    
    public class Kata
    {
        public int MissingValues(int[] arr){
          
          Dictionary<int, int> counts = arr.GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count());
          
          int result = 1;
          
          foreach (KeyValuePair<int, int> pair in counts)
          {
            if (pair.Value == 2)
              result *= pair.Key;
            if (pair.Value == 1)
              result *= (int)Math.Pow(pair.Key, 2);
          }
          
          return result;
          
        }
    }
}

答案7:

namespace myjinxin
{
    using System.Linq;
    
    public class Kata
    {
        public int MissingValues(int[] arr)
        {
            var res = arr.OrderBy(x => arr.Count(el => el == x)).ToArray();
            return res[0]*res[0]*res[1];
        }
    }
}

答案8:

using System.Linq;
namespace myjinxin
{
    using System;
    
    public class Kata
    {
        public int MissingValues(int[] arr)
        {
            int x = 0;
            int y = 0;
            foreach(int i in arr)
            {
                if(((from j in arr where j.Equals(i) select j).Count()) == 1)
                    x += i;
                if(((from m in arr where m.Equals(i) select m).Count()) == 2)
                    y = i;
            }
            return (x*x)*y;
        }
    }
}

答案9:

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

namespace myjinxin
{
    public class Kata
    {
        public int MissingValues(int[] arr)
        {
            var dic = new Dictionary<int, int>();
            foreach (var number in arr)
            {
                if (dic.ContainsKey(number))
                {
                    dic[number]++;
                }
                else
                {
                    dic.Add(number, 1);
                }
            }
            var appearsTwiceNumber = dic.First(x => x.Value == 2).Key;
            var appearsOnceNumber = dic.First(x => x.Value == 1).Key;

            return appearsTwiceNumber *appearsOnceNumber* appearsOnceNumber;
        }
    }
}

答案10:

namespace myjinxin
{
    using System.Linq;
    
    public class Kata
    {
        public int MissingValues(int[] arr){
          var q = arr.GroupBy(n=>n).OrderBy(x=>x.Count()).Select(x => x.Key).ToList();
          return q[0]*q[0]*q[1];
        }
    }
}



猜你喜欢

转载自blog.csdn.net/weixin_45444821/article/details/102737403