前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Contest100000576 – 《算法笔记》3.2小节——入门模拟->查找元素

Contest100000576 – 《算法笔记》3.2小节——入门模拟->查找元素

作者头像
可定
发布2020-04-20 14:58:46
3070
发布2020-04-20 14:58:46
举报
文章被收录于专栏:细嗅蔷薇细嗅蔷薇

http://codeup.cn/contest.php?cid=100000576

Problem A: 统计同成绩学生人数

Time Limit: 1.000 Sec Memory Limit: 32 MB

Submit: 2279 Solved: 1103

Description

读入N名学生的成绩,将获得某一给定分数的学生人数输出。

Input

测试输入包含若干测试用例,每个测试用例的格式为

第1行:N

第2行:N名学生的成绩,相邻两数字用一个空格间隔。

第3行:给定分数

当读到N=0时输入结束。其中N不超过1000,成绩分数为(包含)0到100之间的一个整数。

Output

对每个测试用例,将获得给定分数的学生人数输出。

Sample Input

代码语言:javascript
复制
4
70 80 90 100
80
3
65 75 85
55
5
60 90 90 90 85
90
0

Sample Output

代码语言:javascript
复制
1
0
3

代码(C语言)

代码语言:javascript
复制
#include<stdio.h>
const int maxn=1010;
int a[maxn];
int main(){
    int n;
    while(scanf("%d",&n) && n!=0){
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        int x,k=0;
        scanf("%d",&x);
        for(int k=0;k<n;k++) {
            if(x==a[k]){
                printf("%d\n",k);
                break;
            }
            if(k==n-1){
                printf("-1\n");
            }
        }
    }
    return 0;
}

Problem B: 找x

Time Limit: 1.000 Sec Memory Limit: 32 MB

Submit: 12241 Solved: 3256

Description

输入一个数n,然后输入n个数值各不相同,再输入一个值x,输出这个值在这个数组中的下标(从0开始,若不在数组中则输出-1)。

Input

测试数据有多组,输入n(1<=n<=200),接着输入n个数,然后输入x。

Output

对于每组输入,请输出结果。

Sample Input

代码语言:javascript
复制
4
1 2 3 4
3

Sample Output

2

代码(C++)

代码语言:javascript
复制
#include<cstdio>
const int maxn = 210;
int a[maxn];
int main(){
    int n,x;
    while(scanf("%d,&n"!=EOF){
        for(int i = 0; i < n ; i++){
            scanf("%d",&a[i]);
        }
        scanf("%d",&x);
        int k;//下标
        for(k = 0; k < n ; k++){
            if(a[k]==x){
                printf("%d\n",k);
                break;
            }
        }
        if(k==n-1){
            printf("-1\n");
        }
    }
    return 0;
}

Problem C: 查找学生信息

Time Limit: 1.000 Sec Memory Limit: 32 MB

Submit: 5946 Solved: 1207

Description

输入N个学生的信息,然后进行查询。

Input

输入的第一行为N,即学生的个数(N<=1000)

接下来的N行包括N个学生的信息,信息格式如下:

01 李江 男 21

02 刘唐 男 23

03 张军 男 19

04 王娜 女 19

然后输入一个M(M<=10000),接下来会有M行,代表M次查询,每行输入一个学号,格式如下:

02

03

01

04

Output

输出M行,每行包括一个对应于查询的学生的信息。

如果没有对应的学生信息,则输出“No Answer!”

Sample Input

代码语言:javascript
复制
5
001 张三 男 19
002 李四 男 20
003 王五 男 18
004 赵六 女 17
005 刘七 女 21
7
003
002
005
004
003
001
006

Sample Output

代码语言:javascript
复制
003 王五 男 18
002 李四 男 20
005 刘七 女 21
004 赵六 女 17
003 王五 男 18
001 张三 男 19
No Answer!

代码(C++)

代码语言:javascript
复制
#include<cstdio>
#include<cstring>
const int maxn=1010;
struct stu{
    char id[10];
    char name[100];
    char sex[20];
    int age;
}stu[maxn];
int main(){
    int N,M,j;
    while(scanf("%d",&N)!=EOF){
        for(int i=0;i<N;i++) {
            scanf("%s %s %s %d",stu[i].id,stu[i].name,stu[i].sex,&stu[i].age);
        }
         scanf("%d",&M);
         while(M--){
            char id_search[10];
            scanf("%s",id_search);
            for(int i=0;i<N;i++){
            if(strcmp(stu[i].id,id_search)==0){
                printf("%s %s %s %d\n",stu[i].id,stu[i].name,stu[i].sex,stu[i].age);
                break;
            }else if(strcmp(stu[i].id,id_search)!=0&&i==N-1){
                printf("No Answer!\n");
               }
           }
        }
    }
    return 0;
}

Problem D: 查找

Time Limit: 1.000 Sec Memory Limit: 32 MB

Submit: 2822 Solved: 1272

Description

输入数组长度 n

输入数组 a1...n

输入查找个数m

输入查找数字b1...m

输出 YES or NO 查找有则YES 否则NO 。

Input

输入有多组数据。

每组输入n,然后输入n个整数,再输入m,然后再输入m个整数(1<=m<=n<=100)。

Output

如果在n个数组中输出YES否则输出NO。

Sample Input

代码语言:javascript
复制
6
3 2 5 4 7 8
2
3 6

Sample Output

代码语言:javascript
复制
YES
NO

代码

代码1(C++)

代码语言:javascript
复制
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
    int n,m;
    int a[101],b[101];
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        cin>>m;
        for(int i=0;i<m;i++) {
            cin>>b[i];
        }
        int j=0;
        for(int i=0;i<m;i++) {
            for(j=0;j<n;j++){  //j是a数组的数组下标, b数组与a数组比较,a数组有或没有的才输出Y或N,因此要判断j的变化 
                if(b[i]==a[j]){
                    printf("YES\n");
                    break;
                }
                if(j==n-1){ //遍历了一圈儿,j所指的数中没有要查找的数,输入N ,注意!if不在里层fao循环里,外层for循环里 
                    printf("NO\n");
                }
            }
        }
    }
    return 0;
}

