1.用于多组输入数据
int n;
int buf[100];
while(scanf("%d",&n) != EOF ){
for( int i = 0;i<n;i++){
scanf("%d",&buf[i]);
}
}
int n;
int buf[100];
while(scanf(gets(字符串变量) ){
for( int i = 0;i<n;i++){
scanf("%d",&buf[i]);
}
}
2.sort排序后的结果是升序(冒泡的复杂度不能超过百万级,超过则要改用复杂度低的其他排序)
#include <stdio.h>
#include <algorithm>
using namespace std;
int main(){
sort(buf,buf+n);//buf是起始地址,buf+n是结束地址
}
降序:(1)将得到的数组倒序
(2)使用sort另一种重载格式:sort(排序起始地址,排序结束地址,比较函数)
#include <stdio.h>
#include <algorithm>
using namespace std;
bool cmp(int x , int y){
return x>y;//表示若第一个参数比第二个参数大,则排在前面
}
int main(){
sort(buf,buf+n,cmp);//sort(排序起始地址,排序结束地址,比较函数);结果得到降序
}
cmp在这里是一种排序规则,定义该规则告诉sort函数以何种规则进行排序
P.S.qsort看起来太复杂了不想看……
3.