-
Notifications
You must be signed in to change notification settings - Fork 1
10-hadongun #40
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
10-hadongun #40
Conversation
9kyo-hwang
left a comment
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.
νμ΄μ¬μ κΈ°λ₯μ μ΅λν νμ©ν, 'νμ΄μ¬'λ΅κ² μμ±λ μ½λλ₯Ό 'νμ΄μ¨λνλ€'λΌκ³ λ§ν©λλ€.
νμ΄μ¬μ κ°κ²°νκ³ νΈλ¦¬νκ² μ½λλ₯Ό μμ±ν μ μλλ‘ λ€μν κΈ°λ₯μ μ 곡νλλ°μ, μ λ μ΅λν νμ΄μ¬μ κΈ°λ₯μ νμ©ν΄ κ°κ²°νκ² μ§λ κ²μ λͺ©νλ‘ ν΄λ΄€μ΅λλ€.
def solution(sequence):
N = len(sequence)
sequence.sort()
counts = Counter(sequence) # μ΄ν°λ¬λΈ(리μ€νΈ) λ΄ μμλ€μ κ°μλ₯Ό μΈμ΄ λμ
λ리 ννλ‘ μ μ₯ν΄μ£Όλ λΌμ΄λΈλ¬λ¦¬μ
λλ€.
avg = round(sum(sequence) / N) # sum ν¨μλ₯Ό μ¬μ©νλ©΄ μ΄ν°λ¬λΈμ μμ ν©μ μ½κ² ꡬν μ μμ΅λλ€
med = sequence[N // 2]
max_mode = max(counts.values()) # μμ κ°μλ₯Ό μ μ₯ν λμ
λ리μ values, μ¦ λΉλκ°λ€ μ€ κ°μ₯ ν° κ°(==μ΅λΉκ°)μ μ μ₯ν©λλ€
multimode = [k for k, v in counts.items() if v == max_mode] # λμ
λ리λ₯Ό μνν΄ μ΅λΉκ°κ³Ό λμΌν λΉλλ₯Ό κ°μ§ keyκ°, μ¦ μ΅λΉκ°μ ν΄λΉνλ μμλ€μ μ μ₯ν©λλ€
mode = multimode[1] if len(multimode) > 1 else multimode[0] # μ΅λΉκ°μ΄ μ¬λ¬ κ°μΌ κ²½μ° 2λ²μ§Έ κ°μ, μλλΌλ©΄ 1λ²μ§Έ κ°μ μ΅λΉκ°μΌλ‘ μ€μ ν©λλ€
rng = sequence[-1] - sequence[0] # 리μ€νΈ 맨 λ μμμ 첫 μμλ₯Ό λΉΌλ©΄ λ²μλ₯Ό ꡬν μ μμ΅λλ€
return (avg, med, mode, rng)
def main(input):
sequence = [int(input()) for _ in range(int(input()))]
print('\n'.join(map(str, solution(sequence))))
if __name__ == "__main__":
from collections import Counter
main(open(0).readline)μ°Έκ³ λ‘ Counterλ₯Ό μ¬μ©ν΄ μμλ€μ λΉλκ°μ μ μ₯νλ countsλ₯Ό μμ±νκΈ° μ μ, 리μ€νΈλ₯Ό μ λ ¬ν΄μ€¬κΈ° λλ¬Έμ countsλ μ λ ¬λ μμ μμλλ‘ κ°μ΄ λ€μ΄μμ΅λλ€.
첫 λ²μ§Έ ν
μ€νΈ μΌμ΄μ€λ₯Ό μλ‘ λ€μλ©΄
5
1
3
8
-2
2
μΌλ‘ μ£Όμ΄μ§λ―λ‘ countsλ λ€μκ³Ό κ°μ΄ ννλ©λλ€.
Counter({-2: 1, 1: 1, 2: 1, 3: 1, 8: 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.
μ¬μ보μλλ° μ΅λΉκ°μμ ν€λ§€κ² λλ€μ. λμ€λ μλμ½λλ³΄κ³ ν΄κ²°νμ΅λλ€! μΈλ±μ€λ₯Ό 8001κΉμ§ νλ€λ λ°μμ΄ μλ‘λ€μ. CμΈμ΄λ‘ μ΅λΉκ° λΆλΆλ§ ꡬνν΄λ΄€λλ° λ°μ μμ²΄κ° λμ€λκ³Ό λκ°μ κ² κ°μ΅λλ€
#define MAX 8001
for (int i = 0; i < N; i++) {
count[arr[i] + OFFSET]++;
}
int modeMax = 0;
int mode = 0;
int modeCount = 0;
for (int i = 0; i < MAX; i++) {
if (count[i] > modeMax) {
modeMax = count[i];
}
}```
|
μ λ νμ΄μ¬μ κΈ°λ₯μ νμ©ν΄μ λ¬Έμ λ₯Ό νμ΄λ΄€μ΅λλ€. from collections import Counter
import sys
input = sys.stdin.readline
n = int(input())
n_list = [int(input()) for _ in range(n)]
print(round(sum(n_list)/n))
print(sorted(n_list)[n//2])
c = Counter(n_list)
ms = [k for k, v in c.items() if v == max(c.values())]
ms.sort()
print(ms[1] if len(ms) > 1 else ms[0])
print(max(n_list)-min(n_list)) |
dohyeondol1
left a comment
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.
μ λ² νκΈ° λ ν΅κ³νμ λ°°μ μ΄μ κ·Έλλ§ μ½κ² ν μ μμμλλ€
μ λ λμ€λκ³Ό λ§μ°¬κ°μ§λ‘ λ°°μ΄μ λ΄κ³ μ
(μμ μΈλ±μ€λ λΆκ°λ₯νκ³ , μ μμ μ λκ°μ΄ 4000μ λμ§ μμΌλ―λ‘)
-4000 ~ 4000 μ λ΄μ μ μλ λ°°μ΄μ μ¬μ©νμ΅λλ€.
C++
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> num(N);
int sum = 0;
vector<int> count(8001, 0);
for(int i = 0; i < N; i++) {
cin >> num[i];
sum += num[i];
count[num[i] + 4000]++;
}
sort(num.begin(), num.end());
int arithmeticMean = round((double)sum / N);
int median = num[N / 2];
int maxCount = 0;
int mode = 0;
bool second = false;
for(int i = 0; i <= 8000; i++) {
if(count[i] > maxCount) {
maxCount = count[i];
mode = i - 4000;
second = false;
} else if(count[i] == maxCount && !second) {
mode = i - 4000;
second = true;
}
}
int range = num.back() - num.front();
cout << arithmeticMean << "\n";
cout << median << "\n";
cout << mode << "\n";
cout << range << "\n";
return 0;
}
νκΈλ‘ μ½λ©λΈλ‘μ μμ±ν΄λ μΈμ΄λ₯Ό μ§μ ν΄μ£Όλ©΄ μμ μ νμ£Όλ건 μ²μ μμμ΅λλ€..μμν ν κ°μ Έκ°μμ~~~
π λ¬Έμ λ§ν¬
https://www.acmicpc.net/problem/2108
βοΈ μμλ μκ°
2.5h
β¨ μλ μ½λ
λ¬Έμ μμ μ μν 쑰건 μ€ μ΅λΉκ°μ ꡬνλ λ¬Έμ μμ λ°μμ μ€λ μκ°μ΄ κ±Έλ Έμ΄μ...
μ λ ₯ λ°λ μ μμ λ²μκ° -4000<n<4000 μΌλ‘ μμλ ν¬ν¨λμ΄ μμ΄μ μ²μμ ꡬνν λ μΈλ±μ€λ₯Ό μ΄μ©ν΄μ ꡬνλ € νλλ°, μΈλ±μ€λ μμκ° μ λ€μ΄κ° μμ.. μμ λλ μ λμ νλ €λ€ μ€ν¨νμμ΅λλ€.-> μ§νΌν°μκ² μμ΄λμ΄λ₯Ό μ»μ΄λ΄€μ΅λλ€..!
μ²μ μλ₯Ό μ λ ₯λ°μ ν sort()λ₯Ό μ¬μ©ν΄ μ€λ¦μ°¨μ μ λ ¬ νμμ΅λλ€.
μ°μ νκ· μ κ° μλ₯Ό forλ¬ΈμΌλ‘ κ°κ° λν΄ λ¦¬μ€νΈμ ν¬κΈ°λ‘ λλμ΄ μ£Όμμ΅λλ€!.
μ 체 μμ κ°μκ° νμλΌκ³ λ¬Έμ μ μ£Όμ΄μ‘κΈ° λλ¬Έμ μ 체 ν¬κΈ°λ₯Ό μ μ μ°μ°μ //λ‘ λλ κ°μ΄ μ€μκ°μ΄ λ©λλ€(μμμ μ΄νλ λ²λ¦¬κΈ° λλ¬Έ)
κ°μ₯ λ¬Έμ μλ μ΅λΉκ°μΈλ°, μ°μ μ λ ₯ λ°μ μλ₯Ό μΈλ±μ€λ‘ κ·Έλλ‘ μ΄μ©νκΈ°λ‘ νμμ΅λλ€. λ€λ§ μ μμ λ²μκ° -4000
4000μ΄λ―λ‘8000μΈ count리μ€νΈλ₯Ό μμ±νμμ΅λλ€.μ λ ₯ λ°μ μ + 4000μ μΈλ±μ€ κ°μΌλ‘ νμ¬ 0
ex) 5λ₯Ό μ λ ₯ λ°μΌλ©΄ count[5]μ κ°μ΄ 1 μ¦κ°νλ μμΌλ‘ μΉ΄μ΄νΈ νμμ΅λλ€.
μ€κ°μ Max = count[0]μ΄λ temp = 0μ μ΄μ μ μλν λ μ°λ μ½λλΌ νμ μλ μ½λμΈλ° κΉλΉ‘νκ³ λͺ» μ§μ λ€μ.. 무μν΄μ£ΌμΈμ
μ΄ν maxν¨μλ₯Ό μ¬μ©ν΄ κ°μ₯ λ§μ΄ λ±μ₯ν νμλ₯Ό ꡬνμμ΅λλ€.
μ΄ν ifλ¬ΈμΌλ‘ Max κ°κ³Ό countμ κ°μ΄ κ°λ€λ©΄ κ·Έ κ°μ max_listμ μ μ₯νμμ΅λλ€.
μ΄λ κ² νμ¬ μ΅λΉκ°λ€μ max_listμ λͺ¨μ ν max_listμ ν¬κΈ°κ° 1μ΄λΌλ©΄ μ΅λΉκ°μ νλμ΄λ―λ‘ max_list[0]μ μΆλ ₯, max_listμ ν¬κΈ°κ° 1λ³΄λ€ ν¬λ€λ©΄ μ΅λΉκ°μ΄ μ¬λ¬κ°μ΄λ―λ‘ max_list[1]μ μΆλ ₯νλ μμΌλ‘ ν΄κ²°νμμ΅λλ€.
λ§μ§λ§ rangeλ μ λ ₯ λ°μ μκ° μ€λ¦μ°¨μ μ λ ¬λμ΄μκΈ° λλ¬Έμ μ μΌ ν° κ°- μ μΌ μμ κ°μ νμ¬ κ΅¬ν΄μ£Όμμ΅λλ€!!>.<
μ κ·Έλ¦¬κ³ 26λ²μ§Έ μ€μ count == Maxκ° μλλ° μλͺ»λμ΄μ..! count[k] == Maxκ° λ§λ€μ
μμ νκ³ μ¬λ¦¬λ©΄ μ λ²μ²λΌ μ΄μνκ² κΌ¬μΌ κ² κ°μμ.. μ¬κΈ° μ¨μ
Details
π μλ‘κ² μκ²λ λ΄μ©
round() ν¨μ -> λ°μ¬λ¦Ό ν΄μ€.
// μ°μ°μ -> μ μ λλμ (μμμ μ΄νλ λ€ λ²λ¦Ό)
from μ±μ§νΌν°