728x90

https://www.acmicpc.net/problem/2752

😢 단순 비교도 가능하지만, 정렬로 쉽게 풀 수 있는 문제

😊 각종 정렬을 이용해 풀 수 있음(여기선 선택정렬 selection sort)

// 세 수 정렬하기
#include <bits/stdc++.h>
using namespace std;
int main(void) {
ios::sync_with_stdio(false), cin.tie(nullptr);
int a, b, c;
cin >> a >> b >> c;
int d, e, f;
d = min({a, b, c});
f = max({a, b, c});
e = a + b + c - d - f;
cout << d << ' ' << e << ' ' << f;
}
/*
#include <bits/stdc++.h>
using namespace std;
int main(void) {
ios::sync_with_stdio(false), cin.tie(nullptr);
int arr[4];
for (int i = 0; i < 3; i++) cin >> arr[i];
sort(arr, arr + 3);
for (int i = 0; i < 3; i++) cout << arr[i] << ' ';
}
*/
/*
#include <stdio.h>
int arr[3] = {};
void selectionSort(int *arr) {
int i, j;
int indexOfMin;
int lesser;
for (i = 0; i < 2; i++) {
indexOfMin = i;
for (j = i + 1; j < 3; j++) {
if (arr[indexOfMin] > arr[j]) {
indexOfMin = j;
}
}
if (indexOfMin != i) {
lesser = arr[indexOfMin];
arr[indexOfMin] = arr[i];
arr[i] = lesser;
}
}
}
void print(int arr[]) {
for (int i = 0; i < 3; i++) printf("%d ", arr[i]);
}
int main(void) {
for (int i = 0; i < 3; i++) scanf("%d", &arr[i]);
selectionSort(arr);
print(arr);
return 0;
}
*/
/* 단순 비교 연산
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int a[3] = {};
for (int i = 0; i < 3; i++) {
cin >> a[i];
}
if (a[0] < a[1] && a[0] < a[2]) {
cout << a[0] << " ";
if (a[1] < a[2]) {
cout << a[1] << " " << a[2];
} else {
cout << a[2] << " " << a[1];
}
} else if (a[1] < a[0] && a[1] < a[2]) {
cout << a[1] << " ";
if (a[0] < a[2]) {
cout << a[0] << ' ' << a[2];
} else {
cout << a[2] << ' ' << a[0];
}
} else {
cout << a[2] << ' ';
if (a[0] < a[1]) {
cout << a[0] << ' ' << a[1];
} else {
cout << a[1] << ' ' << a[0];
}
}
}
*/
view raw 2752.cpp hosted with ❤ by GitHub

'Algorithm > C&C++' 카테고리의 다른 글

백준 2753번: 윤년(c++)  (0) 2021.09.29
백준 1000번: A+B(c++)  (0) 2021.09.27
백준 10871번: X보다 작은 수 c++(cpp)  (0) 2021.09.06
백준 10804번: 카드 역배치 c++(cpp)  (0) 2021.09.02
백준 2440번: 별 찍기 - 3 c++(cpp)  (0) 2021.09.01
728x90

https://www.acmicpc.net/problem/10871

😢 N개의 수열을 받아서 X보다 작은지 판단하는 문제

😊 N, X을 먼저 입력받고, N만큼 loop, a는 X보다 작은지 판단하여 출력

#include <iostream>
using namespace std;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int N, X;
cin >> N >> X;
for (int i = 0; i < N; i++) {
int a;
cin >> a;
if (a < X) cout << a << ' ';
}
}
view raw 10871.cpp hosted with ❤ by GitHub
728x90

https://www.acmicpc.net/problem/10804

😢 iterate & swap!

😊 입력 받는 두 수(a, b)를 반복해서 입력해줘야하며,
a++, b--로 두 수를 증가 또는 감소시키는 것을 반복해야한다.
두 수가 같아졌을 경우 반복문을 빠져나오는 조건을 걸어줘야한다(a < b)

#include <iostream>
using namespace std;
int arr[21];
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
for (int i = 1; i <= 20; i++) arr[i] = i;
for (int i = 0; i < 1; i++) {
int a, b;
cin >> a >> b;
while (a < b) {
int tmp = arr[a];
arr[a++] = arr[b];
arr[b--] = tmp;
}
}
for (int i = 1; i <= 20; i++) cout << arr[i] << ' ';
}
view raw 10804.cpp hosted with ❤ by GitHub
728x90

https://www.acmicpc.net/problem/2440

😢 단순 출력 문제

😊 row가 증가할 때마다 n-row만큼 줄어들도록 *을 출력하면 된다
row는 1개씩 증가하니
5를 입력받았을 경우
5-0, *****
5-1, ****
5-2, ***
5-3, **
5-4, *

#include <iostream>
using namespace std;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int n;
cin >> n;
for (int row = 0; row < n; row++) {
for (int col = 0; col < n - row; col++) cout << '*';
cout << '\n';
}
}
view raw 2440.cpp hosted with ❤ by GitHub
728x90

https://www.acmicpc.net/problem/2439

😢 단순 출력 문제(그래도 생각이 조금은 필요)

😊 *(별)은 row(행)의 수만큼 출력, 공백은 그 나머지 라는 생각을 통해 해결 가능

즉, 5를 입력받으면

1. 공백4개 별1개
2. 공백3개 별2개
3. 공백2개 별3개
4. 공백1개 별4개
5. 공백 0개 별5개

n을 입력받으면

for (int row = 0; row < n; row++) {
	for (int col = 0; col < n - row - 1; col++) cout << ' ';
    	for (int col = 0; col <= row; col++) cout << '*';
  	cout << '\n';
}

#include <iostream>
using namespace std;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int n;
cin >> n;
for (int row = 0; row < n; row++) {
for (int col = 0; col < n - row - 1; col++) cout << ' ';
for (int col = 0; col <= row; col++) cout << '*';
cout << '\n';
}
}
view raw 2439.cpp hosted with ❤ by GitHub

728x90

https://www.acmicpc.net/problem/2438

😢 단순 출력 문제

😊 1개부터 입력 받는 숫자만큼 1개씩 증가하는 별 출력

#include <iostream>
using namespace std;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int n;
cin >> n;
for (int row = 0; row < n; row++) {
for (int col = 0; col <= row; col++) {
cout << '*';
}
cout << '\n';
}
}
view raw 2438.cpp hosted with ❤ by GitHub

+ Recent posts