C#练习题答案: 整数深度【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

这篇博客介绍了C#编程练习中的整数深度问题,难度为二级。提供了从答案1到答案10的详细解题思路和方法,适合C#初学者和进阶者提升编程技能。
摘要由CSDN通过智能技术生成

整数深度【难度:2级】:

答案1:

using System.Collections.Generic;

namespace Solution 
{
  public class Kata 
  {
 
    public static int ComputeDepth(int n)
    {
      var intArray = new HashSet<char>(new [] { '0','1','2','3','4','5','6','7','8','9' });
      var i = 1;
      for (; intArray.Count > 0; i++) {
        foreach (var c in (i*n).ToString()) intArray.Remove(c);
      }
      return i-1;
    }
  }
}

答案2:

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

namespace Solution 
{
  public class Kata 
  { 
    public static int ComputeDepth(int n)
    {
      var hs = new HashSet<char>();
      return Enumerable.Range(1, int.MaxValue).TakeWhile(i => {
        hs.UnionWith((n * i).ToString());
        return hs.Count < 10;
      }).Count() + 1;    
    }
  }
}

答案3:

namespace Solution 
{
    public class Kata 
    {
        public static int ComputeDepth(int n)
        {
            int d = 0, i = 1;
            
            for (int v = n; d != 1023 ; v += n, i++)
            {
                int t = v;
                while (t != 0)
                {
                    d |= 1 << t % 10;
                    t /= 10;
                }
            }
            
            return i - 1;
        }
    }
}

答案4:

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

namespace Solution 
{
  public class Kata 
  {
 
    public static int ComputeDepth(int n)
    {
       var s = new HashSet<char>();
       var k = 0;
       while (s.Count != 10)
       { 
         k++;
         foreach(var c in (k*n).ToString()) s.Add(c);         
       }
       return k;
    }
  
  }
}

答案5:

using System;
using System.Linq;

namespace Solution 
{
  public class Kata 
  {
    public static int ComputeDepth(int n)
    {
      var exists = Enumerable.Repeat(false, 10).ToArray();
      var m = 0;
      
      do
      {
        m += 1;
        var digits = (n * m).ToString().ToCharArray().Select(c => int.Parse($"{c}"));
        foreach(var num in digits)
        {
          exists[num] = true;
        }
      } while(exists.Contains(false));
       
      return m;
    }
  }
}

答案6:

using System.Linq;
namespace Solution 
{
  public class Kata 
  {
        public static int ComputeDepth(int n)
        {
            var digits = new bool[10];
            var m = 1;
            while(true)
            {
                var r = n * m;
                var s = r.ToString();
                for (int i = 0; i < s.Length; i++) digits[s[i] - '0'] = true;
                if (digits.All(x => x == true)) return m;
                m++;
            }
        }
  }
}

答案7:

using System;
namespace Solution 
{
  public class Kata 
  {
 
    public static int ComputeDepth(int n)
    {
       string digits = "0123456789";
       int i = 1;
       while (digits.Length > 0)
       {
         int num = i * n;
         string numdigits = num.ToString();
         i++;
         
         foreach (var digit in numdigits)
         {
           if (digits.Contains(digit))
             digits = digits.Replace(digit.ToString(), string.Empty);
  
         }
         Console.WriteLine(digits);
       }
       
       return i-1;
    }
  
  }
}

答案8:

using System;
using System.Collections.Generic;
namespace Solution 
{
  public class Kata 
  {
 
    public static int ComputeDepth(int n)
    {
      Console.WriteLine("Start: " + n);
      int res = 0;
      int count = 1;
      string val = "";
      bool allTen = false;
      List<char> digits = new List<char>();
      
      while(!allTen) {
        res = n * count;
        val = res.ToString();
        Console.WriteLine("Mult by " + count + " = " + val);
        
        for (int i = 0; i < val.Length; i++) {
          if (!digits.Contains(val[i])) {
            digits.Add(val[i]);
            Console.WriteLine("Added: " + val[i]);
          }
          if (digits.Count == 10) { 
            allTen = true; 
          }
        }
        if (!allTen) { count++; }
      }
            
      return count;
    }
  
  }
}

答案9:

using System;
using System.Collections.Generic;

namespace Solution 
{
    public class Kata 
    {
        public static int ComputeDepth(int n)
        {
            var set = new HashSet<int>();
            var i = 0;
            while (set.Count < 10)
            {
                i++;
                var t = (n * i).ToString();
                foreach (var v in t)
                {
                    set.Add(int.Parse(v.ToString()));
                }
            }
            return i;
        }
    }
}

答案10:

using System.Collections.Generic;

namespace Solution 
{
  public class Kata 
  {
 
    public static int ComputeDepth(int n)
    {
    var depth = 0;
    HashSet<char> foundDigits = new HashSet<char>();
    while(foundDigits.Count < 10){
    depth++;
    var result = n * depth;
    foreach(var c in result.ToString().ToCharArray())    {foundDigits.Add(c);}    
    }
    
    return depth;
    }
  
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值