| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 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, ¶m1, ¶m2);
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.