NOIP 2006 普及组 明明的随机数

冒泡排序
#include <bits/stdc++.h>
using namespace std;

const int N = 109;
int n, m, a[N], b[N];

int main() {
	cin >> n;
	for (int i = 1; i <= n; i ++) cin >> a[i];
	
	for (int i = n - 1; i > 0; i --) {
		for (int j = 1; j <= i; j ++) {
			if (a[j] > a[j + 1]) {
				swap(a[j], a[j + 1]);
			}
		}
	}
	
	b[1] = a[1];
	m = 1;
	for (int i = 2; i <= n; i ++) {
		while (a[i] == b[m]) i ++;
		b[++ m] = a[i];
	}

	cout << m << endl;
	for (int i = 1; i <= m; i ++) cout << b[i] << ' ';
	
	return 0;
}
选择排序
#include <bits/stdc++.h>
using namespace std;

const int N = 109;
int n, m, a[N], b[N];

int main() {
	cin >> n;
	for (int i = 1; i <= n; i ++) cin >> a[i];
	
	for (int i = n; i > 1; i --) {
		int t = 1;
		for (int j = 2; j <= i; j ++) {
			if (a[j] > a[t]) {
				t = j;
			}
		}
		swap(a[t], a[i]);
	}
	
	b[1] = a[1];
	m = 1;
	for (int i = 2; i <= n; i ++) {
		while (a[i] == b[m]) i ++;
		b[++ m] = a[i];
	}

	cout << m << endl;
	for (int i = 1; i <= m; i ++) cout << b[i] << ' ';
	
	return 0;
}
插入排序
#include <bits/stdc++.h>
using namespace std;

const int N = 109;
int n, m, a[N], b[N];

int main() {
	cin >> n;
	for (int i = 1; i <= n; i ++) {
		cin >> a[i];
		int j = i;
		while (j > 1 && a[j] < a[j - 1]) {
			swap(a[j], a[j - 1]);
			j --;
		}
	}	
	
	b[1] = a[1];
	m = 1;
	for (int i = 2; i <= n; i ++) {
		while (a[i] == b[m]) i ++;
		b[++ m] = a[i];
	}

	cout << m << endl;
	for (int i = 1; i <= m; i ++) cout << b[i] << ' ';
	
	return 0;
}
计数排序
#include <bits/stdc++.h>
using namespace std;

const int N = 1009;
int n, m, x, a[N];

int main() {
	cin >> n;
	for (int i = 1; i <= n; i ++) {
		cin >> x;
		if (!a[x]) {
			m ++;
			a[x] ++;
		}
	}	
	
	cout << m << endl;
	for (int i = 1; i <= 1000; i ++) {
		if (a[i]) {
			cout << i << ' ';
		}
	}
	
	return 0;
}
归并排序
#include <bits/stdc++.h>
using namespace std;

const int N = 109;
int n, m, a[N], b[N];

void mergesort(int l, int r) {
	if (l == r) return;
	
	int mid = l + r >> 1;
	mergesort(l, mid);
	mergesort(mid + 1, r);
	
	int i = l, j = mid + 1, k = l;
	while (i <= mid && j <= r) {
		if (a[i] <= a[j]) {
			b[k ++] = a[i ++];
		}
		else {
			b[k ++] = a[j ++];
		}
	}
	
	while (i <= mid) {
		b[k ++] = a[i ++];
	}
	while (j <= r) {
		b[k ++] = a[j ++];
	}
	
	for (i = l; i <= r; i ++) a[i] = b[i];
}

int main() {
	cin >> n;
	for (int i = 1; i <= n; i ++) cin >> a[i];
	
	mergesort(1, n);
	
	b[1] = a[1];
	m = 1;
	for (int i = 2; i <= n; i ++) {
		while (a[i] == b[m]) i ++;
		b[++ m] = a[i];
	}

	cout << m << endl;
	for (int i = 1; i <= m; i ++) cout << b[i] << ' ';
	
	return 0;
}
快速排序
#include <bits/stdc++.h>
using namespace std;

const int N = 109;
int n, m, a[N], b[N];

void quicksort(int l, int r) {
	if (l == r) return;
	
	int x = a[l + r >> 1];	
	int i = l - 1, j = r + 1;
	
	while (i < j)	 {
		do {
			i ++;
		}while (a[i] < x);
		
		do {
			j --;
		}while (a[j] > x);
		
		if (i < j) {
			swap(a[i], a[j]);
		}
	}
	
	quicksort(l, j);
	quicksort(j + 1, r);
}

