1094 - I2P CS 2016 Chen midterm 2 Scoreboard

Time

2016/12/08 13:10:00 2016/12/08 15:10:00

Clarification

# Problem Asker Description Reply Replier Reply Time For all team

# Problem Pass Rate (passed user / total user)
11246 Cheat Sheet Chen Mid2
11247 Sorting
11248 Eating Group
11250 The Cost to Pass the Forest

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




11247 - Sorting   

Description

This is a partial judge problem , your mission is to read the partial judge code given below

and implement 2 functions: 

    1.void  sort(int a[],int len) 

     --> Sort the disorder integer array in ascending order. ※len = the length of the integer array

    2. void show_the_array(int a[],int len)

      --> Print out the sorted array , and follow the output format ※len = the length of the integer array

Please submit the implementation of these 2 function to NTHU OJ and choose C compiler . 

You have to use this partial judge code to implement the 2 function:

#include<stdio.h>
#include<stdlib.h>
void sort(int a[] , int len);
void show_the_array(int a[] , int len);

int main(void){
    int i,N;
    scanf("%d",&N);
    int *S = malloc(sizeof(int)*N);
    for(i=0;i<N;i++)scanf("%d",S+i);
    sort(S,N);
    show_the_array(S,N);
    return 0;
}

 

Input

The 1st line contains an integer N which indicate the length of the integer array.

The 2nd line contains N integers which are the elements in that array.

Output

Print out the sorted integer array in ascending order.

Note that :

1.There is a "space" between any 2 integers.

2.There is no need to add "\n" at the end of output.

3.There is no need to add "space" at the end of output.

Sample Input  Download

Sample Output  Download

Partial Judge Code

11247.c

Partial Judge Header

11247.h

Tags




Discuss




11248 - Eating Group   

Description

[Partial Judge: C]

There are N hungry people, numbered from 1 to N. Each of them is going to have a lunch with someone else or alone.

With the given input, try to find out that which restaurant to go for each person. Each restaurant is also represented by an integer.


 

Notice:

You need to implement all three functions defined in the 11248.h header file.

There are two arrays of length 100 (a,b) leaved for you to implement your method. That should be enough.

 

Read input hint:

int i, a;
scanf("%d" , &a);
for ( i=0; i<N-1; i++) {
    scanf(",%d" , &a);
    ...

}

Input

The input contains three lines.

First line only has a single number N (N<=100), representing the total number of hungry people.

Second line is a comma-seperated sequence of N integers. The ith integer indicates who will ith person eat with. 0 means unknown.

Third line is also a comma-seperated sequence of N integers. The ith integer indicates which restaurant will ith person eat at. 0 means unknown. No '\n' at the end of this line.

Note, ith entry in second line is zero iff ith entry in third line is non-zero.

Output

Output a line of N comma saperated integers with no trailing \n behind.

Each integer indicates the restaurant to eat for the corresponding person.

Sample Input  Download

Sample Output  Download

Partial Judge Code

11248.c

Partial Judge Header

11248.h

Tags




Discuss




11250 - The Cost to Pass the Forest   

Description

The forest is described as follows:

S: the start of the forest

T: the destination of the forest

#: the passway in the forest

*: the trees in the forest

Now please count the cost to pass through the forest.

Input

The first line of the input give two integer M N, representing the height and the width of the forest.

Then the following M*N arrays gives the description of the forest.

Note. 3<=M, N<=100

Output

Give the minimum cost(steps) to pass through the forest.

If the destination is unreachable, the cost will be 0.

Remember to print a '\n' at the end of the output.

Sample Input  Download

Sample Output  Download

Tags




Discuss