๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[์ด๊ฒ์ด ์ฝ๋ฉ ํ ์คํธ๋ค with Python]_13_๊ทธ๋ฆฌ๋ ์๊ณ ๋ฆฌ์ฆ ์ ํ ๋ฌธ์ ๋ณธ๋ฌธ
[์ด๊ฒ์ด ์ฝ๋ฉ ํ ์คํธ๋ค with Python]_13_๊ทธ๋ฆฌ๋ ์๊ณ ๋ฆฌ์ฆ ์ ํ ๋ฌธ์
์ง์ง์ํ์นด 2022. 1. 31. 20:37220131 ์์ฑ
<๋ณธ ๋ธ๋ก๊ทธ๋ ใ์ด๊ฒ์ด ์ทจ์ ์ ์ํ ์ฝ๋ฉ ํ ์คํธ๋คใ ์ youtube๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค>
https://www.youtube.com/watch?v=_TG0hVYJ6D8&list=PLVsNizTWUw7H9_of5YCB0FmsSc-K44y81&index=13
< ๋ฌธ์ 1 > 1์ผ ๋ ๋๊น์ง
: ์ด๋ ํ ์ N์ด 1์ด ๋ ๋๊น์ง ๋ค์์ ๋ ๊ณผ์ ์ค ํ๋๋ฅผ ๋ฐ๋ณต์ ์ผ๋ก ์ ํํ์ฌ ์ํ
: ๋๋ฒ ์งธ ์ฐ์ฐ์ N์ด K๋ก ๋๋์ด ๋จ์ด์ง ๋๋ง ์ ํ
1) N์์ 1 ๋นผ๊ธฐ
2) N์ K๋ก ๋๋๊ธฐ
: N, K ์ฃผ์ด์ง ๋ N์ด 1์ด ๋ ๋๊น์ง 1 or 2 ๋ฒ์ ๊ณผ์ ์ ์ํํด์ผํ๋ ์ต์ ํ์
- ์ฃผ์ด์ง N์ ๋ํด ์ต๋ํ ๋ง์ด ๋๋๊ธฐ
- 1 ๋ฒ๋ณด๋ค 2๋ฒ์ด ์์ ์ ๋ง์ด ์ค์
- python
n, k = map(int, input().split())
cnt = 0
while True :
target = ( n // k ) * k # k๋ก ๋๋์ด์ง๋น
cnt += (n-target)
n = target
if n < k :
break
cnt += 1
n //= k
cnt += (n-1)
print(cnt)
- c++
#include <bits/stdc++.h>
using namespace std;
int n, k;
int cnt = 0;
int main(void) {
cin >> n >> k;
while (true) {
int target = ( n // k ) * k # k๋ก ๋๋์ด์ง๋น
cnt += (n-target)
n = target
if n < k ;
break
cnt ++;
n /= k;
}
cnt += (n-1);
cout << cnt << '\n';
}
- java
import javna.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int cnt = 0;
while(true) {
int target = (n/k) * k;
cnt += (n - target);
n = target;
if ( n < k )
break;
cnt += 1;
n /= k;
}
cnt += (n-1);
System.out.println(cnt);
}
}
< ๋ฌธ์ 2 > ๊ณฑํ๊ธฐ or ๋ํ๊ธฐ
: ๊ฐ ์ซ์๊ฐ 0~9 ๋ก ๋ ๋ฌธ์์ด S ๊ฐ ์ฃผ์ด์ง ๋, ์ผ์ชฝ๋ถํฐ ์ค๋ฅธ์ชฝ์ผ๋ก ํ๋์ฉ ๋ชจ๋ ์ซ์๋ฅผ ํ์ธํ๋ฉฐ ์ซ์ ์ฌ์ด์ X or + ์ฐ์ฐ์๋ฅผ ๋ฃ์ด
๊ฒฐ๊ณผ์ ์ผ๋ก ๋ง๋ค์ด์ง ์ ์๋ ๊ฐ์ฅ ํฐ ์ ๊ตฌํ๊ธฐ
- + ๋ณด๋ค๋ X ๊ฐ ๋ ํฌ๊ฒ
- 1 ์ดํ๋ฉด ๋ํ๊ธฐ, 2 ์ด์์ด๋ฉด ๊ณฑํ๊ธฐ
- python
data = input()
result = int(data[0])
for i in range(1, len(data)) :
num = int(data[i])
if num <= 1 or result <= 1 :
result += num
else :
result *= num
print(result)
- c++
# include <bits/stdc++.h>
using namespace std;
string str;
int main(void) {
cin >> str;
long long result = str[0] - '0';
for (int i = 1; i<str.size(); i++); {
int num = str[i] - '0';
if (num <= 1 || result <= 1)
result += num;
else :
result *= num;
}
cout << result << '\n';
}
- java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in)l
String str = sc.next();
long result = str.charAt(0) - '0';
for (int i = 1; i < str.length(); i++) {
int num = str.charAt(i) - '0';
if (num <= 1 || result <= 1) {
result += num;
else :
result *= num;
}
}
System.out.println(result);
}
}
< ๋ฌธ์ 3 > ๋ชจํ๊ฐ ๊ธธ๋
: ๊ณตํฌ๋๊ฐ X์ธ ๋ชจํ๊ฐ๋ ๋ฐ๋์ X๋ช ์ด์์ผ๋ก ๊ตฌ์ฑํ ๋ชจํ๊ฐ ๊ทธ๋ฃน์ ์ฐธ์ฌ
: N๋ช ์ ๋ชจํ๊ฐ์ ๋ํ ์ ๋ณด๊ฐ ์ฃผ์ด์ก์ ๋, ์ฌํ์ ๋ ๋ ์ ์๋ ๊ทธ๋ฃน์ ์ต๋๊ฐ
- ์ค๋ฆ์ฐจ์ ์ ๋ ฌ ์ดํ ๊ณตํฌ๋๊ฐ ๊ฐ์ฅ ๋ฎ์ ๋ชจํ๊ฐ๋ถํฐ ํ์ธ
- ํ์ฌ ๊ทธ๋ฃน ์๊ฐ ํ์ฌ ํ์ธํ๊ณ ์๋ ๊ณตํฌ๋๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ผ๋ฉด ์ค์
- python
n = int(input())
data = list(map(int, input().split()))
data.sort()
result = 0 # ์ด ๊ทธ๋ฃน์
count = 0 # ํ์ฌ ๊ทธ๋ฃน ๋ชจํ๊ฐ ์
for i in data :
count += 1
if count >= i : # ํ์ฌ ๊ทธ๋ฃน ๋ชจํ๊ฐ ์๊ฐ ํ์ฌ ๊ณตํฌ๋ ์ด์์ด๋ฉด ๊ทธ๋ฃน ใฑ
result += 1
count = 0 # ์ด๊ธฐํ
print(result)
- c++
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> arr;
int main(void) {
cin >> n;
for (int i = 0; i < n; i++ ) {
int x;
cin >> x;
arr.push_back(x);
}
sort(arr.begin(), arr.end());
int result = 0;
int cnt = 0
for (int i = 0; i < n; i++ ) {
cnt += 1;
if (cnt >= arr[i] ) {
result += 1;
cnt = 0;
}
}
cout << result << '\n';
}
- java
import java.util.*;
public class Main {
public static int n;
public static ArrayList<Integer> arrayList = new ArrayList<>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int i =0; i< n; i++) {
arryList.add(sc.nextInt());
}
Collections.sort(arrayList);
int result = 0;
int count = 0;
for (int i = 0; i<n; i++) {
count += 1;
if (count >= arrayList.get(i)) {
resut += 1;
count = 0;
}
}
System.out.printIn(result);
}
}