1101 - I2P CS 2016 Fall Lee Midterm 2 Scoreboard

Time

2016/12/12 15:40:00 2016/12/12 17:50:00

Clarification

# Problem Asker Description Reply Replier Reply Time For all team

# Problem Pass Rate (passed user / total user)
11246 Cheat Sheet Chen Mid2
11253 String Left Shift
11254 Array Sorting
11256 Square Tile Sticking - Partial

11246 - Cheat Sheet Chen Mid2   

Description

 printf() and  scanf() format

printf("%d", n);
 

FORMAT  ARGUMENT TYPE

%d, %i  int           decimal

%u      unsigned int

%x      unsigned int  hexadecimal

%#x     unsigned int  hexadecimal with prefix 0x

%f      double  

%Lf     long double

%e, %E  double      scientific notation

%c      int         to print a character

%s      char *      string (character array ended with '\0')

%p      void *      print memory address

%g, %G  double      %f or %e depending on the length


scanf("%d", &n);
 

FORMAT  ARGUMENT TYPE

%d      int *       &n, store the input integer in n

%ld     long *

%lld    long long *

%u      unsigned int *

%f      float *     read float

%lf     double *    read double

%Lf     long double *   read long double

%c      char *      read 3 characters %3c

%s      char *      read a string until whitespace

%n      int *       with %s, to get string length

                   char a[100]; int len;
                  scanf("%s%n", a, &len);
                  len
will have the string length

 

Frequently used functions

#include <string.h>
char str[10];
scanf("%s", str);
to get the string length using strlen(str)
to compare two strings strcmp(str1, str2) ==0 if equal
to compare the first n chars of two strings strncmp(str1, str2, n) ==0 if equal
to copy str2 to str1 strcpy(str1, str2)
to copy the first n chars of str2 to str1 strncpy(str1,str2, n) remember to add '\0' to str1
#include <ctype.h>
isspace(ch), islower(ch), isupper(ch), isdigit(ch)
isalpha(ch), toupper(ch), tolower(ch)

To create a 5-by-5 two-dimensional array, we need to write

int a[5][5];

 

It will be indexed as follows:

 

 a[0][0] 

 a[0][1]

 a[0][2]

 a[0][3]

 a[0][4]

 a[1][0]

 a[1][1]

 a[1][2]

 a[1][3]

 a[1][4]

 a[2][0]

 a[2][1]

 a[2][2]

 a[2][3]

 a[2][4]

 a[3][0]

 a[3][1]

 a[3][2]

 a[3][3]

 a[3][4]

 a[4][0]

 a[4][1]

 a[4][2]

 a[4][3]

 a[4][4]

 


How to read the following data?
1 2 3 4 5 e
#include <stdio.h>

int main(void)
{
    int x;
    while (scanf("%d", &x) == 1) {   
    
printf("x=%d\n", x);
    }
    return 0;
}

How to read the following data?

2

L 5 2
D 5 3

#include <stdio.h>

int main(void)

{

   char ch;
   int i, n, row, col;

   scanf("%d", &n);

   for (i=0; i<n; i++) {

      while(getchar()!='\n');

      scanf("%c%d%d", &ch, &row, &col);

   }

   return 0;

}

 

Using for loops to print a two-dimensional array

   for(i = 0; i < row; i++) {
      for (j = 0; j < col; j++) {
         printf("%5d", A[i][j]);
      }
      printf("\n");
   } 

Using bubble sort to rearrange an array A

for (i = 0; i < n; i++) {
   for (j = 1; j < n; j++) {
      if (A[j] > A[j-1]) {
         /* swap A[j] A[j-1] */
      }
   }
}

operators:
!   &&    ||    ==     !=    +    -    *    /    %
>   <     >=    <=

How to avoid common errors and how to debug for OJ

1. Put the arrays in the 'global' area. Set their size bigger than required. Avoid using variable-length arrays (e.g. int arr[n];). Keep the array size fix (e.g., int arr[1000];).

2. After writing the code for reading input data, you may print out the data to check if your code reads them correctly. Do not proceed to write subsequent code before you confirm that.

3. If your program crashes, usually it is caused by memory related errors. Check the ranges of for-loops to see if your code attempts to read or write the elements out of the arrays’ boundary.

*(a+i) is equivalent to a[i]
(a+i) is equivalent to &a[i]

qsort :

you have to include <stdlib.h>

usage :

void qsort (void *array, size_t count, size_t size, comparison_fn_t compare);

qsort an int array

int compare_int (const void *a, const void *b)
{
    const int *va = (const int *) a;
    const int *vb = (const int *) b;
    return *va-*vb;
}
 

qsort a double array

int compare_double (const void *a, const void *b)
{
    const double *da = (const double *) a;
    const double *db = (const double *) b;
    return (*da > *db) - (*da < *db);
}
 

Input

Output

Sample Input  Download

Sample Output  Download

Tags




Discuss




11253 - String Left Shift   

Description

Given a string S, you are going to shift part of it.
Specifically, a shift on a string is to move all the characters left but the leftmost characters would be move to the rightmost.
For example, a shift on "abcd" would result in "bcda".

This problem ask you implement a function shift(char *start, char *end) that shift a substring of S starting from start to end.

1.      This problem involves three files.

  • function.h: Function definition of shift.
  • function.c: Function implementation of shift.
  • main.c: A driver program to test your implementation.

