排序数组C ++没什么问题 [英] Little problem with sorting arrays C++
问题描述
大家好我想要用户输入两个数组
我想排序的方式是哪一个更大,输出越大
i设法达到但是我有问题它不断重复相同的元素
hi guys i wanted too sort two arrays enterd by the user
the way i wanted to sort is which one is bigger the bigger is outputted
i managed to that but i have problem it keeps repeating equal elements
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
void inputnumbers();
void sfr(int a[],int b[],int c[]);
int main(){
cout << "Enter two arrays element :-" << endl;
inputnumbers();
return 0;
}
void inputnumbers(){
int z=10;
int a[z]={};
cout << "Enter first array :- " << endl;
for (int i=0;i < 10 ; i++){
cin >> a[i]; }
cout << "Enter Second array elements :-" << endl;
int b[z]={};
for (int j=0;j < 10 ; j++){
cin >>b[j];
}
int c[]={};
sfr(a,b,c);
}
void sfr(int a[],int b[],int c[]){
const int NUM_ELEMS = 10;
a[NUM_ELEMS] = {};
b[NUM_ELEMS] = {};
c[NUM_ELEMS] = {};
for (int i = 0; i < NUM_ELEMS; i++)
{
c[i] = a[i];
}
for (int i = NUM_ELEMS, j=0; i < NUM_ELEMS * 2; i++, j++)
{
c[i] = b[j];
}
cout <<"\nArray c unsorted: ";
for (int i = 0; i < NUM_ELEMS * 2; i++)
{
cout << c[i] << " ";
}
cout << "\n\n";
sort(c, c + NUM_ELEMS * 2, greater<int>());
cout <<"\nArray c sorted: ";
for (int i = 0; i < NUM_ELEMS * 2; i++)
{
cout << c[i] << " ";
}
cout << "\n\n";}
我尝试过:
这是输出:
输入两个数组元素: -
输入第一个数组: -
1
2
3 br />
4
5
6
7
8
9
10
输入第二个数组元素: -
30
4
22
5
$ b $ 70
44
20
10 br />
6
8
阵列c未分类:1 2 3 4 5 6 7 8 9 10 30 4 22 5 -70 44 20 10 6 8
阵列c已分类:44 30 22 20 10 10 9 8 8 7 6 6 5 5 4 4 3 2 1 -70
What I have tried:
this is the output:
Enter two arrays element :-
Enter first array :-
1
2
3
4
5
6
7
8
9
10
Enter Second array elements :-
30
4
22
5
-70
44
20
10
6
8
Array c unsorted: 1 2 3 4 5 6 7 8 9 10 30 4 22 5 -70 44 20 10 6 8
Array c sorted: 44 30 22 20 10 10 9 8 8 7 6 6 5 5 4 4 3 2 1 -70
推荐答案
是的。输入中有重复的值,因此它们在逻辑上会出现在输出中。
如果不需要它们,那么你需要允许一个不是固定长度的返回数组 - 因为如果从输出中删除10,8,6,5和4,则数组末尾将有5个未使用元素c
并且无法判断有多少元素是真实的。
排序算法通常不会删除重复项。
如果你想要这样做,我会对数组进行后处理并将其缩小到另一个数组中(这非常简单)
Well yes. There are duplicate values in the input, so they will logically be present in the output.
If they aren't wanted, then you need to allow for a return array which is not of a fixed length - because if you remove a "10", an "8", a "6", a "5", and a "4" from your output you will have 5 "unused" elements at the end of the arrayc
and no way to tell how many elements are "real".
Sort algorithms normally don't remove duplicates.
If you want to do that, I'd post-process the array and "shrink" it into another array (which is pretty trivial)
你有两个选择:
- 用正确的排序实现替换错误的排序实现,修复它。调试器会给你很多帮助。
- 使用可用的排序实现,首先,标准:
sort - C ++参考a> [ ^ ],
标准初学者指南:: sort()函数 - C ++文章 [ ^ ]。
You have two options:
- Replace your incorrect sorting implementation with correct one, fix it. The debugger will give you a lot of help.
- Use available sorting implementation, first of all, standard:
sort — C++ Reference[^],
Beginners guide to the std::sort() funct — C++ Articles[^].
这篇关于排序数组C ++没什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!