C# 打印所有唯一的数字
问题:打印所有只有唯一数字的号码。 输入:n=15 产出:1234567891012141415 这里11不包括在内,因为它有两次1,同样的方法是123456。。也有效,但121 1344无效,因为同一数字不止一次 我从1-n开始运行循环并检查每个数字。 我使用哈希映射来确定数字的唯一性C# 打印所有唯一的数字,c#,algorithm,data-structures,C#,Algorithm,Data Structures,问题:打印所有只有唯一数字的号码。 输入:n=15 产出:1234567891012141415 这里11不包括在内,因为它有两次1,同样的方法是123456。。也有效,但121 1344无效,因为同一数字不止一次 我从1-n开始运行循环并检查每个数字。 我使用哈希映射来确定数字的唯一性 有没有更好的办法解决上述问题。这就是我如何消除重复字符的数字 Console.Write("Input:"); int number = int.Parse(Console.ReadLine()
有没有更好的办法解决上述问题。这就是我如何消除重复字符的数字
Console.Write("Input:");
int number = int.Parse(Console.ReadLine());
List<int> numbers = new List<int>();
List<int> acceptedNumbers = new List<int>();
for (int i = 1; i <= number; i++)
{
numbers.Add(i);
}
foreach (var num in numbers)
{
bool rejected = false;
char[] numChars = num.ToString().ToCharArray();
foreach (var numChar in numChars)
{
if (numChars.Where(n => n == numChar).Count() > 1)
{
rejected = true;
}
}
if (!rejected)
{
acceptedNumbers.Add(num);
}
}
acceptedNumbers.ForEach(n => Console.Write($"{n} "));
Console.Read();
Console.Write(“输入:”);
int number=int.Parse(Console.ReadLine());
列表编号=新列表();
List AcceptedNumber=新列表();
对于(inti=1;inn==numChar.Count()>1)
{
拒绝=正确;
}
}
如果(!拒绝)
{
acceptedNumbers.Add(num);
}
}
ForEach(n=>Console.Write($“{n}”);
Console.Read();
您可以使用LINQ,将数字转换为字符串,并检查字符串的长度是否等于不同字符的数量
for (int i = 1; i < n; i++){
if (i.ToString().Length == i.ToString().Distinct().Count())
Console.Out.Write(i + " ");
}
for(int i=1;i
我的想法:
没有字符串操作发生,所以它应该会带来效率,我不确定,但类似的东西
List<int> numbers = new List<int>(){};
numbers =numbers.Where(p=>validCheck(p)==true).ToList();
static bool validCheck(int n)
{
return (n.ToString().Length==n.ToString().Disctinct().Count());
}
列表编号=新列表(){};
数字=数字。其中(p=>validCheck(p)==true);
静态布尔有效检查(整数n)
{
返回值(n.ToString().Length==n.ToString().Disctinct().Count());
}
字符串是IEnumerable-因此您可以使用LINQ语句来解决您的问题:
Numbers.Where(N => N.ToString().Distinct().Count() == N.ToString().Length);
查询正在检查数字字符串中有多少个字符是不同的,并将此数字与总字符数进行比较
Console.Write("Input:");
int number = int.Parse(Console.ReadLine());
List<int> numbers = new List<int>();
List<int> acceptedNumbers = new List<int>();
for (int i = 1; i <= number; i++)
{
numbers.Add(i);
}
foreach (var num in numbers)
{
bool rejected = false;
char[] numChars = num.ToString().ToCharArray();
foreach (var numChar in numChars)
{
if (numChars.Where(n => n == numChar).Count() > 1)
{
rejected = true;
}
}
if (!rejected)
{
acceptedNumbers.Add(num);
}
}
acceptedNumbers.ForEach(n => Console.Write($"{n} "));
Console.Read();
以下是整个代码,打印出所有不同的数字,直到20:
List<int> Numbers = new List<int>();
for (int i = 1; i <= 20; i++)
{
Numbers.Add(i);
}
IEnumerable<int> AcceptedNumbers = Numbers.Where(N => N.ToString().Distinct().Count() == N.ToString().Length);
foreach (int AcceptedNumber in AcceptedNumbers)
{
Console.WriteLine(AcceptedNumber);
}
列表编号=新列表();
for(int i=1;i N.ToString().Distinct().Count()==N.ToString().Length);
foreach(AcceptedNumber中的int AcceptedNumber)
{
Console.WriteLine(接受号码);
}
另一种解决方案是使用整数除法和模(无数字到字符串的转换)。您可以使用以下方法验证数字的唯一性(假设digits
是int
数组有10个元素)
工作示例作为一个半有用的库函数,您可以在其中以开始和所需数量为种子
public static IEnumerable<int> UniqueDigits(int start, int count)
{
for (var i = start; i < (start + count); i++)
{
var s = i.ToString();
if (s.Distinct().Count() == s.Length)
{
yield return i;
}
}
}
或
只有9*9!/(10-n)!具有n
位的唯一数字。对于较大的n
,您可能需要下一个词典算法来避免不必要的迭代。(例如,只有544320个7位数字,但您的程序需要迭代近1000万个数字才能生成它们!)
下面是我对一组n唯一数字
数字(其中n>1
)的下一个词典编纂过程的尝试:
为什么不直接以增量的形式使用和生成输出数字…nwhy for循环的n@MBo值的上限是多少?当您使用LINQ?OP时,没有发布任何示例代码,我假设要检查的数字没有“存储”在任何地方,只是从1运行到输入数字。做得很好。:)哪里也会直接接受这个方法。何处(有效检查)。。。它已经返回bool,因此与true的比较没有任何作用。。。。为什么要传入
数字
?为什么不把它声明为局部变量呢?另外,如果num
为负数,会发生什么情况?传递数字
,以防止每次调用函数时分配内存。我已经更新了处理负数的方法(在之前取绝对值而循环)不,我的意思是我想这就是你试图避免的,我只是不确定在一般情况下“设置”成本是否值得。现在我很好奇JIT是否能够识别这种情况并重用本地数组……你没事吧。使用局部变量会更好、更干净。我做了一个简单的基准测试。结果表明,使用局部变量的方法是最快的。我将编辑答案。
UniqueDigits(0,15).ToList().ForEach(Console.WriteLine);
foreach (var digit in UniqueDigits(100,50))
{
Console.WriteLine(digit);
}
(1) From left to right, start with the digits 10, then ascend from 2.
For example, the first 4-digit number would be 1023.
(2) Increment the right-most digit that can be incremented to the next available
higher digit unused by digits to its left. Ascend to the right of the
incremented digit with the rest of the available digits, starting with lowest.
Examples: 1023 -> 1024 (4 is unused by the digits left of 3)
^
9786 -> 9801 (8 is unused be the digits left of 7)
^
9658 -> 9670 (7 is unused by the digits left of 5)
^