| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 11267 | Equivalent relation |
|
| 11268 | I2P(I)2016_Yang_hw6-2 |
|
| 11269 | Text Editor |
|
| 11275 | Sentence Reversal |
|
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
Given a pointer integer array **ptr with size N, and an integer array *array with size (N+1)*N/2. Please use malloc function to allocate memory to **ptr and *array. The *array is an ascending sequence of number. Each pointer in array **ptr shall point to one element of *array. And the elements pointed by the **ptr are also ascending.
For example, when n = 5, the size of **ptr will be 5, and the size of *array will be 15. The first pointer of **ptr is *ptr[0] which points to array[0], and the second pointer of **ptr is *ptr[1] which points to array[1], and the third pointer of **ptr is *ptr[2] which points to array[3], and the third pointer of **ptr is *ptr[3] which points to array[6].
main.c
#include <stdio.h>
#include <stdlib.h>
#include "function.h"
int main() {
int *array;
int **ptr;
int N;
int offset;
scanf("%d %d",&N, &offset);
malloc_array(&array, (1+N)*N/2);
ptr = malloc_ptr(N);
int i;
for(i = 0; i < (1+N)*N/2; i++){
array[i] = i;
}
pointer_ptr_to_array(array,ptr,N);
for(i = 0; i < N; i++){
printf("%d\n",*(ptr[i]+(offset)));
}
free(ptr);
free(array);
return 0;
}
function.h
int** malloc_ptr(int array_size); void malloc_array(int **array, int array_size); void pointer_ptr_to_array(int *array, int **ptr,int N);
Input
The first line is size of **ptr
The second line is offset
offset < size <10000
Output
Print each pointer of **ptr + offset
Note that you need to print a newline character ‘\n’ after each number, that is, just one number will be shown in each line of the output.
Sample Input Download
Sample Output Download
Partial Judge Code
11268.cPartial Judge Header
11268.hTags
Discuss
Description
In this problem we simulate a simple text editor. Given a series of keyboard input, output the final text content.
The text editing rules are defined as following:
1. Normal alphabetical input and whitespace input (abcdefg…. and ‘ ‘) directly write after the cursor of the text content.
And four special commands started with a backslash(/) character
2. The backspace command which deletes a letter before the cursor (/backspace)
3. The newline command which creates a new line after the cursor (/newline)
4. The two navigating commands which move the cursor (/left /right)
The size of the text content is fixed to 500 characters, and the text content of testcases will not exceed 500 characters when simulating.
Use fgets(). (https://www.dummies.com/programming/c/how-to-use-the-fgets-function-for-text-input-in-c-programming/)
Hint:
#include <stdio.h>
#define MAX_SIZE 500
char content[MAX_SIZE];
char input[MAX_SIZE];
int main()
{
fgets(input, MAX_SIZE, stdin);
/* your code here */
printf("%s", content);
return 0;
}
Input
The keyboard input sequence.
There is always a valid command(/backspace /newline /left /right) right after the backslash character.
There is no newline character at the end
Output
The final text content.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
In this problem, you are given several sentences, and you have to print the reversal of each of them.
A reversal of a sentence is the reversed order of words in the original sentence. For example, for the input string "this is a book", the result should be "book a is this".
Hint 1: we suggest you to do the task by a recursive function. The function seeks the first word from current location, and call itself recursively. Before each return, the function will print the word it found. Then all words will be printed in the reversed order.
The function prototype is something like this:
void sentence_reversal( char *now ) ; // which seek the first word from *now, and pass an new *now location to the called function
And the recursion is like the following figure:
this (4th printed word)
-> is (3rd printed word)
-> a (2nd printed word)
-> book (1st printed word)
Hint 2: Print the spaces between words, and there should be no blanks (spaces) at the begin or the end of new sentences. If the original sentence has only one word, print the word directly without any spaces.
Note: Another method to solve this problem is seeking words from the end of the original sentence. But we strongly recommend you to try the recursive way as a practice.
Hint 3: Use fgets(). (https://www.dummies.com/programming/c/how-to-use-the-fgets-function-for-text-input-in-c-programming/)
Input
There are several lines of input, and each line presents a sentence.
No lines are longer than 500 characters, and there will be no empty lines. At most 10 sentences will appear in a test case.
Words in sentences are separated by a space (' '). There will be no continuous spaces, or any spaces at the beginning or end of the sentences.
Output
Print out each reversal sentence. All words in a reversal sentence are separated by a space, and there is a '\n' at the end of each line.