1558 - I2P (I) 2018_Chen_Lab6 Scoreboard

Time

2018/12/20 13:20:00 2018/12/20 15:20:00

Clarification

# Problem Asker Description Reply Replier Reply Time For all team

# Problem Pass Rate (passed user / total user)
10851 Equivalent relation (Find maximum)
11728 Distance on 2D plane
12093 Sorting 2D array

10851 - Equivalent relation (Find maximum)   

Description

There are N integer pointers, indexed from 0 to N-1 (N<100). Each pointer initially points to an integer of value 0.

 

There are three kinds of instructions.
1. “S n k” : n is the index of the pointer, and k is an integer value; this operation is to assign the value k to the integer pointed by pointer n; after this operation, the integer pointed by pointer n will have a value k.
2. “M n k”: n is the index of the pointer, and k is an integer value; this operation is to multiply the integer pointed by pointer n by k times; after this operaiton, the integer pointed by pointer n will be k times larger.
3. “P n m”: n and m are the indexes of two pointers; this operation is to make pointer n point to the same integer as pointer m; after this operation, m and n will point at the same address.

 

After some manipulation following the given instructions, we want to find out the maximum of what pointer array points to in some specific range, which is also given by the input.

Note that

1.      This problem involves three files.

  • function.h: Function definition of execInst, findMax.
  • function.c: Function describe of execInst, findMax.
  • 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:

main.c

#include <stdio.h>
#include "function.h"

#define SIZE 100

int main() {
    int *ptrArr[SIZE];
    int dataArr[SIZE] = {0};
    char inst;
    int dataNum, instNum;
    int param1, param2;
    int start, end;
    int i;

    /* input */
    scanf("%d %d", &dataNum, &instNum);

    /* initialize the ptrArr */
    for (i = 0; i < dataNum; i++)
        ptrArr[i] = &dataArr[i];

    for (i = 0; i < instNum; i++) {
        scanf(" %c %d %d", &inst, &param1, &param2);
        execInst(ptrArr, inst, param1, param2);
    }
    scanf("%d %d", &start, &end);
    /* output */
    for (i = 0; i < dataNum - 1; i++) {
        printf("%d ", dataArr[i]);
    }
    printf("%d\n", dataArr[i]);
    for (i = 0; i < dataNum - 1; i++) {
        printf("%d ", *ptrArr[i]);
    }
    printf("%d\n", *ptrArr[i]);


    printf("%d\n", findMax(ptrArr , start, end));

    return 0;
}

function.h

#ifndef FUNCTION_H
#define FUNCTION_H

void execInst(int *ptrArr[], char inst, int param1, int param2);

int findMax(int *ptrArr[], int start, int end);

#endif

function.c

#include "function.h"

void execInst(int *ptrArr[], char inst, int param1, int param2){
    if(inst=='S'){
        /--your code--/
    }
    else if(inst=='M'){
        /--your code--/
    }
    else if(inst=='P'){
        /--your code--/
    }
}

int findMax(int *ptrArr[], int start, int end){
        /--your code--/
    return max;
}

 

Input

The first line contains two positive X and Y. X indicates the size of data. Y indicates that there are Y instructions needed to be done.

The next Y lines contain the instructions.

The last line contain two integers, start and end respectively.

Output

First line: All the values that dataArr[0] to dataArr[N-1] stored in order. Each value is seperated by a blank ' '.

Second line: All the values that ptrArr[0] to ptrArr[N-1] point to in order. Each value is seperated by a blank ' '.

Final line: The max value that ptrArr[start] to ptrArr[end] point to.

Sample Input  Download

Sample Output  Download

Partial Judge Code

10851.c

Partial Judge Header

10851.h

Tags




Discuss




11728 - Distance on 2D plane   

Description

TA knows that in Monday lecture, HT taught everyone the concept of structure.

Moreover, the hackthon will be held on Saturday, and you students will use lots of APIs, containing many usages of structure.

Therefore, TA wants to ensure that you have enough basic knowledge of structure through this problem. 

 

In this problem, TA forces you to use structure to record imformation and use afterward.

You will be given several points' locations on 2D plane(x, y), and you should compute the Euclidean distance of specific two points.

(Hint: you can use sqrt() to calculate the square root)

 

You should implement funcion.c below and submit it. (Due to Partial judge)

#include <stdio.h>
#include <math.h>
#include "function.h"

Point * ones_vec_1(int length)
{
   /// Please implement
   /// 1. Malloc memory for point array
   /// 2. Read values into point array
   /// 3. Return the address of the first element in the array

}

float compute_distance(Point* a, int first_id, int second_id)
{
    float ans;
    Point first_p, second_p;

    /// Please implement
    /// 1. Get two point from function argument
    /// 2. Compute 2D distance and return ans

    return ans;
}

 

You should see .c and .h file below to know how the function and the whole program works.

Input

Input may have several lines.

The first line contains an integer M, indicating how many points' locations you should read.

(Notice that the first point is indexed 0, and the second point is indexed 1, and so on...)

And the next M lines , each line has two integers, indicating point's location x and y.

 

The next line contains an integer N, indicating the number of the pairs of chosen two points.

And the next N lines, each line contains two integers, which are two indices representing two specific points needed to be calculated for Euclidean distance.

 

Output

Ouput has N lines.

Each line contains a floating number, indicating the Euclidan distance of two specific points.

Sample Input  Download

Sample Output  Download

Partial Judge Code

11728.c

Partial Judge Header

11728.h

Tags




Discuss




12093 - Sorting 2D array   

Description

Today, TA wants to sort 2d array using qsort, yet he doesn't know how to complete the compare function. Therefore, he asks for student's help.

In this problem, we need to sort 2d array under situation that:

       1. The seond dimension's size is fixed to 2. (2d array's shape is always fixed to n by 2. Ex. array[5][2] , array[7][2]) 

       2. We consider each row (has two columns) to be individual and sort them. Ex. If we want to sort array in size 4 x 2, we divide array into 4 rows: array[0], array[1], array[2], array[3] (each has 2 elements) and sort these 4 rows. 

       3. When we sort 2d array and compare any two rows, we compare first element at first, and then compare second element, all in ascending order (遞增).

 

Ex. After sorting, {1, 2} is in front of {2, 1} (because first element of {1, 2} is 1, smaller than first element of {2, 1} = 2).

After sorting, {1, 2} is in back of {1, 1} (because first element of two arrays is equal = 1, but second element of {1, 2} is larger than second element of {1, 1} = 1).

 

Note: This is a partial judge problem. We have already handled the input and output for you.

All you have to do is to implement the function "compare".

#include <stdio.h>
#include <stdlib.h>

int compare(const void* pa, const void* pb)
{
    // do comparison 
    
}

 

Input

The first line contains an integer N, representing how many rows of 2d array you should read. (2 <= N <= 100)

The following N lines, each line contains two integers which is two elements of that row in 2d array. 

We have already handled the input for you.

Output

Please print the result of 2d array sorted by rules we establish above. 

We have already handled the output for you.

Sample Input  Download

Sample Output  Download

Partial Judge Code

12093.c

Partial Judge Header

12093.h

Tags




Discuss