| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 11267 | Equivalent relation |
|
| 12490 | Little Brick's Calculator |
|
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 two kinds of instructions. The instruction “S n k” is used to set the integer, which pointer n points to, to be k. For example, S 1 10 means that the integer that pointer 1 points to is set to 10. And the instruction “P n m” means that pointer n points to the integer that pointer m points. For example, P 2 1 means that pointer 2 points to the integer that pointer 1 points to. After P 2 1, pointer 2 and pointer 1 point to the same integer, which is pointed by pointer 1.
Note that you don't have to change all the pointers if one pointer changes its target. The following table is an example. The instructions are P 1 2 and then P 2 3. You do not have to change the target of pointer 1.
|
instruction |
Description |
|
P 1 2 |
Pointer 1 points to the integer that pointer 2 points to.
|
|
P 2 3 |
Pointer 2 points to the integer that pointer 3 points to. And you don’t have to change the target of pointer 1. |
Finally, output all the values that pointers 0 to N-1 point to in order.
Note that
1. This problem involves three files.
- function.h: Function definition of execInstruct.
- function.c: Function describe of execInstruct.
- 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 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);
}
/* output */
for (i = 0; i < dataNum - 1; i++) {
printf("%d ", *ptrArr[i]);
}
printf("%d", *ptrArr[i]);
return 0;
}
function.h
#ifndef FUNCTION_H
#define FUNCTION_H
void execInst(int *ptrArr[], char inst, int param1, int param2);
#endif
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.
Output
All the values that pointers 0 to pointer N-1 point to in order. Each value is seperated by a blank ' '.
# Note that there is no '\n' at the end of the output.
Sample Input Download
Sample Output Download
Partial Judge Code
11267.cPartial Judge Header
11267.hTags
Discuss
Description
Little Bricks(小磚頭) has a very brilliant calculator,
it can parse the number from the a sentense and sum up all the numbers in the sentense.
One day, he broke the calculator,
but the calculator cannot be bought anymore,
so he ask you to make a new one for him.
ouo.
Hint:
This is a Partial Judge Problem:
0. You will be provided 2 files below: 'main.c', 'function.h'. You should only upload your 'solver' function inside a your '.c' file.
1. The 'main.c' file contains input, output, and function call, the 'function.h' file contains the defination of 'solver' function, and your '.c' file should contain the implement of 'solver' function.
2. You can compile multiple file by command, ex: 'gcc main.c function.h your_code.c', or create a project in your IDE.
3. Remember to include 'function.h'.
main.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <stdio.h> #include "function.h" #define maxn 1000 char input[1000010]; int main() { int sum = 0; int a[maxn]; int *ptr[maxn]; for (int i = 0; i < maxn; i++) { a[i] = 0; ptr[i] = &a[i]; } scanf("%s", input); int n = solver(ptr, &sum, input); printf("%d", a[0]); for (int i = 1; i < n; i++) printf(" %d", a[i]); printf("\n%d\n", sum); return 0; } |
function.h
1 2 3 4 5 |
#ifndef FUNCTION_H #define FUNCTION_H int solver(int **ptr, int *sum, char *s); #endif |
For example, if the sentense is 'Now:12/31,23:59',
then you should parse the 4 numbers: 12, 31, 23, 59 out,
and calculate the sum of these numbers, which is 12+31+23+59=129
The numbers should be separate by a space, after a newline character, output the sum of numbers.
Note that your calculator should be able to handle negative number.
Input
Input contain only 1 line, a string S.
It is guarantee that:
0. 1<= |S| <= 10^6
1. Numbers in S will be in the range [-10^5, 10^5]
2. The ammount of number in S will be in the range [1, 1000]
Output
Output contains 2 lines.
The first line should output all numbers appear in the input string S, separate with a space character,
the next line should be the sum of numbers at the first line.