Problem link:
Discuss: you can take input in array or vector and sort that.after sort you can search the query.you can do it with linear or binary search or others there is no problem with time limit. be aware of test case number it's some time makes trouble.
try yourself before see the code
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long n,q,i,j,temp,t=1,flag,x,y,l,k,r;
while(scanf("%lld %lld",&n,&q)==2)
{
vector<long long>marble;
vector<long long>qur;
if(n==0&&q==0)
break;
for(l=0;l<n;l++)
{
cin >>r;
marble.push_back(r);
}
for(k=0;k<q;k++)
{
cin >>r;
qur.push_back(r);
}
printf("CASE# %lld:\n",t);
t++;
sort(marble.begin(),marble.end());
for(j=0;j<q;j++)
{
flag=0;
for(i=0;i<n;i++)
{
if(qur[j]==marble[i]&&flag!=1)
{
x=qur[j];
y=i;
flag=1;
break;
}
}
if(flag==1)
printf("%lld found at %lld\n",x,y+1);
else
printf("%lld not found\n",qur[j]);
}
}
return 0;
}
Discuss: you can take input in array or vector and sort that.after sort you can search the query.you can do it with linear or binary search or others there is no problem with time limit. be aware of test case number it's some time makes trouble.
try yourself before see the code
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long n,q,i,j,temp,t=1,flag,x,y,l,k,r;
while(scanf("%lld %lld",&n,&q)==2)
{
vector<long long>marble;
vector<long long>qur;
if(n==0&&q==0)
break;
for(l=0;l<n;l++)
{
cin >>r;
marble.push_back(r);
}
for(k=0;k<q;k++)
{
cin >>r;
qur.push_back(r);
}
printf("CASE# %lld:\n",t);
t++;
sort(marble.begin(),marble.end());
for(j=0;j<q;j++)
{
flag=0;
for(i=0;i<n;i++)
{
if(qur[j]==marble[i]&&flag!=1)
{
x=qur[j];
y=i;
flag=1;
break;
}
}
if(flag==1)
printf("%lld found at %lld\n",x,y+1);
else
printf("%lld not found\n",qur[j]);
}
}
return 0;
}
No comments:
Post a Comment