wzt的树"/>
652. wzt的树
#include<stdio.h>
#include<algorithm>
using namespace std;//使用sort函数
int main(){int t;scanf("%d",&t);for(;t>0;t--){int n,q;scanf("%d",&n);int h[n]={0};int i,count;int xi;for(i=0;i<n;i++){scanf("%d",&h[i]);}sort(h,h+n);//使数组从小到大排列scanf("%d",&q);for(;q>0;q--){scanf("%d",&xi);for(count=n,i=0;i<n;i++){//注意count=nif (h[i]>xi){count=i;break;}}printf("%d\n",count);}}return 0;
}
sort与cmp搭配使用可以使数组从大到小排列:
#include<stdio.h>
#include <algorithm>
using namespace std;
bool cmp(int x,int y)
{return x>y;
}
//bool为布尔类型,只有一个字节,取值0(false)、1(true)。
int main()
{int a[5];printf("输入:");for(int i=0;i<5;i++){scanf("%d",&a[i]);}//sort(a,a+5);//从小到大排序 sort(a,a+5,cmp);//从大到小排序 printf("默认排序结果:");for(int i=0;i<5;i++){printf("%d ",a[i]);}return 0;
}
更多推荐
652. wzt的树
发布评论