Sequence table application 6: sequence table query
Time Limit: 1000 ms Memory Limit: 4096 KiB
Problem Description
There are n different integers in the order of small to large in the sequence table. Input an integer arbitrarily to determine whether the integer exists in the sequence table. If the integer exists in the order table, output its ordinal in the table; otherwise, output "No Found!".
Input
Enter the integer n (1 < = n < = 100000) in the first row to indicate the number of elements in the order table;
In the second line, input n different ordered non negative integers, representing the elements in the table;
In the third line, enter the integer t (1 < = T < = 100000), which represents the number of times to query;
In the fourth line, enter t non negative integers in turn, representing the value to be queried each time.
Ensure that all input numbers are in the int range.
Output
Output t row, which represents the result of T queries. If you find the position of this element in the table in this row, otherwise, this row will output No Found!
Sample Input
10 1 22 33 55 63 70 74 79 80 87 4 55 10 2 87
Sample Output
4 No Found! No Found! 10
Hint
Source
Attach timeout code
#include<stdio.h> #include<stdlib.h> typedef int ElementType; typedef struct node { ElementType data; struct node *next; }List; List L,*PtrL; int Find(ElementType X,List *PtrL) { int i = 1; List *p = PtrL; while(p != NULL&&p->data != X) { p = p->next; i++; } return i; } int length(List *PtrL) { List *p = PtrL; int j = 0; while(p) { p = p->next; j++; } return j; } int main() { int n, i,m,x,z,j; struct node *head,*p,*tail; head = (struct node *)malloc(sizeof(struct node)); head->next =NULL; scanf("%d",&n); tail = head; for(i = 1;i <= n;i++) { p = (struct node *)malloc(sizeof(struct node)); scanf("%d",&p->data); p-> next = NULL; tail ->next = p; tail = tail ->next; } scanf("%d",&m); for(i = 1;i <= m;i++) { scanf("%d",&x); tail = head->next; z = Find(x,tail); j = length(tail); if(z <1 || z >j) { printf("No Found!\n"); } else { printf("%d\n",z); } } return 0; }
This is over time~~
#include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }; int main() { int n, i,x,flag,key,sum; struct node *head,*tail,*p; head = (struct node *)malloc(sizeof(struct node)); head->next = NULL; tail = head; scanf("%d",&n); for(i = 1; i <= n; i++) { p = (struct node *)malloc(sizeof(struct node)); scanf("%d",&p->data); p->next =NULL; tail->next = p; tail = p; } scanf("%d",&x); for(i = 1; i <= x; i++) { flag = -1; sum = 0; scanf("%d",&key); tail = head -> next; while(tail) { sum++; if(tail->data == key) { flag = sum; break; } else tail = tail->next; } if(flag != -1) { printf("%d\n",flag); } else { printf("No Found!\n"); } } return 0; }
It's over time~~
Finally, ac
#include<stdio.h> #include<stdlib.h> #define MAXSIZE 100000 typedef int ElementType; typedef struct { ElementType data[MAXSIZE]; int Last; }List; List L,*PtrL; /*List *MakeEmpty() { List *PtrL; PtrL = (List *)malloc(sizeof(List)); PtrL -> Last = -1; return PtrL; }*/ int Binsearch(int a[],int n,int x) { int s = 1,t = n; int mid; while(s <= t) { mid = (s + t)/2; if(L.data[mid] == x) return mid; if(L.data[mid]<x) { s = mid +1; } else t = mid -1; } return -1; } int main() { int n,i,m,j,x; scanf("%d",&n); for(i = 1;i <= n;i++) { scanf("%d",&L.data[i]); } scanf("%d",&m); for(i = 1;i <= m;i++) { scanf("%d",&x); j = Binsearch(L.data,n,x); if(j == -1) { printf("No Found!\n"); } else { printf("%d\n",j); } } /* for(i = 1;i <= n;i++) { printf("%d ",L.data[i]); }*/ return 0; }
Using dichotomy~~