数组重复 [英] array duplications

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

问题描述

任何人都可以告诉我如何为数组编写一个循环,这将需要每次迭代检查以前输入值的重复。所以,

确切的循环次数是未知的,但是已知的数组元素的总数是




示例:数组大小 - 数组[100]

(某种类型的循环语句)

{

array [n]"等于或不等于或一个变量。如果不是重复,

将计数增加一。如果是重复,请检查下一个变量

是否有重复等等。

}


这只是我的方法已经尝试了,但它没有用。


而(i <100)

{

ct + = 1;


" statements"

for(n = 0; n< ct; n ++)

{

array [n]!= var.name? array [n] == var.name:n--

}

}

解决方案

< blockquote> cdg写道:

任何人都可以告诉我如何为数组编写一个循环,它可以检查每次迭代是否有先前输入的值的重复。所以,


std :: vector< T>数组;

而(数组大小小于100)

{

T值;

读数值;

if(找不到数组中的值)

将值推到数组末尾

}


这是一个非常简单的算法。你正在做的事情相当

不同:

而(i <100)


哪里是i来自?

{
ct + = 1;


数组内容总是增加几乎可以肯定是不是你想要的。
语句
for(n = 0; n< ct; n ++)
{
array [n]!= var.name? array [n] == var.name:n -




想一想!为什么要在每个

迭代中分配新值?此外,您需要跟踪

变量的含义! ''n''只是本地循环索引,你必须

确保它处理循环终止。你的数组

大小显然是在''ct''中跟踪的。搜索

元素的循环非常简单,只需确定每个元素

是否与当前值不同。如果不是,则

循环终止并提供是否终止

的指示,因为检查了所有数组元素或发现了重复。

-

< mailto:di *********** @ yahoo.com> < http://www.dietmar-kuehl.de/>

< http://www.eai-systems.com> - 高效的人工智能


我不确定你会如何写出你的建议。但是

我考虑过这种方法(下图)。你能否建议更正,因为

它无法正常工作。

我认为这是一个简单的算法,但似乎更多

比其他一些循环问题复杂。我正在使用这本书

" Starting Out with C ++"并没有很多经验。另外,我还没有使用向量开始

,这些都不在书中。


do

{

"


for(n = nn; n = 100?n = 100:n<(n + 1); n ++)// nn for zero

{

if(array [n]!= var_name)

{

array [n] == var_name ;

休息;

}

if(array [n] == var_name)

n--;

}

}

while(n <100);


cdg写道:

我不确定你会如何写出你的建议。


好​​吧,我的描述中的每一行都可以翻译成单个执行所述任务的
C ++行。当然,找到一个

匹配元素可能会导致多行,如果你不想要

(或者不知道如何)使用''std :: find()''算法。

您能否建议更正,因为它无法正常工作。


按照单独的步骤进行操作并按照我之前的说明进行操作:您应该清楚地知道您的变量用于什么。将每个变量

用于一个任务,每个任务只使用一个变量。如果你没有使用''std :: vector''但是使用了一系列元素,你需要

来保持数组中当前元素的数量。要找到数组中潜在元素

,您需要处理当前索引

加上当然正在寻找的值。在

中,你可能想要替换文字100。命名为

常量可以轻松修改数组大小。

do
{
" statements"


我想那个陈述是指获取变量var_name的内容的操作......?这个

变量是什么类型的?某些类型可以使用等于运算符

进行比较,但不会产生您可能想要的内容。特别是,
''char''的数组不能与'=='相比,而''std :: string'的数组可以。

for (n = nn; n = 100?n = 100:n<(n + 1); n ++)// nn为零
{
if(array [n]!= var_name) {
array [n] == var_name;
break;
}
if(array [n] == var_name)
n - ;
}




按照我的建议做:

- 首先尝试在现有数组中找到元素

合适的循环。

- 然后,在此循环之外,确定元素是否可以位于
。如果它不是只是将元素添加到你的数组并且

增加你的元素数量。

-

< mailto:di ***********@yahoo.com> < http://www.dietmar-kuehl.de/>

< http://www.eai-systems.com> - 高效的人工智能


Could anyone tell me how to write a loop for an array, that would have to
check each iteration for duplications of previous entered values. So, the
exact number of loops is not known, but the total number of array elments is
known.

Example: array size - array[100]

(some type of loop statement)
{
array[n] "equal to" or "not equal to" a variable. If not a duplication,
increase the count by one. If it is a duplication, check the next variable
for duplication and so on.
}

This is only approach I have tried, but it is not working.

while(i<100)
{
ct += 1;

"statements"

for(n=0; n<ct; n++)
{
array[n] != var.name ? array[n] == var.name : n--
}
}

解决方案

cdg wrote:

Could anyone tell me how to write a loop for an array, that would have
to check each iteration for duplications of previous entered values. So,
std::vector<T> array;
while (array size is less than 100)
{
T value;
read value;
if (not find value in array)
push value to the end of array
}

It is a pretty simple algorithm. You are doing something rather
different:
while(i<100)
Where is "i" coming from?
{
ct += 1;
The array content is always increased which is almost certainly
not what you want.
"statements"

for(n=0; n<ct; n++)
{
array[n] != var.name ? array[n] == var.name : n--



Think of it! Why would you want to assign the new value in each
iteration? Also, you need to track what the meaning of your
variables is! ''n'' is just the local loop index and you have to
make sure that it processes for the loop to terminate. You array
size is apparently tracked in ''ct''. The loop searching for an
element is pretty simple and just determines for each element
whether it is different to the current value. If it is not, the
loop terminates and provides an indication on whether it terminated
because all array elements were examined or a duplicate was found.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence


I am not exactly sure of how you would write what you had suggested. But
I had considered this approach(below). Could you suggest corrections, since
it does not work correctly.
And I would think that this is a simple algorithm, but seems more
complicated than some other looping problems. And I am using the book
"Starting Out with C++" and not very experienced. Also, I have not started
using vectors, these are not in the book.

do
{
"statements"

for(n=n-n; n=100?n=100:n<(n+1); n++) //n-n for zero
{
if(array[n] != var_name)
{
array[n] == var_name;
break;
}
if(array[n] == var_name)
n--;
}
}
while(n<100);


cdg wrote:

I am not exactly sure of how you would write what you had suggested.
Well, each line in my description can be translated to a single
C++ line performing the described task. Of course, finding a
matching element may result in multiple lines if you don''t want
(or don''t know how) to use the ''std::find()'' algorithm.
Could you suggest corrections, since it does not work correctly.
Proceed in separate steps and do what I stated before: you shall
become clear what your variables are used for. Use each variable
for exactly one task and for each task only one variable. If you
don''t use ''std::vector'' but an array of elements, you need to
maintain the number of elements currently in the array. To locate
a potential element within the array, you need a current index
being processed plus, of course, the value you are seeking. In
addition you might want to replace the literal "100" with named
constant to easily modify the array size.
do
{
"statements"
I guess that "statements" refers to the operations obtaining
the content of the variable "var_name"...? What type has this
variable? Some types can be compared using the equality operator
but do not yield what you might intent. In particular, arrays of
''char'' cannot be compared with "==", while ''std::string''s can.
for(n=n-n; n=100?n=100:n<(n+1); n++) //n-n for zero
{
if(array[n] != var_name)
{
array[n] == var_name;
break;
}
if(array[n] == var_name)
n--;
}



Do as I have suggested:
- First try to locate the element within the existing array in a
suitable loop.
- Then, outside this loop, determine whether an element could be
located. If it wasn''t just add the element to your array and
increase your element count.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence


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

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