int main() {
	cin >> n;
	for (int i = 1; i <= n; i ++) cin >> a[i];
	
	quicksort(1, n);
	
	b[1] = a[1];
	m = 1;
	for (int i = 2; i <= n; i ++) {
		while (a[i] == b[m]) i ++;
		b[++ m] = a[i];
	}

	cout << m << endl;
	for (int i = 1; i <= m; i ++) cout << b[i] << ' ';
	
	return 0;
}
堆排序
#include <bits/stdc++.h>
using namespace std;

const int N = 109;
int n, m, a[N], b[N], cnt;

void down(int u) {	//大根堆 
	int maxx = u;
	
	if (u * 2 <= cnt && a[u * 2] > a[maxx]) maxx = 2 * u;
	if (u * 2 + 1 <= cnt && a[u * 2 + 1] > a[maxx]) maxx = 2 * u + 1;
	
	if (maxx != u) {
		swap(a[maxx], a[u]);
		down(maxx);
	}
}

int main() {
	cin >> n;
	for (int i = 1; i <= n; i ++) cin >> a[i];
	
	cnt = n;
	for (int i = n / 2; i > 0; i --) down(i);
	
	for (int i = 1; i <= n; i ++) {
		swap(a[1], a[cnt --]);
		down(1);
	}
	
	b[1] = a[1];
	m = 1;
	for (int i = 2; i <= n; i ++) {
		while (a[i] == b[m]) i ++;
		b[++ m] = a[i];
	}

	cout << m << endl;
	for (int i = 1; i <= m; i ++) cout << b[i] << ' ';
	
	return 0;
}
二叉排序树
#include <bits/stdc++.h>
using namespace std;

const int N = 109;
int n, x, idx, root;

struct BST {
	int l, r, val;
}tr[N];

void insert(int& u, int x) {
	if (!u) {
		u = ++ idx;
		tr[u].val = x;
		return;
	}
	
	if (x > tr[u].val) insert(tr[u].r, x);
	else if (x < tr[u].val) insert(tr[u].l, x);
}

void inorder(int u) {
	if (!u) return;
	
	inorder(tr[u].l);
	cout << tr[u].val << ' ';
	inorder(tr[u].r);
}

int main() {
	cin >> n;
	for (int i = 1; i <= n; i ++) {
		cin >> x;
		insert(root, x);
	}

	cout << idx << endl;
	inorder(root);
	
	return 0;
}
字典树
#include <bits/stdc++.h>
using namespace std;

const int N = 109;
int n, m, x, idx = 1, son[1100][2], h[1100];

void insert(int x) {
	int u = 1;
	
	for (int i = 9; i >= 0; i --) {
		int j = x >> i & 1;
		if (!son[u][j]) son[u][j] = ++ idx;
		u = son[u][j];
	}
	if (!h[u]) {
		m ++;
		h[u] = x;
	}
}

void dfs(int u) {
	if (h[u]) {
		cout << h[u] << ' ';
		return;
	}
	
	if (son[u][0]) dfs(son[u][0]);
	if (son[u][1]) dfs(son[u][1]);
}

int main() {
	cin >> n;
	for (int i = 1; i <= n; i ++) {
		cin >> x;
		insert(x);
	}

	cout << m << endl;
	dfs(1);
	
	return 0;
}
set
#include <bits/stdc++.h>
using namespace std;

const int N = 109;
int n, x;

set <int> s; 
set <int> :: iterator it;

int main() {
	cin >> n;
	for (int i = 1; i <= n; i ++) {
		cin >> x;
		s.insert(x);
	}

	cout << s.size() << endl;
	for (it = s.begin(); it != s.end(); it ++) {
		cout << *it << ' ';
	}
	
	return 0;
}
sort + unique
#include <bits/stdc++.h>
using namespace std;

const int N = 109;
int n, m, a[N];

int main() {
	cin >> n;
	for (int i = 1; i <= n; i ++) cin >> a[i];
	
	sort(a + 1, a + 1 + n);
	m = unique(a + 1, a + 1 + n) - &a[1];
	
	cout << m << endl;
	for (int i = 1; i <= m; i ++) {
		cout << a[i] << ' ';
	}
	
	return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值