华为机试2题:排序并剔除连续的数字只保留最大最小数

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>


int main(){
	int a[10];
	int n = 10,low=0,high=1;
	for (int i = 0; i < 10; i++){
		scanf("%d,", &a[i]);
	}
		int i, j, k, temp;
		for (i = 0; i<n - 1; i++){
			k = i;
			for (j = i + 1; j<n; j++){
				if (a[j]>a[k]) {
					temp = a[k];
					a[k] = a[j];
					a[j] = temp;
				}
			}
		}
		for (i = 0; i < n-1; i++){
			printf("%d,",a[i]);
		}
		printf("%d", a[n - 1]);
		printf("\n");
		while (low < n){
			while (a[low] != a[low + 1] + 1) {
				printf("%d ", a[low]); low++;
			}
			printf("%d,", a[low]);
			high = low;
			while (a[high] == a[high + 1] + 1) high++;
			if (high != (n - 1)) 
				printf("%d,", a[high]);
			else printf("%d", a[high]);
			low = high + 1;
		}
	system("pause");
	return 0;
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页