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

@[TOC]

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

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

来自 http://codeup.cn/problem.php?cid=100000576&pid=0

来自 <http://codeup.cn/problem.php?cid=100000576&pid=0> 
题目解析:简单的查找匹配,计数;注意下循环条件(若干测试用例,当读到N=0时输入结束

//1932 Problem  A   统计同成绩学生人数 
#include <iostream>
#include <stdlib.h>
using namespace std;
int grade[1005];
//int count[1005] = {0};
int main()
{
    int N;
    int i,j;
    while(scanf("%d",&N) != EOF && N!=0)
    {//若干测试用例,当读到N=0时输入结束
        int count = 0;
        for(i=0;i<N;i++)
        {
            scanf("%d",&grade[i]);
        }
        int grade_;
        scanf("%d",&grade_);
        for(j=0;j<i;j++)
        {
            if(grade_ == grade[j])
            {
                count++;
            }
        }
        printf("%d\n",count);
    }
    return 0;
} 


1934 Problem B 找x

题析:简单的遍历查找,出现的问题见注释
#include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;
int num[205];
int main()
{
    int n,xiabiao;
    
    int i,j;
    int x;
    while(scanf("%d",&n) != EOF)//多组数据  
    {
        int flag=0;//flag每次得初始化为0 
        for(i=0;i<n;i++)
        {
            scanf("%d",&num[i]);
        }
        scanf("%d",&x);
        for(j=0;j<i;j++)
        {
            if(x == num[j])
            {
                xiabiao = j;
                flag = 1;
            }
        }
        if(flag==0)
        {//两种都行 
            printf("%d\n",-1);
        //  printf("-1\n");
        }
        else
            printf("%d\n",xiabiao); 
    }
    return 0;
} 


1935 Problem C 查找学生信息

问题:字符串输出为?问号;有多组示例时得用while(**!=EOF)才行;
还有学生信息的字符串数组必须足够大
//1935ProblemC查找学生信息
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct stu
{
    char no[10];
    char name[100];
    char sex[15];
    int age;
}student[1005];

int main()
{
    int N;
    while(scanf("%d",&N)!= EOF)
    {
        for(int i=0;i<N;i++)
        {
            scanf("%s %s %s %d",student[i].no,student[i].name,student[i].sex,&student[i].age);
        }
        int M;
        scanf("%d",&M);
        while(M--)
        {
            char chaxun[10];
            int flag=1;
            scanf("%s",chaxun);     
            for(int i=0;i<N;i++)
            {
            //  if(number == student[i].no)
                if(strcmp(chaxun,student[i].no) == 0)
                {
                    printf("%s %s %s %d\n",student[i].no,student[i].name,student[i].sex,student[i].age);
                    flag = 0;
                    break;
                }
        //      cout<<"asc"<<endl;//测试用的 
            }
            if(flag == 1)
                printf("No Answer!\n");
        }
    }
    return 0;   
} 


1937 Problem D 查找

题目较为简单,为上一题的简化版

//1937ProblemD查找
#include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;

int main()
{
    int a[105],b[105];
    int n;
    while(scanf("%d",&n)!= EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        int m;
        scanf("%d",&m);
        while(m--)
        {
            int chaxun;
            scanf("%d",&chaxun);
            int flag=1;
            for(int i=0;i<n;i++)
            {
                if(a[i]==chaxun)
                {
                    printf("YES\n");
                    flag=0;
                    break;  
                } 
            }
            if(flag==1)
            {
                printf("NO\n");
            }
        }
    }
    return 0;   
} 


2020 Problem E 学生查询

//2020ProblemE学生查询
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct stu
{
    int no;
    char name[15];
    char sex[10];
    int age;
}student[25];


int main()
{
    int m;
    scanf("%d",&m);
    //cin>>m;
    while(m--)
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d %s %s %d",&student[i].no,student[i].name,student[i].sex,&student[i].age);
        }
        int chaxun;
        scanf("%d",&chaxun);
        for(int i=0;i<n;i++)
        {
            if(student[i].no == chaxun)
            {
                printf("%d %s %s %d\n",student[i].no,student[i].name,student[i].sex,student[i].age);
            //  cout<<student[i].no<<" "<<student[i].name<<" "<<student[i].sex<<" "<<student[i].age<<endl;
                break;
            }
        }
    }
    return 0;
}



问题:序号、年龄错乱,姓名性别字符变为问号???
自答:复制黏贴时候可能会有中文符号代替英文半角符号,从而乱码,但OJ自己给的测试用例不会错


字符乱码
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,470评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,393评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,577评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,176评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,189评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,155评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,041评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,903评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,319评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,539评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,703评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,417评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,013评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,664评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,818评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,711评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,601评论 2 353

推荐阅读更多精彩内容