C#练习题答案: 整数深度【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战
作者:互联网
整数深度【难度: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;
}
}
}
标签:练习题,digits,景越,C#,System,int,var,using,public 来源: https://blog.csdn.net/weixin_45444821/article/details/100750780