백준 2751 수 정렬하기 2
//중복된 값 제거
정렬하고 중복된 값들을 뒤로보내고 뒤에 있는애들 지워버리는 컨셉
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()),vec.end());
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> vec;
int main(){
int N;
cin >> N;
for(int i=0; i<N; i++){
int tmp;
cin >> tmp;
vec.push_back(tmp);
}
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()),vec.end());
for(int i=0; i<vec.size(); i++){
cout << vec[i]<<"\n"; //<<endl; 하면 시간초과 떠서 실패 뜸
}
}
백준 10989 수 정렬하기 3
//메모리제한과 시간제한
sort함수를 안쓰고 아래 세줄 추가해서 시간 줄여주기
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#include <iostream>
#include <algorithm>
#include <vector>
#define MAX_SIZE 10000+1
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N;
cin >> N;
int arr[MAX_SIZE] ={0};
for(int i=0; i<N; i++){
int tmp;
cin >> tmp;
arr[tmp]+=1;
}
for(int i=0; i<MAX_SIZE; i++){
for(int j=0; j<arr[i]; j++){
cout << i <<"\n";
}
}
}
백준 2108 통계학
산술평균, 중앙값, 최빈값, 범위 구하기
디테일한 부분 고치는게 오래걸림
1. 반올림 (-0이라는것도 뜨더라,,)
2. 두번째 큰거라 쉽게하려고 걍 sort()해서 vector(1) 하려했더니 인덱스 에러 떠서 조건 추가하느라 빙빙 돌아옴 ㅠ
최빈값 하는거 다른방법 있었던거같은데 뭐지 그냥 있었는데 이렇게 어려운거 말고
#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#define MAX_SIZE 8000+1
using namespace std;
int arr[MAX_SIZE] = {0};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
vector<int> vec;
int N;
double sum=0;
cin >> N;
for(int i=0; i<N; i++){
int tmp;
cin >> tmp;
sum += tmp;
vec.push_back(tmp);
if(tmp>=0){
arr[tmp]+=1;
}else{
arr[tmp*(-1)+4000]+=1;
}
}
sum /= N;
sum >0 ? sum +=0.5 : sum -=0.5;
//산술평균값 출력
cout << (int)sum <<"\n";
//중앙값 출력
sort(vec.begin(), vec.end());
cout << vec[(N/2)] <<"\n";
int max = 0;
//최빈값 출력
for(int i=0; i<MAX_SIZE; i++){
if(max<arr[i]){
max = arr[i];
}
}
vector<int> v;
int cnt=0;
for(int i=0; i<MAX_SIZE; i++){
if(max==arr[i]){
if(i>4000){
v.push_back(i*(-1)+4000);
cnt++;
}else{
v.push_back(i);
cnt++;
}
}
}
sort(v.begin(), v.end());
// cout <<v[1] <<"\n";
if(cnt>1){
cout <<v[1]<<"\n";
}else{
cout <<v[0]<<"\n";
}
//범위 출력
cout << vec.back()-vec.front() << "\n";
}
'코딩테스트 대비' 카테고리의 다른 글
[프로그래머스 c++] 최소직사각형 (0) | 2022.08.22 |
---|---|
[프로그래머스 c++] 같은 숫자는 싫어 (1) | 2022.08.22 |
[프로그래머스 c++] 구명보트 (0) | 2022.08.22 |
[c++] 소수점 자릿수 출력하기 / [Softeer] 성적평균 문제 풀이 (0) | 2022.06.10 |
평범한 배낭 문제 (0) | 2022.05.12 |