Where is the Marble?

Source: Internet
Author: User

UVA_10474

You can use Hash to read, sort, and traverse the Marbles array and assign values to the hash array, hash [I] indicates the position where the Marble labeled I appears for the first time.

After reading a Q, you only need to determine whether the elements in the corresponding hash array are assigned a value and output accordingly.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int a[10010],hash[10010];
int cmp(const void *_p,const void *_q)
{
int *p=(int *)_p;
int *q=(int *)_q;
return *p-*q;
}
int main()
{
int i,j,k,N,Q,t;
t=0;
while(1)
{
scanf("%d%d",&N,&Q);
if(N==0)
break;
printf("CASE# %d:\n",++t);
for(i=0;i<N;i++)
scanf("%d",&a[i]);
qsort(a,N,sizeof(a[0]),cmp);
memset(hash,-1,sizeof(hash));
for(i=0;i<N;i++)
if(hash[a[i]]==-1)
hash[a[i]]=i+1;
for(i=0;i<Q;i++)
{
scanf("%d",&k);
if(hash[k]==-1)
printf("%d not found\n",k);
else
printf("%d found at %d\n",k,hash[k]);
}
}
return 0;
}

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.