티스토리 뷰

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;
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
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
글 보관함