Bupt Postgraduate Exam C language on the computer topics selected _c language

Source: Internet
Author: User
Tags array length

Find

Topic Description:

Input array length n
Input array A[1...N]
Number of input lookups m
Input Lookup number B[1...M]

Output yes or no to find yes or No.
Input:

Multiple sets of data are entered.
Enter n for each group, then enter n integers, then enter M, and then enter M int (1<=m<=n<=100).
Output:

Output No if yes is output in n arrays.
Sample input:

5
1 5 2 4 3
3
2 5 6
Sample output:

YES
YES
NO

AC Code:
This problem is not any difficulty, is two for loop implementation can

  #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); 
      } 
      The decision lookup exists 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 ("yes\n"); 
        } else 
        { 
          printf ("no\n"); 
    }} return 0; 
  } 


Now, I would definitely use the Java HashMap to do this topic.


Find K decimal

Topic Description:

Look for a small number of the K of an array, note that the same size is the same as large.
such as 2 1 3 4 5 2 The third decimal number is 3.
Input:

Multiple sets of data are entered.
Enter n for each group, then enter n integers (1<=n<=1000), and then enter K.
Output:

An integer that outputs the small K.
Sample input:

6
2 1 3 5 2 2
3
Sample output:

3

AC Code:
The expedition is simply a quick sort, 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 number k small for (i = 0, j = 0; I < n && J < K; i + +) {if (A[i]!= a[i + 1]) { 
            if (j = = k-1) {printf ("%d\n", 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); 
   
    an 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 to left; 

 }

Play cards

Topic Requirements:

Topic Description:

The card is only 1 to 9, holding in hand has already arranged the order the card A, the other side card B, uses the procedure to judge whether the hand card can press over the other side to play the card.
Rule: There are 5 kinds of card type
[1] A sheet as 4 then 5 ... 9 can be pressed over
[2] Two if 44 55,66,77,...,99 can be pressed over
[3] Three sheets as 444 rule as [2]
[4] Four sheets as 4444 rule as [2]
[5] The five card type has only 12345 23456 34567 45678 567,895, and the rear is larger than the front.

Input:

Multiple sets of data are entered.
Enter two strings per group (no more than a string size) a,b. A string represents the hand, and the B string represents the card.

Output:

Press over output yes or No.

Sample input:

12233445566677
33

Sample output:

YES

Note:
started submitting 3 times, all WA, found that there is a test case I can not pass, but also the last code error where the test cases are as follows:
Sample input: 1122335566778899 (discontinuous) 12345
Sample output: Yes
AC Code:

  #include <stdio.h> #include <stdlib.h> #include <string.h> int main () {char a[1 
    01]; 
    Char b[101]; 
    char ch, key; 
    int I, Lena, LenB, flag; 
   
   
    int count[11]; 
      while (scanf ("%s", a)!= EOF) {//receive carriage return ch = getchar (); 
      Receive the card scanf ("%s", b); 
      ch = getchar (); 
      Length Lena = strlen (a); 
      LenB = strlen (b); 
      Initialize memset (count,0,sizeof (count)); 
      Traversal hand for (i = 0; I < Lena i + +) {Count[a[i]-' 0 '] + +; 
          //Check each other's card 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 & 
              amp;& Count[key + 3] > 0 && count[key + 4] > 0) {flag = 1; 
            Break 
      }} break; 
      ///printout if (flag) {printf ("yes\n"); 
      }else {printf ("no\n"); 
  } return 0; 
 }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.