代码2(C语言)

代码语言:javascript
复制
#include<cstdio>
int main(){
    int n,m;
    int a[101],b[101];
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++){
            printf("%d",&a[i]);
        }
        printf("%d",&m);
        for(int i=0;i<m;i++) {
            printf("%d",&b[i]);
        }
        int j=0;
        for(int i=0;i<m;i++) {
            for(j=0;j<n;j++){  //j是a数组的数组下标, b数组与a数组比较,a数组有或没有的才输出Y或N,因此要判断j的变化 
                if(b[i]==a[j]){
                    printf("YES\n");
                    break;
                }
                if(j==n-1){ //遍历了一圈儿,j所指的数中没有要查找的数,输入N ,注意!if不在里层fao循环里,外层for循环里 
                    printf("NO\n");
            }
                }
        }
    }
    return 0;
}

提示:输出一堆乱码。为何?

Problem E: 学生查询

Time Limit: 1.000 Sec Memory Limit: 32 MB

Submit: 2107 Solved: 824

Description

输入n个学生的信息,每行包括学号、姓名、性别和年龄,每一个属性使用空格分开。最后再输入一学号,将该学号对应的学生信息输出。

Input

测试数据有多组,第一行为样例数m。对于每个样例,第一行为学生人数n(n不超过20),加下来n行每行4个整数分别表示学号、姓名、性别和年龄,最后一行表示查询的学号。

Output

输出m行,每行表示查询的学生信息,格式参见样例。

Sample Input

代码语言:javascript
复制
1
4
1 李江 男 21
2 刘唐 男 23
3 张军 男 19
4 王娜 女 19
2

Sample Output

2 刘唐 男 23

代码(C++)

代码语言:javascript
复制
#include<stdio.h>
#include<string.h>
struct stu{
    int id;
    char name[20];
    char sex[10];
    int age;
}stu[200];
int main(){
    int m,n,ids;
    scanf("%d",&m);
    for(int i=0;i<m;i++){
        scanf("%d",&n);
        for(int j=0;j<n;j++){
            scanf("%d %s %s %d",&stu[j].id,stu[j].name,stu[j].sex,&stu[j].age);
        }
        scanf("%d",&ids);
        for(int j=0;j<n;j++){
            if(ids==stu[j].id){
                printf("%d %s %s %d\n",stu[j].id,stu[j].name,stu[j].sex,stu[j].age);
                break;
            }
        }
    }
    return 0;
}

鸣谢

无耻柠檬兵(https://blog.csdn.net/c1014yzh/article/details/87912794)

版权所有:可定博客 © WNAG.COM.CN

本文标题:《Contest100000576 – 《算法笔记》3.2小节——入门模拟->查找元素》

本文链接:https://cloud.tencent.com/developer/article/1616889

特别声明:除特别标注,本站文章均为原创,本站文章原则上禁止转载,如确实要转载,请电联:wangyeuuu@qq.com,尊重他人劳动成果,谢过~

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-01-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem A: 统计同成绩学生人数
  • Problem B: 找x
  • Problem C: 查找学生信息
  • Problem E: 学生查询
  • 鸣谢
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档