当前位置:网站首页>[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);
}
}
}
边栏推荐
- Leetcode daily question solution: 1189 Maximum number of "balloons"
- 3D vision - 4 Getting started with gesture recognition - using mediapipe includes single frame and real time video
- Blue Bridge Cup embedded stm32g431 - the real topic and code of the eighth provincial competition
- Superfluid_ HQ hacked analysis
- servlet(1)
- ThreeDPoseTracker项目解析
- Recommended areas - ways to explore users' future interests
- [understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.
- 电气数据|IEEE118(含风能太阳能)
- File upload vulnerability test based on DVWA
猜你喜欢
Who knows how to modify the data type accuracy of the columns in the database table of Damon
MySQL learning notes 2
False breakthroughs in the trend of London Silver
一圖看懂!為什麼學校教了你Coding但還是不會的原因...
Cookie concept, basic use, principle, details and Chinese transmission
Leetcode skimming questions_ Sum of squares
Dede collection plug-in free collection release push plug-in
Alibaba-Canal使用详解(排坑版)_MySQL与ES数据同步
一图看懂!为什么学校教了你Coding但还是不会的原因...
MATLB|实时机会约束决策及其在电力系统中的应用
随机推荐
Leetcode daily question solution: 1189 Maximum number of "balloons"
Unity | two ways to realize facial drive
CocoaPods could not find compatible versions for pod 'Firebase/CoreOnly'
MySQL learning notes 2
记一个 @nestjs/typeorm^8.1.4 版本不能获取.env选项问题
SPIR-V初窺
Dede collection plug-in free collection release push plug-in
Force buckle 1020 Number of enclaves
干货!通过软硬件协同设计加速稀疏神经网络
Basic process and testing idea of interface automation
Hcip---ipv6 experiment
朝招金安全吗 会不会亏损本金
Mongodb problem set
Mysql--- query the top 5 students
关于softmax函数的见解
ClickOnce does not support request execution level 'requireAdministrator'
IP storage and query in MySQL
Maya hollowed out modeling
A Cooperative Approach to Particle Swarm Optimization
【Flask】静态文件与模板渲染