algorithm
[c++] 석차 구하기
tonirr
2020. 12. 8. 23:44
- 석차 구하기
#include <iostream>
#include <vector>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
//freopen("input.txt", "rt", stdin);
int n, i, j;
scanf("%d", &n);
std::vector <int> a(n), b(n);
for(i=0; i<n; i++){
scanf("%d", &a[i]);
b[i]=1;
}
for(i=0; i<n; i++){
for(j=i+1; j<n; j++){
if(a[i] < a[j]) b[i]++;
else if(a[i] > a[j]) b[j]++;
}
}
for(i=0; i<n; i++){
printf("%d ", b[i]);
}
return 0;
}
- 마라톤
#include <iostream>
#include <vector>
#include <algorithm>
//내가 짠것
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
//freopen("input.txt", "rt", stdin);
int n, i, j;
scanf("%d", &n);
std::vector <int> a(n), b(n);
for(i=0; i<n; i++){
scanf("%d", &a[i]);
b[i]=1;
}
for(i=n-1; i>=0; i--){
for(j=0; j<i; j++){
if(a[i] < a[j]) b[i]++;
}
}
for(i=0; i<n; i++){
printf("%d ", b[i]);
}
return 0;
}
// 참고한것
#include <iostream>
#include <vector>
#include <algorithm>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
//freopen("input.txt", "rt", stdin);
int n, i, j, cnt=1;
scanf("%d", &n);
std::vector <int> a(n);
for(i=0; i<n; i++){
scanf("%d", &a[i]);
}
printf("1 ");
for(i=1; i<n; i++){
cnt=1;
for(j=i-1; j>=0; j--){
if(a[i] < a[j]) cnt++;
}
printf("%d ", cnt);
}
return 0;
}