Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 선형판별분석
- 로지스틱 회귀
- Numerical optimization
- directed graphical model
- secant
- chapter01
- chapter02
- 이것이 MySQL이다
- SCPC
- Perceptron Convergence theorem
- 5397번
- 1차예선
- CH01
- 알고리즘대회
- 알고리즘
- undirected graphical model
- 근구하기
- 2018
- bisection
- MySQL
- graphical models
- 개발순서
- 인공지능
- 자바ORM표준JPA프로그래밍
- vector미분
- 스터디
- 델타 rule
- 선형분류
- Fisher discriminant analysis
- falsePosition
Archives
- Today
- Total
computer_study
[탐색] BAEKJOON '1302'번 '베스트셀러'문제 (C++/python) 본문
문제 : www.acmicpc.net/problem/1302
1302번: 베스트셀러
첫째 줄에 오늘 하루 동안 팔린 책의 개수 N이 주어진다. 이 값은 1,000보다 작거나 같은 자연수이다. 둘째부터 N개의 줄에 책의 제목이 입력으로 들어온다. 책의 제목의 길이는 50보다 작거나 같고
www.acmicpc.net
-
문제 해결 아이디어
1. python에선 dictionary, c++에선 map함수를 이용하여, key값과 value값을 설정하고 문제를 해결한다.
2. 입력값들을 순회하며, 같은 값이 나올 때 마다 value값을 증가시켜, 마지막에 그중 최대값을 출력한다.
-
python
n = int(input())
books = {}
for _ in range(n):
book = input()
if book not in books:
books[book] = 1
else:
books[book] += 1
target = max(books.values())
array = []
for book, number in books.items():
if number == target:
array.append(book)
print(sorted(array)[0])
※ dictionary
-
c++
#include "iostream"
#include "string"
#include "map"
using namespace std;
int main(){
int n;
map<string, int> books;
scanf("%d",&n);
for(int i=0 ; i< n ; i++){
string tmp;
cin >> tmp;
if(books.count(tmp) == 0){
books.insert(make_pair(tmp,1));
}
else{
books.find(tmp)->second++;
}
}
int max = 0;
for(auto iter = books.begin(); iter != books.end(); iter++){
if(iter->second > max)
max = iter->second;
}
for(auto iter = books.begin(); iter != books.end(); iter++){
if(iter->second == max){
cout << iter->first << "\n";
return 0;
}
}
return 0;
}
※ map
'알고리즘 > 문제풀이' 카테고리의 다른 글
[c++] codeground 연습문제'우주정거장' (2018년도 SCPC 예선1차) (0) | 2020.08.17 |
---|---|
[c++] codeground 연습문제 '버스 타기'(2018년도 SCPC 예선1차) (0) | 2020.08.12 |
[탐색] BAEKJOON '1568'번 '새'문제 (C++/python) (0) | 2020.08.11 |
[탐색] BAEKJOON '1543'번 '문서 검색'문제 (C++/python) (0) | 2020.08.11 |
[재귀함수] BAEKJOON ' 7490 '번 '0 만들기 '문제 (C++/python) (0) | 2020.08.11 |
Comments