이 문제를 이해하는 데 문제가 있습니다. 사용자에게 20 개의 값을 읽어 들이고 10에서 100 사이인지 확인합니다. 데이터가 유효하면 중복되지 않는 경우에만 배열에 저장하십시오. 20 개의 값을 살펴본 후에는 고유 한 값만 표시합니다. 나는 왜 그것을 알지 못한다. 그러나 그것이 중복인지 아닌지에 관계없이 모든 코드를 저장하고있다. 어떤 도움을 주셔서 감사합니다!배열로 중복 제거
if(duplicate[i] == numberEntered)
{
cout << "This number was already entered " << endl;
dup = true;
break;
}
그렇지 중복 검사 루프 인덱스 j
에 대해, duplicate[i]
에 대해 확인 중에서
#include <iostream>
using namespace std;
int main()
{
int duplicate[20];
int numberEntered;
int currentIndex = 0;
bool dup = false;
for (int i = 0; i < 20; i++) duplicate[i] = 0; //initializes all indices to 0
cout << "Enter 20 numbers " << endl;
for (int i = 0; i <= 20; i++)
{
cout << "Enter Number " << endl;
cin >> numberEntered;
if((numberEntered > 10) && (numberEntered < 100))
{
for (int j = 0; j < currentIndex; j++)
{
if(duplicate[i] == numberEntered)
{
cout << "This number was already entered " << endl;
dup = true;
break;
}
}
if(dup==false)
{
duplicate[currentIndex] = numberEntered;
currentIndex++;
}
}
else
{
cout << "Invalid Number, must be between 10 and 100 " << endl;
i -- ;
}
}
for (int i = 0; i < currentIndex; i++)
{
cout << duplicate[i] << endl;
}
return 0;
}
'std :: find_if'와 같은 알고리즘을 사용하십시오. 코드가 훨씬 간단해질 수 있습니다. – Mahesh