You will be provided with main.c and function.h, and asked to implement function.c.

  • 2.     For OJ submission:

            Step 1. Submit only your function.c into the submission block. (Please choose c compiler) 

    Step 2. Check the results and debug your program if necessary.

Input

The first line is a string S. (length of S<=20)
The second is a integer n. (n<=10)
The next n lines, each contains two integers l and r.(l<=r<=n)
This means that a shift on S[l]S[l+1]...S[r].
If the length of substring is one(l=r), then you should keep the string unchanged.
All these shift must be done in order.

 

Output

The result string after all n shift operations.

Sample Input  Download

Sample Output  Download

Partial Judge Code

11253.c

Partial Judge Header

11253.h

Tags




Discuss




11254 - Array Sorting   

Description

Given a two-dimensional array of size R x 5 (1 < R < 100).

We want to sort the two dimensional array according to each column.

For example:

5

1

3

11

25

45

82

97

73

63

13

47

34

26

14

After sorted

5

1

3

11

14

13

47

34

26

25

45

82

97

73

63

Note that

1.      This problem involves three files.

  • function.h: Function definition of sortArray.
  • function.c: Function implementation of sortArray.
  • main.c: A driver program to test your implementation.

You will be provided with main.c and function.h, and asked to implement function.c.

2.     For OJ submission:

        Step 1. Submit only your function.c into the submission block. (Please choose c compiler) 

Step 2. Check the results and debug your program if necessary.

Hints:

function.h

main.c

Input

The first line has an integer N(1<=N<=5000), which means the number of test cases.

For each case, the first line has an integer R (1<R<100) represent the numbers of rows. The following R lines, each containing 5 integers, specify the elements of the two-dimensional array.

Output

Print out all elements of the sorted array row-by-row.

All of the integers in the same line are separated by a space and there is a '\n' at the end of each line. All of the arrays should be separated by a new line character (\n).

Sample Input  Download

Sample Output  Download

Partial Judge Code

11254.c

Partial Judge Header

11254.h

Tags




Discuss




11256 - Square Tile Sticking - Partial   

Description

You have infinite SQUARE ceramic tiles(方形磁磚) of some sizes and you want to stick them to fill a rectangle area, which may have been partially covered.  What will be the number of all the possible outcomes you can make to the empty space?

For example, if there is a 3-by-3 area that some regions have already been covered by some tiles as below:

Empty Empty Covered
Empty Empty Empty
Covered Empty Empty

and you have two kinds of tiles: 2-by-2 and 1-by-1, then there are 3 different outcomes you can make:(Let A* be the 2-by-2 tiles and B* be the 1-by-1 tiles)

A1 A1     Covered
A1 A1     B1
Covered B2 B3

 

B1 B2     Covered
B3 A1 A1
Covered A1 A1

 

B1 B2     Covered
B3 B4 B5
Covered B6 B7

Input

The first line provides two integers, M and N, which are larger than or equal to 1 and less than 8, indicating the rectangle area that will be filled.

The second line provides an integer, K,  that is larger than 1 and less than 5, indicating the amount of types of tiles.  There will be ONE integer in each of the following K lines, indicating the sizes of the side of given square tiles.  The K numbers are all different.

The following M lines, which have N bits in each line to indicate the unit area is filled or not.  1 denotes that the unit area is already filled0 denotes that it is empty.  Every bit is separated by a space.

Note: There will always be some empty spaces to be filled.

Output

You should output the number of all the possible outcome that can be made.  Note that reflection and rotation is NOT considered.  That's why the output of the example is 3 instead of 2(the case that rotation does not count).

Reference Structure

Note: It is absolutely fine if you don't follow this template.  

#include"function.h"

int isDone(void){
        for (int i = 0; i < M; i++){
                for (int j = 0; j < N; j++){
                        if(area[i][j] == 0) return 0;
                }
        }
        return 1;
}

int isStickable(int k, int y, int x){   
    if sticking tile k would be out of the broader then say no

    for the area covered by this tile {

        /*is this space previously filled?*/
        if some area not empty say no
    }
    say yes
}

void stick(int k, int y, int x){    
    for the area covered by this 
tile {
            fill the space
    }
}
void remove(int k, int y, int x){    
    for the area covered by this 
tile {
            empty the space
    }
}

void getnext(int *nexty, int *nextx){
        for (int i = 0; i < M; i++){
                for (int j = 0; j < N; j++){
                        if(area[i][j] == 0){
                                *nexty = i;
                                *nextx = j;
                                return;
                        }
                }
        }
}

int count(int y, int x){
    
    int ret = 0;
    int nexty, nextx;

    /*check the ending condition*/
    if(isDone())
        return 1;
    
    for all kinds of tiles {
        /*combine above functions here*/
    }

    return ret;
}

Note: If you don't like the prototype of function count(int y, int x) defined in the header file, you may implement your idea like this:

int my_better_idea(int arg1, int arg2, ...){
    ...
    my_better_idea(arg3, arg4, ...);
    ...

    return result;
}

int count(int y, int x){
    ...
     return my_better_idea(a, b, ...);
}

Sample Input  Download

Sample Output  Download

Partial Judge Code

11256.c

Partial Judge Header

11256.h

Tags




Discuss