-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
6-g0rnn #23
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ ์ ์๊ธฐ ์ ์ ๋นจ๋ฆฌ ๋ฆฌ๋ทฐํ๊ณ ์์ผ๊ฒ ๋ค๋ ์๊ฐ์ด์๋๋ฐ์....
์ต๋น๊ฐ์ด ๊ฐ์ง๋ง๋ผ๊ณ ํ๋ค์...
์ ๋ ์ถ๊ฐ ๋ฐฐ์ด์ ์์ฐ๊ณ ์ถ์์ง๋ง ๊ฒฐ๊ตญ ์ผ์ต๋๋ค.
์ด์ง ๋จธ๋ฆฌ๋ฅผ ๊ตด๋ ค์ {count, index} ๋ก ์ ์ฅ์ ํ์ต๋๋ค.
์ด๋ ๊ฒ ํ๋ฉด ๋น๊ต๋ count๋ก, ๋์ค์ return๊ฐ์ index๋ก ํด์ ๋ต์ ์ฝ๊ฒ ์ฐพ์ ์ ์์ด์.
๊ทธ๋ฆฌ๊ณ ์ด์ง ์ด๋ ค์์ด ์์๋๊ฑด compareTo๋ฅผ ์ ์ํ๋ ๋ถ๋ถ์ธ๋ฐ์.
๋ก์ง์ ๋จธ๋ฆฌ์ ์์์ง๋ง ํ์์ ์ ์จ๋ณด์ง ์์์ ์งํผํฐ ์ฐฌ์ค ์ผ์ต๋๋ค.
๋ค์์๋ ๊ฒ์ ์์ด ์จ๋จน์ ์ ์์ ๊ฒ ๊ฐ๋ค์.
๋ ์์์์ ๋ฐ์ฌ๋ฆผํ๋ ์ถ๊ฐ์ ์ธ ๋ก์ง์ด ํ์ํ ์ค ์์๋๋ฐ ๊ทธ๋ฅ round ํ๋ฉด ๋๋ค์.
๊ณ ์ํ์ จ์ต๋๋ค.
CPP CODE
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int N;
bool compareTo(pair<int, int> a, pair<int, int> b) {
if (a.first == b.first) return a.second < b.second;
return a.first > b.first;
}
int solution1(vector<int> s) {
int sum = 0;
for (int i : s) sum += i;
float average = (float) sum / N;
return round(average);
}
int solution2(vector<int> s) {
return s[N / 2];
}
int solution3(vector<int> s) {
vector<pair<int, int>> temp(8001, {0, 0});
for (int i : s) {
temp[i + 4000].first++;
temp[i + 4000].second = i;
}
sort(temp.begin(), temp.end(), compareTo);
if (temp[0].first == temp[1].first) return temp[1].second;
return temp[0].second;
}
int solution4(vector<int> s) {
return s[N - 1] - s[0];
}
int main() {
cin >> N;
vector<int> s(N);
for (int i = 0; i < N; i++) cin >> s[i];
sort(s.begin(), s.end());
cout << solution1(s) << endl;
cout << solution2(s) << endl;
cout << solution3(s) << endl;
cout << solution4(s) << endl;
return 0;
}
if (arr[i] == arr[i + 1]) | ||
{ | ||
tmp++; | ||
} | ||
else | ||
{ | ||
tmp += 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฒ์์ tmp๋ฅผ arr[0]๋ก ์ด๊ธฐํ ํ๋ฉด, for๋ฌธ์ 1๋ถํฐ ๋๋ฆด ์ ์์ ๊ฒ ๊ฐ์์.
๋ฐ๋ผ์ arr[i] == arr[i-1]
๋ก ๋น๊ตํ ์ ์๊ณ , else์ธ ์ํฉ์์ tmp+=1 ์ ์ํด๋ ๋์ ๊ฒ ๊ฐ์์.
(์ ํํ๊ฑด ๋ค์ ๋ก์ง๋ ์ดํดํด์ผ ํ์ง๋ง.. ์กฐ๊ธ ํจ์คํ๊ฒ ์ต๋๋ค ใ
ใ
)
if์ else๋ชจ๋ tmp๋ฅผ ์ฆ๊ฐ์ํค๋๊น ์ด~์ง ๋ ๋ณต์กํด์ง๋ฏ ํฉ๋๋ค.
๊ตฌํ ์์ ๋นก์ธ๋ค์...
์ ๋ ๋ฐฐ์ด ๋ง๋๋๊ฑด ์ ํ์
์ด ์๋์ด์ ๊ท ํธ๋์ฒ๋ผ ์๋ํด๋ดค๋๋ฐ,
์๊พธ ๊ณ ๋ คํด์ผํ ์ฌํญ๋ค์ด ์๊ธฐ๊ธธ๋ ํฌ๊ธฐํ์ต๋๋ค.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
float๊ณผ double ๋ชจ๋ ๋ถ๋์์์ ์ผ๋ฅด ์ฌ์ฉํ์ฌ -0์ด ์กด์ฌํ๊ณ ์ฝ์์ 0์ด ์๋๋ผ -0์ผ๋ก ์ถ๋ ฅ๋ฉ๋๋ค.
ํ์ง๋ง solution1์ ๋ฐํ ๊ฐ์ด intํ์ด์ด์ ์๋ ํ๋ณํ์ผ๋ก ์ธํด 0์ผ๋ก ๋ณํ๋์ด ์ถ๋ ฅ๋๊ฒ ๊ฐ์ต๋๋ค. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๊ทธ๋ ๊ตฐ์. -0์ด ์๋ ๊ฑธ ๋ชจ๋ฅด๊ณ ์์๋ค์.
๊ทธ๋ฆฌ๊ณ ํจ์ ๋ฐํ ํ์
๋ ๋ฌด์ํ๊ณ ์์๋ค์. ..๊ฐ์ฌํฉ๋๋ค~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ์ด์ฌ์์๋ collections.Counter๋ฅผ ํ์ฉํด ์ต๋น๊ฐ์ ๊ตฌํ๋ ๊ฒ์ด ๋งค์ฐ ํธ๋ฆฌํ์ต๋๋ค. C++์์ 8000 ํฌ๊ธฐ์ ๋ฐฐ์ด์ ์ง์ ์์ฑํ๋ ๊ฒ์ฒ๋ผ ๊ณ ์ ๋ ๋ฐฐ์ด์ ์ฌ์ฉํ๋ ๋์ , ํ์ด์ฌ์ ๋ฐ์ดํฐ์ ๋ฒ์๋ฅผ ๋ชฐ๋ผ๋ ๋์ ์ผ๋ก ์ฒ๋ฆฌํ ์ ์๋ ์ฅ์ ์ด ์์ต๋๋ค. ์ ๋ ์ด ์ ์ ํ์ฉํด ํจ์จ์ ์ผ๋ก ์ต๋น๊ฐ์ ๊ณ์ฐํ์ต๋๋ค.
from collections import Counter
import sys
input = sys.stdin.read
data = input().split()
N = int(data[0])
numbers = list(map(int, data[1:]))
# 1. ์ฐ์ ํ๊ท
mean = round(sum(numbers) / N)
# 2. ์ค์๊ฐ
numbers.sort()
median = numbers[N // 2]
# 3. ์ต๋น๊ฐ
count = Counter(numbers)
most_common = count.most_common()
most_common.sort(key=lambda x: (-x[1], x[0])) # ๋น๋์ ์ ๋ ฌ ํ ๊ฐ ๊ธฐ์ค ์ ๋ ฌ
if len(most_common) > 1 and most_common[0][1] == most_common[1][1]:
mode = most_common[1][0] # ๋ ๋ฒ์งธ๋ก ์์ ์ต๋น๊ฐ
else:
mode = most_common[0][0] # ์ฒซ ๋ฒ์งธ ์ต๋น๊ฐ
# 4. ๋ฒ์
range_value = numbers[-1] - numbers[0]
# ๊ฒฐ๊ณผ ์ถ๋ ฅ
print(mean)
print(median)
print(mode)
print(range_value)
-
Counter์ ์ ์ฉ์ฑ
์ต๋น๊ฐ ๊ณ์ฐ์์ collections.Counter๊ฐ ๋งค์ฐ ์ ์ฉํ๋ค๊ณ ๋๊ผ์ต๋๋ค. C++์ฒ๋ผ ์ง์ ๋ฐฐ์ด์ ์์ฑํ๊ฑฐ๋ ๋ฐ๋ณต๋ฌธ์ ์ด์ฉํด ๋น๋๋ฅผ ์ธ๋ ๊ณผ์ ์์ด ๋จ์ํ Counter๋ฅผ ์ฌ์ฉํด ๋น ๋ฅด๊ฒ ๋น๋๋ฅผ ๊ณ์ฐํ ์ ์์์ต๋๋ค. -
๋ถ๋ ์์์ ์ฒ๋ฆฌ
ํ์ด์ฌ์์๋ round() ํจ์๊ฐ ์์๋ฅผ ์ฒ๋ฆฌํ ๋๋ ์ฌ๋ฐ๋ฅด๊ฒ ๋ฐ์ฌ๋ฆผ๋๋ฏ๋ก ๋ฐ๋ก ๋ถํธ๋ฅผ ํ์ธํ๊ฑฐ๋ ์ถ๊ฐ์ ์ธ ์์ธ ์ฒ๋ฆฌ๋ฅผ ํ์ง ์์๋ ๋์ต๋๋ค.
๐ ๋ฌธ์ ๋งํฌ
ํต๊ณํ
โ๏ธ ์์๋ ์๊ฐ
1h
โจ ์๋ ์ฝ๋
์ ๋ฒ๋ถํฐ ๊ตฌํ์ด ์ฝํ ๊ฒ ๊ฐ์. ๊ตฌํ์ ์ฐ์ตํด๋ณด๊ณ ์์ต๋๋ค. ๊ฐ๋จํด์ 10๋ถ๋ง์ ํ๊ณ ๋ค๋ฅธ ๋ฌธ์ ์ฌ๋ฆฌ๋ ค๊ณ ํ๋๋ฐ 1์๊ฐ์ด ๋์ด๊ฐ๊ฒ ๋์ด ๊ทธ๋ฅ ์ฌ๋ฆฌ๊ฒ ์ต๋๋ค.
ํ์ด์ฌ์์๋ ์ผ๋ง๋ ์ด๋ ค์ธ์ง ๋ชจ๋ฅด๊ฒ ์ง๋ง.. cpp์์๋ ๊ท์ฐฎ์๊ฒ ๋ช๊ฐ์ง ์์์ต๋๋ค.
์ ๋ ์ต๋น๊ฐ์ ๊ตฌํ๋๊ฑฐ์์ ์ด๋ ค์ ์ต๋๋ค. ๋๊ตฌ๋ ๊ทธ๋ฅ 8000ํฌ๊ธฐ์ ๋ฐฐ์ด์ ๋ง๋๋๋ฐ, ์ ๋ ๊ทธ๋ฐ ์คํ์ผ์ด ์๋๋ผ์ ํจ์๋ก ๊ตฌํํ๋๋ฐ ๊ทธ๊ฑฐ๋๋ฌธ์ ์๊ฐ์ ์ข ์ก์๋จน์์ต๋๋ค...ใ
๋๋จธ์ง๋ ์ฌ์ฐ๋ ์ต๋น๊ฐ์ ํจ์๋ก ๋ง๋ค์ด ๊ตฌํํ๋ ์๋์ฝ๋๋ง ์๊ฐํ ๊ฒ์
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
cpp์์๋ ๋ถ๋ ์์์ ์ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ -0์ด ์กด์ฌํ ์ ์๋ค๋ ๊ฒ์ ์๊ฒ๋์์ต๋๋ค. ๋ฐ๋ผ์ round๋ฅผ ์ฌ์ฉํ ๋ ์ฃผ์ํด์ผํฉ๋๋ค.