C++ 数组中的不同数

C++ 数组中的不同数,c++,arrays,algorithm,C++,Arrays,Algorithm,我不知道该怎么办。请帮我写代码或者告诉我要查什么课本之类的;我需要代码来完成这个程序,我想解释一下我在看什么 #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 arr

我不知道该怎么办。请帮我写代码或者告诉我要查什么课本之类的;我需要代码来完成这个程序,我想解释一下我在看什么

#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;       
}
#包括
使用名称空间std;
int main()
{    
short num[100],size,//声明一个short类型的数组,该数组包含100个元素
unique[100],number,//声明第二个数组以帮助解决问题;number计算唯一值的数量
k、 //循环控制变量;可能需要其他变量
coutsize;

cout大致要求您执行以下操作:

#include <iostream>

using namespace std;

int main()
{
    // This is the given array.
    int given_array[5] = { 1, 1, 2, 2, 3 };

    // This is the array where unique values will be stored.
    int unique_array[5];

    // This index is used to keep track of the size
    // (different from capacity) of unique_array.
    int unique_index = 0;

    // This is used to determine whether we can
    // insert an element into unique_array or not.
    bool can_insert;

    // This loop traverses given_array.
    for (int i = 0; i < 5; ++i)
    {
        // Initially assume that we can insert elements
        // into unique_array, unless told otherwise.
        can_insert = true;

        // This loop traverses unique_array.
        for (int j = 0; j < unique_index; ++j)
        {
            // If the element is already in unique_array,
            // then don't insert it again.
            if (unique_array[j] == given_array[i])
            {
                can_insert = false;
                break;
            }
        }

        // This is the actual inserting.
        if (can_insert)
        {
            unique_array[unique_index] = given_array[i];
            unique_index++;
        }
    }

    // Tell us how many elements are unique.
    cout << unique_index;

    return 0;
}
#包括
使用名称空间std;
int main()
{
//这是给定的数组。
给定的int_数组[5]={1,1,2,2,3};
//这是存储唯一值的数组。
int唯一_数组[5];
//此索引用于跟踪大小
//唯一_阵列的(不同于容量)。
int unique_index=0;
//这是用来确定我们是否可以
//是否将元素插入到唯一的数组中。
bool can_插入;
//这个循环遍历给定的数组。
对于(int i=0;i<5;++i)
{
//最初假设我们可以插入元素
//除非另有说明,否则将其放入唯一的_数组中。
can_insert=true;
//此循环遍历唯一的_数组。
对于(int j=0;jcout这是我的代码,似乎有效

//************************************************************
//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;
       }
}
//************************************************************
//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;
       }
}
//************************************************************
//将计算唯一值数量的代码放在这里
//存储在数组num中。变量编号将包含计数。
数字=0;
对于(k=0;k
作为替代方案

他的密码是我的,似乎有用

//************************************************************
//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;
       }
}
//************************************************************
//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;
       }
}
//************************************************************
//将计算唯一值数量的代码放在这里
//存储在数组num中。变量编号将包含计数。
数字=0;
对于(k=0;k
试试这个

您可以根据需要插入
cout
语句

#include <iostream>
using namespace std;

int main() 
{
    int Input[100], Unique[100], InSize, UniLength = 0;

    cin >> InSize;
    for (int ii = 0 ; ii < InSize ; ii++ ) 
    {
        cin >> Input[ii];
    }
    Unique[0] = Input[0];
    UniLength++;
    bool IsUnique;
    for ( int ii = 1 ; ii < InSize ; ii++ ) 
    {
        IsUnique=true;
        for (int jj = 0 ; jj < UniLength ; jj++ ) 
        {
            if ( Input[ii] == Unique[jj] )
            {
                IsUnique=false;
                break;
            }                     
        }                      
        if ( IsUnique )  
        {          
            Unique[UniLength] = Input[ii];
            UniLength++;
        }
    }
    cout<<"We've "<<UniLength<<" Unique elements and We're printing them"<<endl;
    for ( int jj = 0 ; jj < UniLength ; jj++ ) 
    {
        cout << Unique[jj] << " ";
    }
}
#包括
使用名称空间std;
int main()
{
int输入[100],唯一[100],InSize,UniLength=0;
cin>>InSize;
对于(int ii=0;ii>输入[ii];
}
唯一[0]=输入[0];
UniLength++;
布尔是独一无二的;
对于(int ii=1;ii这是我的方法,希望能对你有所帮助

#include<iostream>
#include<algorithm>

int main(){

    int arr[] = {3, 2, 3, 4, 1, 5, 5, 5};
    int len = sizeof(arr) / sizeof(*arr); // Finding length of array

    std::sort(arr, arr+len);

    int unique_elements = std::unique(arr, arr+len) - arr;

    std::cout << unique_elements << '\n'; // The output will 5

    return 0;
}
#包括
#包括
int main(){
int arr[]={3,2,3,4,1,5,5};
int len=sizeof(arr)/sizeof(*arr);//查找数组的长度
标准::排序(arr,arr+len);
int unique_elements=std::unique(arr,arr+len)-arr;

std::你可以这样做吗,我可以这样做,但是OP已经被非常精确地告知要做其他事情。你绝对不应该使用
k
两次。如果你有两个循环,你应该为每个循环使用不同的变量。而且它应该是
If(SV==unique)
而不是
If(SV=unique)记住,对于等式,C++中的代码是“代码>=< /代码>,也就是<代码>返回<代码>是错误的,在任一种算法中都没有什么需要使用<代码>返回<代码>。但是基本上问题是,你的代码看起来不像任何一种算法。算法已经被写得如此详细,以至于它们几乎已经是代码。翻译那里的内容,你不需要太多的想象力,这几乎就像复制。对数组进行排序。打印出与前一个不同的数组。排序位在这里完成。我们中的任何人都不敢回答这个问题。那些不尊重两个关键点的人:第一,他们为你提供了一个偏离的解决方案,第二,他们阻止你采取行动理解如何编程。我们可能只会给你一些提示来帮助你的学习曲线……也就是说,
返回true;
返回false;
都结束了你的函数,这意味着它的其余部分将不会被执行。因此,在这一点上它是错误的。这里有“与之前不同”的部分