UVA - 10474 Where is the Marble

简介: Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them.

Raju and Meena love to play with Marbles. They have got a lot of
marbles with numbers written on them. At the beginning, Raju would
place the marbles one after another in ascending order of the numbers
written on them. Then Meena would ask Raju to find the first marble
with a certain number. She would count 1…2…3. Raju gets one point
for correct answer, and Meena gets the point if Raju fails. After some
fixed number of trials the game ends and the player with maximum
points wins. Today it’s your chance to play as Raju. Being the smart
kid, you’d be taking the favor of a computer. But don’t underestimate
Meena, she had written a program to keep track how much time you’re
taking to give all the answers. So now you have to write a program,
which will help you in your role as Raju.

Input
There can be multiple test cases. Total no of test cases is less than 65. Each test case consists begins
with 2 integers: N the number of marbles and Q the number of queries Mina would make. The next
N lines would contain the numbers written on the N marbles. These marble numbers will not come
in any particular order. Following Q lines will have Q queries. Be assured, none of the input numbers
are greater than 10000 and none of them are negative.
Input is terminated by a test case where N = 0 and Q = 0.

Output
For each test case output the serial number of the case.
For each of the queries, print one line of output. The format of this line will depend upon whether
or not the query number is written upon any of the marbles. The two different formats are described
below:

• ‘x found at y’, if the first marble with number x was found at position y. Positions are numbered
1, 2, … , N.
• ‘x not found’, if the marble with number x is not present.

Look at the output for sample input for details.
Sample Input
4 1
2
3
5
1
5
5 2
1
3
3
3
1
2
3
0 0
Sample Output
CASE# 1:
5 found at 4
CASE# 2:
2 not found
3 found at 3

题意:将N个数以升序排序,进行Q次询问,找到输出在第几位,,否则输出not found。
考察sort和lower_bound

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long LL;
int main(){
    int N,Q,cas=0;
    while(cin>>N>>Q){
        if(N==0&&Q==0)
            break;
        printf("CASE# %d:\n",++cas);
        int s[10005];
        for(int i=0;i<N;i++)
            cin>>s[i];
        sort(s,s+N);
        while(Q--){
            int que,ans;
            cin>>que;
            ans=lower_bound(s,s+N,que)-s;
            if(s[ans]==que)
                printf("%d found at %d\n",que,ans+1);
            else
                printf("%d not found\n",que);
        }
    }
    return 0;
}
目录
相关文章
|
监控 Cloud Native 搜索推荐
Springboot/Springcloud整合ELK平台,(Filebeat方式)日志采集及管理(Elasticsearch+Logstash+Filebeat+Kibana)
Springboot/Springcloud整合ELK平台,(Filebeat方式)日志采集及管理(Elasticsearch+Logstash+Filebeat+Kibana)
3071 0
Springboot/Springcloud整合ELK平台,(Filebeat方式)日志采集及管理(Elasticsearch+Logstash+Filebeat+Kibana)
|
关系型数据库 MySQL 索引
MySQL的全文索引查询方法
【8月更文挑战第26天】MySQL的全文索引查询方法
241 0
|
算法 Shell Linux
【Shell 命令集合 文档编辑】Linux 检查文本文件中的拼写错误 spell 命令使用指南
【Shell 命令集合 文档编辑】Linux 检查文本文件中的拼写错误 spell 命令使用指南
316 0
|
7月前
|
Web App开发 人工智能 JavaScript
一键三连不求人!用 CodeBuddy 写个浏览器插件自动点赞、评论、收藏
本文介绍了一款通过 CodeBuddy AI 工具开发的浏览器插件,可自动完成“点赞、评论、收藏”三连操作。作者从需求出发,分四步实现:搭建基础框架、指定目标网页、解析内容并模拟点击事件,最后加载验证插件。借助 CodeBuddy 自动生成代码,整个过程高效便捷,大幅提升用户体验。此工具不仅节省手动操作时间,还为自动化任务提供了新思路,适合热爱技术与效率提升的网页冲浪者尝试。
|
缓存 测试技术 API
解锁开源模型高性能服务:SGLang Runtime 应用场景与实践
SGLang 是一个用于大型语言模型和视觉语言模型的推理框架。
|
存储 安全 大数据
大数据隐私保护:用户数据的安全之道
【10月更文挑战第31天】在大数据时代,数据的价值日益凸显,但用户隐私保护问题也愈发严峻。本文探讨了大数据隐私保护的重要性、面临的挑战及有效解决方案,旨在为企业和社会提供用户数据安全的指导。通过加强透明度、采用加密技术、实施数据最小化原则、加强访问控制、采用隐私保护技术和提升用户意识,共同推动大数据隐私保护的发展。
1363 3
|
机器学习/深度学习 Java Python
代码解密 | 2024春晚刘谦魔术与约瑟夫环问题
2024春节联欢晚会中,刘谦老师的魔术节目可以说是我心目中的全场最佳~春晚刚结束网上就有大佬给出了第二个魔术(拼扑克牌)的数学模拟,也有大佬发布了代码程序。博主在模拟了魔术过程之后,也在此分享一下程序代码和思路。同时,也借此回顾一下经典的数学问题:约瑟夫环问题。
349 8
|
数据中心 网络架构
|
敏捷开发 数据可视化 测试技术
场景用例图的作用是什么?
场景用例图的作用是什么?
447 3
|
机器学习/深度学习 自然语言处理 算法
大模型在自然语言处理中的应用
大模型在自然语言处理中的应用
1627 1