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

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自己给的测试用例不会错
字符乱码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李霁明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值