我想对第一个数组进行排序以找到共同的元素。帮我按最好的方式排序;) 首先,用户输入Array1的长度,然后输入Array1的元素 第二个输入Array2的长度(它应该小于Array1的长度) 然后输入Array2的元素。 最后我想排序Array1查找常用元素。 “那么哪种方式更容易排序?”
#include "stdafx.h"
#include <iostream>
using namespace std;
void check (int[], int[]);
int _tmain(int argc, _TCHAR* argv[])
{
int n1, n2, arr1[50], arr2[50];
cout << " How many elements you want to enter to Array 1 : ";
cin >> n1;
for(int i=0; i<n1; i++)
{
if (n1 >= 50)
cout << " your input is more than MAX !!! " << endl;
else
cout << " Enter elements " << "[" << i <<"]: ";
cin >> arr1[i];
}
cout << endl;
cout << " How many elements you want to enter to Array 2 : ";
cin >> n2;
if (n2 >= n1)
cout << " your input is more than Array 1 !!! " << endl;
else {
for (int j=0; j<n2; j++)
{
cout << " Enter elements " << "[" << j << "]: ";
cin >> arr2[j];
}
for (int i=0; i<n1; i++)
cout << arr1[i] << "\t";
cout << endl;
for (int j=0; j<n2; j++)
cout << arr2[j] << "\t";
}
cout << endl;
check (arr1, arr2);
system ("pause");
return 0;
}
void check (int x[], int y[])
{
int temp;
cout << " Common elements are: " << endl;
for ( int i=0; i<y[i]; i++ )
{
if (x[i] == y[i])
temp = x[i];
cout << temp << "\t";
}
cout << endl;
}
答案 0 :(得分:0)
添加
#include <algorithms>
现在您可以轻松地将元素排序为
std::sort(arr,arr+arr_length);
你在哪里存储元素中的元素?