不用排序,直接上桶
查看原帖
不用排序,直接上桶
1099271
Retana楼主2024/4/18 17:17

但不够简洁

#include<iostream>
#include<cstdio>
#include<math.h>
using namespace std;
int a[105] = {};
int tong[1005] = {};
int main() {
	int n, ans = 0;
	scanf("%d", &n);//scanf和prinf刷时间
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);//先存下来
		tong[a[i]]++;//上桶
	}
	for (int i = 1; i <= 1000; i++) {
		if (tong[i] > 0) {//去重
			ans++;
		}
	}
	printf("%d\n", ans);
	for (int i = 1; i <= 1000; i++) {
		if (tong[i] > 0) {
			printf("%d ", i);//输出桶的序号就好
		}
	}
	return 0;
}
2024/4/18 17:17
加载中...