题目描述
输入
输出
样例输入
4 1 2 3 5 1 5 5 2 1 3 3 3 1 2 3 0 0
样例输出
CASE# 1:
5 found at 4
CASE# 2:
2 not found//没有该大理石的情况
3 found at 3//找到该大理石并找出该大理石的位置
题意翻译
现有N个大理石,每个大理石上写了一个非负整数。首先把各数从小到大排序,然后回 答Q个问题。每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上 写着x。排序后的大理石从左到右编号为1~N。
代码如下:
#include <iostream>
#include <algorithm>
using namespace std;
int q;
int main()
{
int n,m;
while(cin>>n>>m&&n&&m)
{
q++;
int a[101];
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+n+1); //排序
printf("CASE# %d:\n",q); //输出固定格式,q每次while循环加一
while(m--)
{
int x;
cin>>x;
int w=0,r; // 用w来判断是否有x的大理石
for(int i=1;i<=n;i++)
{
if(x==a[i])
{
w=1;
r=i;
break;
}
}
if(w==1)
printf("%d found at %d\n",x,r);
else
printf("%d not found\n",x);
}
}
}