当前位置:网站首页>[Jiudu OJ 09] two points to find student information
[Jiudu OJ 09] two points to find student information
2022-07-06 01:27:00 【CodeLinghu】
【 Nine degrees OJ 09】 Two points to find student information
List of articles
One 、 The question
Two 、 The answer process
Ideas :
Two points search
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct student// Used to represent the structure of students
{
char sno[100];// Student number
char name[100];// full name
char sex[10];// Gender
int age;// Age
// The comparison function embedded in the structure
bool operator < (const student &b) const// Comparison function , Make it possible to sort Function order
{
return strcmp(sno,b.sno)<0;
}
}buf[1000];
int main()
{
int n,m;
char x[100];
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
{
scanf("%s %s %s %d",&buf[i].sno,&buf[i].name,&buf[i].sex,&buf[i].age);
}
sort(buf,buf+n);// Sort the array , Make it sort in ascending order by student number
scanf("%d",&m);
while(m--!=0)
{
int ans=-1;
scanf("%s",&x);// Enter the student number to be searched
int top=n-1,base=0;// The starting subscript is 0, The ending subscript is n-1, Find the entire array
int cnt=1;
while(top>=base)
{
int mid=(top+base)/2;// Calculate the middle point subscript
int tmp=strcmp(buf[mid].sno,x);// Compare the intermediate student number with the target student number
if(tmp==0)
{
ans=mid;
break;// If equal, then complete the jump out of the binary search
}
else if(tmp<0)base=mid+1;// If less than , Then the starting subscript becomes the coordinate of the point after the intermediate node
else top=mid-1;// If more than , Then the end subscript becomes the subscript before the intermediate node
printf("%d\t",mid);
printf("%d\t",cnt++);
}
if(ans==-1)printf(" not find");// To find the failure
else printf("%s %s %s %d\n",buf[ans].sno,buf[ans].name,buf[ans].sex,buf[ans].age);
}
}
}
边栏推荐
- Spir - V premier aperçu
- Gartner released the prediction of eight major network security trends from 2022 to 2023. Zero trust is the starting point and regulations cover a wider range
- 记一个 @nestjs/typeorm^8.1.4 版本不能获取.env选项问题
- 【Flask】获取请求信息、重定向、错误处理
- [day 30] given an integer n, find the sum of its factors
- Leetcode 208. 实现 Trie (前缀树)
- SSH login is stuck and disconnected
- Basic process and testing idea of interface automation
- Netease smart enterprises enter the market against the trend, and there is a new possibility for game industrialization
- Unity | two ways to realize facial drive
猜你喜欢
Blue Bridge Cup embedded stm32g431 - the real topic and code of the eighth provincial competition
Leetcode skimming questions_ Invert vowels in a string
282. Stone consolidation (interval DP)
How does the crystal oscillator vibrate?
Daily practice - February 13, 2022
Kotlin basics 1
关于softmax函数的见解
Mathematical modeling learning from scratch (2): Tools
Unity | 实现面部驱动的两种方式
Hcip---ipv6 experiment
随机推荐
Leetcode study - day 35
Superfluid_ HQ hacked analysis
FFT learning notes (I think it is detailed)
Vulhub vulnerability recurrence 74_ Wordpress
Maya hollowed out modeling
Paddle框架:PaddleNLP概述【飞桨自然语言处理开发库】
JMeter BeanShell的基本用法 一下语法只能在beanshell中使用
MUX VLAN configuration
MYSQL---查询成绩为前5名的学生
JVM_ 15_ Concepts related to garbage collection
Remember that a version of @nestjs/typeorm^8.1.4 cannot be obtained Env option problem
Basic process and testing idea of interface automation
ORA-00030
Paging of a scratch (page turning processing)
基於DVWA的文件上傳漏洞測試
LeetCode 322. Change exchange (dynamic planning)
Idea sets the default line break for global newly created files
[Arduino syntax - structure]
伦敦银走势中的假突破
一图看懂!为什么学校教了你Coding但还是不会的原因...