数组中的不同数字 [英] Distinct numbers in array

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

问题描述

我不知道该怎么办。请帮助我的代码或告诉我什么教科书要查找或什么;我需要代码来完成这个程序,我很想解释我正在看什么。

 #include< iostream> ; 
using namespace std;

int main()
{
short num [100],size,//声明一个short类型的数组,其中包含100个元素
unique [100] number,//声明一个第二个数组来帮助解决问题; number计数唯一值的数量
k; //循环控制变量可能需要其他变量
cout<<输入要存储在数组\\\
中的值的数量;
cin>> size;
cout<<输入要用作该程序的数据的<<<<
for(k = 0; k< size; k ++)
cin>> num [k]

//打印数组的内容
cout <<\\\
there在数组\ n中是< for(k = 0; k cout cout<< endl; //光标移动到下一个输出行

cout<<程序将计数在数组\\\
中找到的不同(不同)值的数量;

// **************************************** ********************
//将这里的代码计算存储在
//数组num中的唯一值的数量。变量号将包含计数。
// ********************************************在<<< size<中发现了****************

cout<< endl< <element array\\\
;
//暂停程序以查看结果
system(pause);
// return 0;
}

我必须做这两件事之一,它们是什么意思?



算法 - 唯一数组用于帮助找到解决方案,用于避免对任何值计数多次

将数字设置为0 - 表示数据集中不同值的数量;也用作唯一数组中的下标

从0到大小逐个循环,继续遍历数据(num)数组的连续元素

  存储当前值非数组变量(SV)中的数组元素

  将event_flag设置为0

  从0到数字循环,继续遍历连续的唯一数组

    如果SV等于唯一数组的当前元素

   将event_flag设置为1 < br>
    中断(停止)内循环

  内循环结束

  如果event_flag相等到0(在唯一数组中找不到值,以前未计数)

    将SV存储在唯一数组的元素号中

  值

外循环结束

解决方案 - 变量号包含数据数组中不同值的计数



替代算法

不使用event_flag的算法(循环控制变量可用于确定是否发生事件)



算法 - 唯一数组用于帮助查找解决方案,用于避免对任何值计数多次

将数字设置为0 - 这表示不同值的数量在数据集中;也用作唯一数组中的下标

从0到大小逐个循环,继续遍历数据(num)数组的连续元素

  存储当前值非数组变量(SV)中的数组元素

  从0到数字循环,继续遍历唯一数组的连续元素

     ;如果SV等于唯一数组的当前元素

       中断(停止)内循环

  循环

  如果内循环的循环控制变量等于数值(SV在唯一数组中找不到且以前未计数)。

  存储SV元素编号的唯一数组

  递增数字的值

外循环结束

解决方案 -




pre pre pre pre $ > // ***************************************** *******************
//将代码放在这里计算存储在数组num中的唯一值的数量。变量号将包含计数。
for(k = 0; k num = SV;
event_flag = 0
for(k = 1; k if(SV = unique)
return true;
return false;
// ******************************************** ****************

这不工作,显然。

解决方案

这是我的代码,似乎工作

  // **************************************** ******************** 
//将代码放入计算唯一值数量
//存储在数组num中。变量号将包含计数。

number = 0;
for(k = 0; k {
short sv = num [k]
short event_flag = 0;
for(int i = 0; i {
if(sv == unique [i])
{
event_flag = 1;
break;
}
}

if(event_flag == 0)
{
unique [number] = sv;
++ number;
}
}

b

他是我的代码,似乎工作

  // ******* **************************************************** *** 
//将代码放入计算唯一值数量
//存储在数组num中。变量号将包含计数。

number = 0;
for(k = 0; k {
short sv = num [k]
int i;
for(i = 0; i if(sv == unique [i])
break;

if(number == i)
{
unique [number] = sv;
++ number;
}
}


I have no idea what to do. Please help me with code or tell me what textbook to look up or something; I need code to finish this program and I would love an explanation of what I'm looking at..

#include<iostream>
using namespace std;

int main()
{    
   short num[100], size, //declare an array of type short that has 100 elements
      unique[100], number,  // declare a second array to help solve the problem; number counts the number of unique values
      k;   // loop control variable; may need other variables
   cout<<"enter the number of values to store in the array\n";
   cin>>size;
   cout<<"enter the "<<size<<" values to be used as data for this program\n";
   for(k=0; k<size; k++)
      cin>>num[k];

   // print the contents of the array
   cout<<"\nthere are "<<size<<" values in the array\n";
   for(k=0; k<size; k++)
      cout<<num[k]<<’ ‘; // there is one space between each number in the display
   cout<<endl;  // cursor moved to next output line

   cout<<"the program will count the number of different (distinct) values found in the array\n";   

   //************************************************************
   //Put the code here that counts the number of unique values stored in the 
   //array num.  The variable number will contain the count.
   //************************************************************

   cout<<endl<<number<<" unique values were found in the "<<size<<" element array\n";
   // pause the program to see the results
   system("pause");
   //return 0;       
}

I have to do one of these two things and I don't know what they mean?

Algorithm – unique array is used to help find the solution, used to avoid counting any value more than one time
Set number to 0 - this represents the number of distinct values in the data set; also used as a subscript in the unique array
Loop from 0 to size by one, proceeding through successive elements of the data (num) array
  Store value of current array element in non-array variable (SV)
  Set event_flag to 0
  Loop from 0 to number by one, proceeding through successive elements of unique array
    If SV is equal to current element of unique array
      Set event_flag to 1
    Break (stop) inner loop
  End of inner loop
  If event_flag is equal to 0 (value not found in unique array and not previously counted)
    Store SV in element number of unique array
  Increment the value of number
End of outer loop
Solution – the variable number contains the count of distinct values in the data array

Alternate Algorithm
Algorithm that does not use the event_flag (loop control variable can be used to determine if event occurred)

Algorithm – unique array is used to help find the solution, used to avoid counting any value more than one time
Set number to 0 - this represents the number of distinct values in the data set; also used as a subscript in the unique array
Loop from 0 to size by one, proceeding through successive elements of the data (num) array
  Store value of current array element in non-array variable (SV)
  Loop from 0 to number by one, proceeding through successive elements of unique array
    If SV is equal to current element of unique array
      Break (stop) inner loop
  End of inner loop
  If loop control variable of inner loop is equal to value of number (SV not found in unique array and not previously counted)
  Store SV in element number of unique array
  Increment the value of number
End of outer loop
Solution – the variable number contains the count of distinct values in the data array

I put this in mine:

//************************************************************
//Put the code here that counts the number of unique values stored in the array num. The variable number will contain the count.
for(k=0; k<size; k++)
num=SV;
event_flag=0;
for(k=1; k<number; k++)
if(SV=unique)
return true;
return false; 
//************************************************************

It's not working, obviously.

解决方案

This is my code, it seems to work

//************************************************************
//Put the code here that counts the number of unique values 
//stored in the array num. The variable number will contain the count.

number = 0;
for (k = 0; k < size; ++k)
{
       short sv = num[k];
       short event_flag = 0;
       for (int i = 0; i < number; ++i)
       {
               if (sv == unique[i])
               {
                       event_flag = 1;
                       break;
               }
       }

       if (event_flag == 0)
       {
               unique[number] = sv;
               ++number;
       }
}

For the alternative ,

his is my code, it seems to work

//************************************************************
//Put the code here that counts the number of unique values 
//stored in the array num. The variable number will contain the count.

number = 0;
for (k = 0; k < size; ++k)
{
       short sv = num[k];
       int i;
       for (i = 0; i < number; ++i)
               if (sv == unique[i])
                       break;

       if (number == i)
       {
               unique[number] = sv;
               ++number;
       }
}

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

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