Uva where is the marble(心得总结)
题目:
思路:
简单的排序和检索
心得:
对比了自己和刘老师的代码
值得学习:
int p= lower_bound(a,a+n,x)-a;
//查找大于等于x的第一个位置
代码:
我的:
#include <iostream>
#include<algorithm>
using namespace std;
const int maxn = 1e4+5;
int stone[maxn];
int q[maxn];
int main()
{
int n,q_cnt;//
int case_cnt=0;
while(cin>>n>>q_cnt)
{
if(!n&&!q_cnt) break;
case_cnt++;
for(int i=0;i<n;i++) cin>>stone[i];
for(int i=0;i<q_cnt;i++) cin>>q[i];
sort(stone,stone+n);
printf("CASE# %d:\n",case_cnt);
for(int i=0;i<q_cnt;i++)
{
int flag = 0;
for(int j=0;j<n;j++)
{
if(stone[j]==q[i])
{
flag = 1;
printf("%d found at %d\n",q[i],j+1);
break;
}
}
if(!flag) printf("%d not found\n",q[i]);
}
}
return 0;
}
刘老师
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=10000;
int main()
{
int n,q,x,a[maxn],kase=0;
while(scanf("%d%d",&n,&q)==2&&n)
{
printf("case%d\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;//查找大于等于x的第一个位置
if(a[p]==x) printf("%d found at %d\n",x,p+1);
else printf("%d not found\n",x);
}
}
}
return 0;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话