如何防止重复数字...... [英] How do I prevent repeating numbers...

查看:72
本文介绍了如何防止重复数字......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我是Visual Basic C3的新手。

我想把数字串起来但没有任何重复。我如何写一个if语句来询问数字但不允许重复之前的条目?

感谢您的帮助。

Thomas Wallace

推荐答案

唯一的方法是在一个集合中记录之前的数字某种形式。

例如,列表:

The only way to do it is to keep a record of the "previous numbers" in a collection of some form.
For example, a list:
private List<int> previousValues = new List<int>();
...
int value;
if (int.TryParse(userInput.Text, out value))
   {
   if (!previousValues.Contains(value))
      {
      previousValues.Add(value);
      ...
      }
   }


我理解这个问题是关于如何设计检测重复值的算法。



如果是关于这些问题的话,你可能会首先考虑如何手动这样做?



例如假设,我告诉你一百个数字,你必须把它们写下来,忽略那些已经给出的数字。你会做什么?



我想你迟早想出类似的东西:

I understand this question is about how to design an algorithm for detecting repeated values.

When it's about such questions, you might think first how you would do it "manually"?

E.g. assume, I tell you one hundred numbers and you had to write them down, ignoring those which were already given. What would you do?

I guess you sooner or later come up with something like:


  1. 有一张空纸,作为已经提到的数字的记忆
  2. 取下一个数字
  3. 在视觉上扫描纸张并决定是否这个数字已经提到了
  4. 如果有人提到(你在纸上发现了它),请忽略它
  5. 否则,将数字写在纸上
  6. 转到第1步,直到没有留下任何号码



如果您认为给定的方法正在解决您的问题,请尝试将其映射到程序中。

为此,您需要了解一下有什么以及您需要发明什么。



在这里,你基本上需要一个容器来记住唯一的数字,一种询问容器中是否已经给出值的方法,最后是一种向容器添加值的方法。



有C#中的几个容器。这些容器中的每一个对于某些目的特别好并且不太适合于其他目的。为了有效地使用容器,您需要在C#提供的各种容器上获取​​知识。



对于您的情况, HashSet 是选择的容器:它是一堆值,允许只存储唯一值,并允许快速找到hey堆栈中的针(值)。 HashSet 不适合存储非唯一值,并且维护一系列值是不好的(例如,您无法检索添加值的序列)。 />


因此,转换是:带有值的纸张是C#中的 HashSet



可能的C#代码:


If you think that the given approach is solving your problem, try to map this into a program.
For this, you need to know a bit on what is there and what you need to invent.

Here, you basically need a container to "remember" the unique numbers, a way to ask if the value is already given in the container, and finally, a way to add a value to the container.

There are several containers in C#. Each of these containers is particularly good for certain purposes and less suited for other purposes. To be effective in using containers, you need to acquire knowledge over time on the various containers C# provides.

For your case, a HashSet is the container of choice: it's a "pile" of values that allows to store only unique values and that allows to quickly find "the needle (a value) in the hey stack". The HashSet is not good for storing non-unique values and it is not good to maintain a sequence of values (e.g. you cannot retrieve the sequence you added the values).

So, the transformation is: the sheet of paper with its values is the HashSet in C#.

Possible C# code:

HashSet<int> uniqueValues = new HashSet<int>();
...
int value = ...;
// only process values which were not yet given - ignore others
if (!uniqueValues.Contains(value))
{
    uniqueValues.Add(value);
    //... do something meaningful here with the given value...
}





干杯

Andi



Cheers
Andi


这篇关于如何防止重复数字......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