프로그래밍 문제/c++ 문제
c++ 문제 27. 주어진 배열의 중복된 숫자를 동적 배열로 이전
지나팩
2023. 11. 2. 16:22
주어진 배열의 중복된 숫자를 동적 배열을 활용해 이전하고 출력하는 코드를 작성하세요.
중복된 숫자가 이미 이전되었다면 중복으로 이전하지 않음.
int arr[] = { 1,2,1,3,7,9,4,6,3,8,3};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 1,2,1,3,7,9,4,6,3,8,3 };
int num = sizeof(arr) / sizeof(*arr);
int* temp = nullptr;
int count = 0;
for (int i = 0; i < num; i++)
{
for (int j = i + 1; j < num; j++)
{
if (arr[i] == arr[j])
{
int sour = arr[j];
if (temp == nullptr) {
temp = new int[1];
temp[count++] = sour;
}
else
{
for (int k = 0; k < count; k++)
{
if (sour == temp[k]) {
sour = 0;
break;
}
}
if (sour != 0)
{
int* t = temp;
temp = new int[++count];
for (int i = 0; i < count; i++)
{
if (i < count - 1) {
temp[i] = t[i];
}
else temp[i] = sour;
}
}
}
}
}
}
cout << "중복된 숫자" << endl;
for (int i = 0; i < count; i++)
{
cout << temp[i] << endl;
}
return 0;
}
|
cs |