cjweffort

博客园 首页 联系 订阅 管理

http://ac.jobdu.com/problem.php?cid=1040&pid=17

题目描述:

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

输入:

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

接下来的N行包括N个学生的信息,信息格式如下:
01 李江 男 21
02 刘唐 男 23
03 张军 男 19
04 王娜 女 19
然后输入一个M(M<=10000),接下来会有M行,代表M次查询,每行输入一个学号,格式如下:
02
03
01
04
输出:

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

如果没有对应的学生信息,则输出“No Answer!”
样例输入:
4
01 李江 男 21
02 刘唐 男 23
03 张军 男 19
04 王娜 女 19
5
02
03
01
04
03
样例输出:
02 刘唐 男 23
03 张军 男 19
01 李江 男 21
04 王娜 女 19
03 张军 男 19
// 题目18:查找学生信息.cpp: 主项目文件。

#include "stdafx.h"
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N=1003;
typedef struct STU
{
	char no[8];
	char name[100],sex[6];
	int age;
}STU;
STU stu[N];

bool cmp(STU m1, STU m2)
{
	return strcmp(m1.no,m2.no)<0;
}

int binarySearch(char *str, int low, int high)
{
	if(low>high)
		return -1;
	int mid=(low+high)>>1;
	if(strcmp(str,stu[mid].no)==0)
		return mid;
	else if(strcmp(str,stu[mid].no)<0)
	{
		high=mid-1;
		return binarySearch(str,low,high);
	}
	else
	{
		low=mid+1;
		return binarySearch(str,low,high);
	}
}

int main()
{
    int n;
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=0;i<n;i++)
			scanf("%s%s%s%d",stu[i].no,stu[i].name,stu[i].sex,&stu[i].age);
		sort(stu,stu+n,cmp);
		int m;
		scanf("%d",&m);
		while(m--)
		{
			char str[8];
			scanf("%s",str);
			int index=binarySearch(str,0,n-1);
			if(index==-1)
				printf("No Answer!\n");
			else
				printf("%s %s %s %d\n",stu[index].no,stu[index].name,stu[index].sex,stu[index].age);
		}
	}
    return 0;
}


posted on 2013-03-02 12:08  cjweffort  阅读(160)  评论(0编辑  收藏  举报