zoukankan      html  css  js  c++  java
  • 大理石在哪(where is the Marble? VUa 10474)

        两个东西,一个是sort函数(默认升序),可以对任意对象进行排序(sort是个模板函数),排序对象可以存在普通数组里,也可以存在vector中。前者用sort(a,a+n),后者用sort(a.begin(),a.end())。一个是lower_bound函数,作用是查找“大于等于X的第一个位置”。(第一次写博客QAQ)

    # include <cstdio>
    # include <algorithm>
    using namespace std;
    const int maxn = 10000;
    int main(){
    	int n, q, x, a[maxn], kase = 0; // n 是总数,q是需要找的数字 
    	while(scanf("%d%d",&n, &q) == 2 &&n){
    		printf("CASE# %d:
    ",++kase); 
    		for(int i = 0;i < n; i++)
    		scanf("%d",&a[i]);
    		sort(a,a+n); // 排序 
    		while(q--){
    			scanf("%d",&x);
    			int p = lower_bound(a,a+n,x) - a;
    			if(a[p] == x)
    			printf("%d found at %d
    ", x, p+1);
    			else
    			printf("%d not found
    ", x);
    		}
    		
    	}
    	return 0;
    }
    
    // 4 1 
    // 2 3 5 1
    // 5
  • 相关阅读:
    redis数据结构-list
    reids数据结构1-string
    jedis工具类
    静态资源放行
    SpringMVC拦截器
    xinetd
    Linux-open函数
    Linux简单的文件读取
    复习ssm02
    springMVC文件上传
  • 原文地址:https://www.cnblogs.com/UUUUh/p/10284078.html
Copyright © 2011-2022 走看看