Skip to content

Commit 601b541

Browse files
committed
Update
1 parent c21151f commit 601b541

20 files changed

+266
-0
lines changed

6/1.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int n = 10;
6+
int arr[10] = {7, 5, 9, 0, 3, 1, 6, 2, 4, 8};
7+
8+
int main(void) {
9+
for (int i = 0; i < n; i++) {
10+
int min_index = i; // 가장 작은 원소의 인덱스
11+
for (int j = i + 1; j < n; j++) {
12+
if (arr[min_index] > arr[j]) {
13+
min_index = j;
14+
}
15+
}
16+
swap(arr[i], arr[min_index]); // 스와프
17+
}
18+
for(int i = 0; i < n; i++) {
19+
cout << arr[i] << ' ';
20+
}
21+
}

6/1.java

Whitespace-only changes.

6/10.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int n;
6+
vector<int> v;
7+
8+
bool compare(int a, int b) {
9+
return a > b;
10+
}
11+
12+
int main(void) {
13+
// N을 입력받기
14+
cin >> n;
15+
16+
// N개의 정수를 입력받아 리스트에 저장
17+
for (int i = 0; i < n; i++) {
18+
int x;
19+
cin >> x;
20+
v.push_back(x);
21+
}
22+
23+
// 파이썬 기본 정렬 라이브러리를 이용하여 정렬 수행
24+
sort(v.begin(), v.end(), compare);
25+
26+
for(int i = 0; i < n; i++) {
27+
cout << v[i] << ' ';
28+
}
29+
}

6/10.java

Whitespace-only changes.

6/11.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Student {
6+
public:
7+
string name;
8+
int score;
9+
Student(string name, int score) {
10+
this->name = name;
11+
this->score = score;
12+
}
13+
// 정렬 기준은 '점수가 낮은 순서'
14+
bool operator <(Student &other) {
15+
return this->score < other.score;
16+
}
17+
};
18+
19+
int n;
20+
vector<Student> v;
21+
22+
int main(void) {
23+
// N을 입력받기
24+
cin >> n;
25+
26+
// N명의 학생 정보를 입력받아 리스트에 저장
27+
for (int i = 0; i < n; i++) {
28+
string name;
29+
int score;
30+
cin >> name >> score;
31+
v.push_back(Student(name, score));
32+
}
33+
34+
sort(v.begin(), v.end());
35+
36+
// 정렬이 수행된 결과를 출력
37+
for(int i = 0; i < n; i++) {
38+
cout << v[i].name << ' ';
39+
}
40+
}

6/11.java

Whitespace-only changes.

6/12.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int n, k;
6+
vector<int> a, b;
7+
8+
bool compare(int x, int y) {
9+
return x > y;
10+
}
11+
12+
int main(void) {
13+
// N과 K를 입력받기
14+
cin >> n >> k;
15+
// 배열 A의 모든 원소를 입력받기
16+
for (int i = 0; i < n; i++) {
17+
int x;
18+
cin >> x;
19+
a.push_back(x);
20+
}
21+
// 배열 B의 모든 원소를 입력받기
22+
for (int i = 0; i < n; i++) {
23+
int x;
24+
cin >> x;
25+
b.push_back(x);
26+
}
27+
// 배열 A는 오름차순 정렬 수행
28+
sort(a.begin(), a.end());
29+
// 배열 B는 내림차순 정렬 수행
30+
sort(b.begin(), b.end(), compare);
31+
32+
// 첫 번째 인덱스부터 확인하며, 두 배열의 원소를 최대 K번 비교
33+
for (int i = 0; i < k; i++) {
34+
// A의 원소가 B의 원소보다 작은 경우
35+
if (a[i] < b[i]) swap(a[i], b[i]); // 두 원소를 교체
36+
// A의 원소가 B의 원소보다 크거나 같을 때, 반복문을 탈출
37+
else break;
38+
}
39+
40+
// 배열 A의 모든 원소의 합을 출력
41+
int result = 0;
42+
for (int i = 0; i < n; i++) {
43+
result += a[i];
44+
}
45+
cout << result << '\n';
46+
}

6/12.java

Whitespace-only changes.

6/2.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int arr[2] = {3, 5};
6+
7+
int main(void) {
8+
swap(arr[0], arr[1]);
9+
cout << arr[0] << ' ' << arr[1] << '\n';
10+
}

6/2.java

Whitespace-only changes.

