|
发表于 2019-4-1 17:35:27
|
显示全部楼层
不用循环遍历,很难做到。不过循环遍历,要看是别人做,还是你做。如果别人做也算,那就推荐你用排序,算你不用循环遍历,是别人用的。其本质核心也要循环遍历。例程如下:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <Windows.h>
using namespace std;
int comp(const void *a, const void *b)
{
return *(char *)a - *(char *)b;
}
int main(void) {
char a[] = { 1,1,1,1,0,1,1,1,0,1,1,1,1,0 };
int len = sizeof(a) / sizeof(a[0]);
cout << " a=";
for (int i = 0;i < len;i++) cout << (int)a[i] << " ";
cout << endl;
qsort(a, len, sizeof(char), comp);
cout << "sort a=" ;
for (int i = 0;i < len;i++) cout << (int)a[i] << " ";
cout << endl;
return 0;
}
运行结果:
a=1 1 1 1 0 1 1 1 0 1 1 1 1 0
sort a=0 0 0 1 1 1 1 1 1 1 1 1 1 1
请按任意键继续. . .
就不要找了,每次qsort一次,0都冒到顶上取了,从0下标开始取。要懒就懒到底。

|
评分
-
查看全部评分
|