zoukankan      html  css  js  c++  java
  • 大理石在哪儿 UVa10474

      现有N个大理石,每个大理石写了一个非负整数。首先把各数从小到大排序,然后回答Q个问题。每个问题是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上写着x。排序后的大理石从左到右编号为1~N。(在样例中,为了节约篇幅,所有大理石上的数合并到一行,所有问题也合并到一行。)

    样例输入:

    4 1

    2 3 5 1 

    5

    5 2

    1 3 3 3 1

    2 3

    样例输出:

    CASE# 1:

    5 found at 4

    CASE# 2:

    2 not found

    3 found at 3

    //UVa 10474
    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int maxn=1000;
    int main(){
    	int n,q,x,a[maxn],kase=0;
    	while (scanf("%d%d",&n,&q)==2 && n)
    	{
    		++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;//lower_bound(begin,end,num) 若存在返回第一个num的地址,否则返回end。返回地址-起始地址begin得num下标
    			printf("CASE# %d
    ",kase);
    			if(a[p]==x) printf("%d found at %d
    ",x,p+1);
    			else printf("%d not found
    ",x);
    		}
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    msp430入门学习30
    msp430入门学习03
    msp430入门学习27
    msp430入门学习26
    msp430入门学习25
    msp430入门学习24
    msp430入门学习23
    msp430入门学习22
    去掉xcode中警告的一些经验
    CocoaPods详解之----使用篇
  • 原文地址:https://www.cnblogs.com/codercql/p/12534206.html
Copyright © 2011-2022 走看看