算法笔记学习笔记

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

2020-04-07  本文已影响0人  木子李_0961

@[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自己给的测试用例不会错


字符乱码
上一篇 下一篇

猜你喜欢

热点阅读