6/3.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int n = 10;
6+
int arr[10] = {7, 5, 9, 0, 3, 1, 6, 2, 4, 8};
7+
8+
int main(void) {
9+
for (int i = 1; i < n; i++) {
10+
// 인덱스 i부터 1까지 감소하며 반복하는 문법
11+
for (int j = i; j > 0; j--) {
12+
// 한 칸씩 왼쪽으로 이동
13+
if (arr[j] < arr[j - 1]) {
14+
swap(arr[j], arr[j - 1]);
15+
}
16+
// 자기보다 작은 데이터를 만나면 그 위치에서 멈춤
17+
else break;
18+
}
19+
}
20+
for(int i = 0; i < n; i++) {
21+
cout << arr[i] << ' ';
22+
}
23+
}

6/3.java

Whitespace-only changes.

6/4.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int n = 10;
6+
int arr[10] = {7, 5, 9, 0, 3, 1, 6, 2, 4, 8};
7+
8+
void quickSort(int* arr, int start, int end) {
9+
if (start >= end) return; // 원소가 1개인 경우 종료
10+
int pivot = start; // 피벗은 첫 번째 원소
11+
int left = start + 1;
12+
int right = end;
13+
while (left <= right) {
14+
// 피벗보다 큰 데이터를 찾을 때까지 반복
15+
while (left <= end && arr[left] <= arr[pivot]) left++;
16+
// 피벗보다 작은 데이터를 찾을 때까지 반복
17+
while (right > start && arr[right] >= arr[pivot]) right--;
18+
// 엇갈렸다면 작은 데이터와 피벗을 교체
19+
if (left > right) swap(arr[pivot], arr[right]);
20+
// 엇갈리지 않았다면 작은 데이터와 큰 데이터를 교체
21+
else swap(arr[left], arr[right]);
22+
}
23+
// 분할 이후 왼쪽 부분과 오른쪽 부분에서 각각 정렬 수행
24+
quickSort(arr, start, right - 1);
25+
quickSort(arr, right + 1, end);
26+
}
27+
28+
int main(void) {
29+
quickSort(arr, 0, n - 1);
30+
for (int i = 0; i < n; i++) {
31+
cout << arr[i] << ' ';
32+
}
33+
}

6/4.java

Whitespace-only changes.

6/6.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <bits/stdc++.h>
2+
#define MAX_VALUE 9
3+
4+
using namespace std;
5+
6+
int n = 15;
7+
// 모든 원소의 값이 0보다 크거나 같다고 가정
8+
int arr[15] = {7, 5, 9, 0, 3, 1, 6, 2, 9, 1, 4, 8, 0, 5, 2};
9+
// 모든 범위를 포함하는 배열 선언(모든 값은 0으로 초기화)
10+
int cnt[MAX_VALUE + 1];
11+
12+
int main(void) {
13+
for (int i = 0; i < n; i++) {
14+
cnt[arr[i]] += 1; // 각 데이터에 해당하는 인덱스의 값 증가
15+
}
16+
for (int i = 0; i <= MAX_VALUE; i++) { // 배열에 기록된 정렬 정보 확인
17+
for (int j = 0; j < cnt[i]; j++) {
18+
cout << i << ' '; // 띄어쓰기를 기준으로 등장한 횟수만큼 인덱스 출력
19+
}
20+
}
21+
}

6/6.java

Whitespace-only changes.

6/7.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int n = 10;
6+
int arr[10] = {7, 5, 9, 0, 3, 1, 6, 2, 4, 8};
7+
8+
int main(void) {
9+
sort(arr, arr + n);
10+
for (int i = 0; i < n; i++) {
11+
cout << arr[i] << ' ';
12+
}
13+
}

6/7.java

Whitespace-only changes.

6/9.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Fruit {
6+
public:
7+
string name;
8+
int score;
9+
Fruit(string name, int score) {
10+
this->name = name;
11+
this->score = score;
12+
}
13+
// 정렬 기준은 '점수가 낮은 순서'
14+
bool operator <(Fruit &other) {
15+
return this->score < other.score;
16+
}
17+
};
18+
19+
int main(void) {
20+
int n = 3;
21+
Fruit fruits[] = {
22+
Fruit("바나나", 2),
23+
Fruit("사과", 5),
24+
Fruit("당근", 3)
25+
};
26+
sort(fruits, fruits + n);
27+
for(int i = 0; i < n; i++) {
28+
cout << '(' << fruits[i].name << ',' << fruits[i].score << ')' << ' ';
29+
}
30+
}

6/9.java

Whitespace-only changes.

0 commit comments

Comments
 (0)