티스토리 뷰

  • Least Recently Used
#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main() {
	//freopen("input.txt", "rt", stdin);
	int s, n, i, j, pos;
	int c[10], a[1000];
	scanf("%d", &s);
	scanf("%d", &n);
	
	for(i=0; i<n; i++){
		scanf("%d", &a[i]);
	}
	
	for(i=0; i<n; i++){
		pos = -1;
		for(j=0; j<s; j++){
			if(a[i]==c[j]) pos = j;
		}
		
		if(pos==-1){
			for(j=s-1; j>=0; j--){
				c[j+1]=c[j];
			}
			
		} else {
			for(j=pos-1; j>=0; j--){
				c[j+1]=c[j];
			}
			
		}
		c[0]=a[i];
	}

	for(i=0; i<s; i++){
		printf("%d ", c[i]);
	}
	return 0;
}

 

  • Inversion Sequence
#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main() {
	//freopen("input.txt", "rt", stdin);
	int n, i, j;
	int a[100], b[100];
	scanf("%d", &n);
	
	for(i=1; i<=n; i++){
		scanf("%d", &a[i]);
	}
	
	for(i=n; i>=1; i--){
		for(j=1; j<=a[i]; j++){
			b[i+j-1]=b[i+j];
		}
		b[i+j-1] = i;
	}

	for(i=1; i<=n; i++){
		printf("%d ", b[i]);
	}
	return 0;
}

'algorithm' 카테고리의 다른 글

[c++] 교집합, 연속된 자연수  (0) 2020.12.24
[c++] 두배열합치기  (0) 2020.12.23
[c++] 삽입정렬, Special sort  (0) 2020.12.15
[c++] 3등의 성적은, 버블정렬  (0) 2020.12.14
[c++] 탄화수소 질량, 선택정렬  (0) 2020.12.13
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
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
글 보관함