当前位置:网站首页>[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);
}
}
}
边栏推荐
- 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
- UE4 unreal engine, editor basic application, usage skills (IV)
- 现货白银的一般操作方法
- General operation method of spot Silver
- Tcpdump: monitor network traffic
- How does the crystal oscillator vibrate?
- 有谁知道 达梦数据库表的列的数据类型 精度怎么修改呀
- XSS learning XSS lab problem solution
- Opinions on softmax function
- Leetcode 208. 实现 Trie (前缀树)
猜你喜欢
Opinions on softmax function
[technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry
MATLB | real time opportunity constrained decision making and its application in power system
servlet(1)
Daily practice - February 13, 2022
How to see the K-line chart of gold price trend?
现货白银的一般操作方法
3D模型格式汇总
A Cooperative Approach to Particle Swarm Optimization
VMware Tools installation error: unable to automatically install vsock driver
随机推荐
3D视觉——4.手势识别(Gesture Recognition)入门——使用MediaPipe含单帧(Singel Frame)和实时视频(Real-Time Video)
XSS learning XSS lab problem solution
leetcode刷题_验证回文字符串 Ⅱ
Condition and AQS principle
Pbootcms plug-in automatically collects fake original free plug-ins
ctf. Show PHP feature (89~110)
Win10 add file extension
Blue Bridge Cup embedded stm32g431 - the real topic and code of the eighth provincial competition
282. Stone consolidation (interval DP)
Remember that a version of @nestjs/typeorm^8.1.4 cannot be obtained Env option problem
Dedecms plug-in free SEO plug-in summary
VMware Tools安装报错:无法自动安装VSock驱动程序
Modify the ssh server access port number
SCM Chinese data distribution
一圖看懂!為什麼學校教了你Coding但還是不會的原因...
SPIR-V初窺
FFT learning notes (I think it is detailed)
MySQL learning notes 2
Mongodb problem set
servlet(1)