Beijing post postgraduate entrance examination C language computer topic selection
- 2020-04-02 03:13:53
- OfStack
To find the
Title description:
Enter the array length n
Input array a[1...n]
Enter the search number m
Enter the search number b[1...m]
The output YES or NO looks for YES or NO.
Input:
The input has multiple sets of data.
Enter n for each group, then n integers, then m, then m integers (1)
<
= m.
<
= n
<
= 100).
Output:
If I print YES in n arrays otherwise I print NO.
Sample input:
5
1, 5, 2, 4, 3
3
2 5 6
Sample output:
YES
YES
NO
AC code:
This problem is not difficult, just two for loop implementation
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[101], b[101];
int n, m, i, j, flag;
while(scanf("%d", &n) != EOF)
{
//Receive input array
for(i = 0; i < n; i ++)
{
scanf("%d", a + i);
}
//Receive lookup array
scanf("%d", &m);
for(j = 0; j < m; j ++)
{
scanf("%d", b + j);
}
//Judgment search existence
for(j = 0; j < m; j ++)
{
flag = 0;
for(i = 0; i < n; i ++)
{
if(b[j] == a[i])
{
flag = 1;
break;
}
}
if(flag)
{
printf("YESn");
}else
{
printf("NOn");
}
}
}
return 0;
}
Now, I would definitely use the Java HashMap to do this
Find the KTH decimal
Title description:
Look for the KTH smallest number in an array, and note that the same size is the same size.
Such as
2, 1, 3, 4, 5, 2, the third decimal is 3.
Input:
The input has multiple sets of data.
Enter n for each group, and then enter n integers (1)
<
= n
<
=1000), and then enter k.
Output:
Output the KTH smallest integer.
Sample input:
6
2, 1, 3, 5, 2, 2
3
Sample output:
3
AC code:
What I'm looking at is a simple quicksort, on my AC code
#include <stdio.h>
#include <stdlib.h>
int partition(int *A, int left, int right);
void quicksort(int *A, int begin, int end);
int main()
{
int i, j, n, k;
int a[1001];
while(scanf("%d",&n) != EOF)
{
//Accept stdin input data
for(i = 0; i < n; i ++)
{
scanf("%d",a + i);
}
scanf("%d",&k);
//Quick sort
quicksort(a, 0, n - 1);
//Output the KTH smallest number
for(i = 0, j = 0; i < n && j < k; i ++)
{
if(a[i] != a[i + 1])
{
if(j == k - 1)
{
printf("%dn",a[i]);
break;
}else
{
j ++;
}
}
}
}
return 0;
}
void quicksort(int *A, int begin, int end)
{
int pivot;
if(begin < end)
{
pivot = partition(A, begin, end);
quicksort(A, begin, pivot - 1);
quicksort(A, pivot + 1, end);
}
}
int partition(int *A, int left, int right)
{
int stand = A[left];
while(left < right)
{
while(left < right && A[right] >= stand)
{
right --;
}
if(left < right)
{
A[left ++] = A[right];
}
while(left < right && A[left] <= stand)
{
left ++;
}
if(left < right)
{
A[right --] = A[left];
}
}
A[left] = stand;
return left;
}
Playing CARDS
Title requirements:
Title description:
The CARDS range from 1 to 9. You hold the a CARDS in order. The opponent plays the b CARDS.
Rule: there are 5 types of play
[1] a sheet of 4 is 5... 9 May
[2] two such as 44 would be 55,66,77... , 99 can be overpressed
[3] three such as 444 rules such as [2]
[4] four such as 4444 rules such as [2]
[5] there are only 12345, 23456, 34567, 45678, 56789 in the five-card model. The latter is larger than the former.
Input:
The input has multiple sets of data.
Enter two strings for each group (string size not exceeding 100)a, b. The a string represents the card in hand, and the b string represents the card.
Output:
Press YES or NO.
Sample input:
12233445566677
33
Sample output:
YES
Notes:
At the beginning, I submitted three times, all of which were wa. I found that I could not pass one test case, which was also the error of the last code.
Sample input: 1122335566778899 (discontinuous) 12345
Sample output: yes
AC code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[101];
char b[101];
char ch, key;
int i, lena, lenb, flag;
int count[11];
while(scanf("%s",a) != EOF)
{
//Receive a carriage return
ch = getchar();
//Receive a card
scanf("%s",b);
ch = getchar();
//The length of the
lena = strlen(a);
lenb = strlen(b);
//Initialize the
memset(count,0,sizeof(count));
//Traverse the hand
for(i = 0; i < lena; i ++)
{
count[a[i] - '0'] ++;
}
//Detect the opponent's play
switch(lenb)
{
case 1:
case 2:
case 3:
case 4:
flag = 0;
for(key = b[0] - '0' + 1; key <= 9; key ++)
{
if(count[key] >= lenb)
{
flag = 1;
break;
}
}
break;
case 5:
flag = 0;
for(key = b[0] - '0' + 1; key < 9; key ++)
{
if(count[key] > 0 && count[key + 1] > 0 && count[key + 2] > 0 && count[key + 3] > 0 && count[key + 4] > 0)
{
flag = 1;
break;
}
}
break;
}
//A printout
if(flag)
{
printf("YESn");
}else
{
printf("NOn");
}
}
return 0;
}