| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 11804 | cheat sheet |
|
| 12104 | Pour Water (8 Directions) |
|
| 12105 | Small Cat Society |
|
| 12108 | Laser Beam |
|
| 12109 | Text Editor 2 |
|
| 12111 | sing song |
|
Description
Cheat Sheet
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:
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
Description
A farmer has a crop land. The land consists of stones (S) and holes (H) as illustrated in the following figure. Note that there is only one hole on the land’s first row.
|
S |
S |
H |
S |
S |
|
H |
H |
H |
H |
S |
|
H |
S |
S |
S |
S |
|
S |
H |
S |
H |
H |
|
S |
H |
S |
S |
S |
The farmer decides to water his crops, so he pours some water through the hole on the first row into the land. Assume that the amount of water is infinite, and the water can move in the eight directions: up, down, left, right, upper-left, upper-right, lower-left, lower-right. Please write a program to find out where the water would reach.
For example, you are given a coordinate (0,2) representing the entrance and a table representing the farmer’s land initially:
|
S |
S |
H |
S |
S |
|
H |
H |
H |
H |
S |
|
H |
S |
S |
S |
S |
|
S |
H |
S |
H |
H |
|
S |
H |
S |
S |
S |
After the farmer pours water into the land, water floods throughout the holes of the land. Finally, a portion of the holes will be filled with water like the following table.
|
S |
S |
W |
S |
S |
|
W |
W |
W |
W |
S |
|
W |
S |
S |
S |
S |
|
S |
W |
S |
H |
H |
|
S |
W |
S |
S |
S |
where W means the corresponding hole is filled with water.
Note that
1. This problem involves three files.
- function.h: Function definition of flooding.
- function.c: Function implementation of flooding.
- 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:
You can use just 4-direction to pass 6 testcases.
function.h
main.c
Input
The first line has an integer N(1<=N<=100), which means the number of test cases.
For each case, the first line has three integers. The first and second integers R and C (3<=R, C<=500) represent the numbers of rows and columns of the land, respectively. The total number of elements in the land is thus R x C. The third integer X (0<=X<=C-1) means the column of the entrance at the first row. The coordinate of the entrance is thus (0,X). The following R lines, each containing C characters, specify the elements of the farmer’s land.
Output
Print out all elements of the lands row-by-row, and there is a '\n' at the end of each line. The states of all lands should be separated by a new line character (\n).
Sample Input Download
Sample Output Download
Partial Judge Code
12104.cPartial Judge Header
12104.hTags
Discuss
Description
Wild cats take care of each other in the wild. However, when winter comes, the preys are not enough to feed all the cats.
In this problem, you are asked to help decide who can enjoy the precious food for three different categories of cats: apprentice, elder, and kitty.
First, the cats dine according to the following order of their occupations:
1. elder
2. kitty
3. apprentice
Then, except that the apprentices have the dining priority of the young over the old, for the other two occupations, the old have higher priority. If the occupations and the ages of two or more cats are the same, they will dine in lexicographic order according to their names.
Input
There are multiple test cases.
The first line of each test case contains two integers N and M, indicating the number of cats and the portions of food respectively, where 0<N,M<=10000.
The next N lines are the information of each cat, including name, occupation, and age.
The length of the names will not exceed 30 letters and will contain no spaces.
Output
Please output the cats that could eat the food in order, each name a line.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Consider a room of size H*W (3<=H < 10000,3<=W < 10000) in which its four walls are covered by mirrors. You need to simulate the laser beam reflection in the room and print out the k-th ( 0 < k < 10000)reflection point.
We assume that the laser beam is always emitted from the left mirror and moves in the upper-right direction at an angle of 45 degrees. More specifically, the starting point can be represented by (r, 1) where 1<r<H. The laser beam will stop if it hits any corner of the room. Also, each point can only reflect the laser beam once. That is, the second time the laser beam hits the same point, the process of reflection stops.
For example, if the room size is 5*5 and the laser beam starts at (3,1), then the first point it hits is (3,1), the second point is (1,3), the third point is (3,5), and so on. If k=3 you need to print out (3,5).
If the laser beam stop before the k-th reflection, you need to print out coordinate of the stop point. For example, if k =10 and the first point is (3,1), you need to print out (3,1) because the laser beam stops at (3,1).
Input
The first line is the height and width of the room.
The second line is the starting point (the first reflection point).
The third line is k, which means you need to print out the k-th reflection point.
Output
The coordinate of the k-th reflection point.
Note that you DO NOT need to print a newline character ‘\n’ at the end of the output.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
In this problem we simulate a simple text editor. Given two lines of keyboard input texts and commands, output the final text content.
The text editing rules are defined as following:
1.
Normal alphabetical input and whitespace input (abcdefg…. and ‘ ‘) in the first line.
And a series of two special commands started with a backslash(/) character in the second line.
2.
The backspace command which deletes a letter before the cursor (/b)
3.
The navigating command which move the cursor to the right(/r)
4.
The cursor is at the beginning of the text initially.
For example, for the following input:
The quick brown fox jumps over the lazy dog
/b/r/r/r/b
You should first store the "The quick brown fox jumps over the lazy dog" in the input char array and set cursor=0.
Then you can simulate the operation of the text editor.
The size of the text content is less than or equal to 500 characters, and there will be less than or equal to 500 commands when simulating.
The C library function char *gets(char *str) reads a line from stdin and stores it into the string pointed to by str. It stops when either the newline character is read or when the end-of-file is reached, whichever comes first.
Hint:
#include <stdio.h>
#define MAX_SIZE 1001
char content[MAX_SIZE];
char input[MAX_SIZE];
char command[MAX_SIZE];
int main()
{
gets(input);
gets(command);
/* your code here */
printf("%s", content);
return 0;
}
Input
The first line is the text content which should be edited.
The second line is the commands.
There is always a valid command(/b /r) right after the backslash character.
Output
The final text content, and there should be no '\n' at the end of the output.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Bonus problem for 2018 Yang Final Exam
There was a lonely singer called Robert who ordered a beer and wanted to write a new song.
While he writes his song, he often changes the lyrics.
So he asks you to write a program for him.
There are 4 commands:
1. "w" : write the new line in the nth sentence.
2. "r" : print out all the lyrics in the newest version.
3. "a" : append the string after the nth sentence (if the nth sentence is blank, then create it.)
4. "s": swap two sentences
Hint:
Input
The first line contains 1 number N, which means the number of instructions.
Each instruction will be one of the following:
- w x string: write string at the sentence on x-th line (remove the old data in x-th sentence)
- a x string: append string at the end of the sentence on x-th line (the sentence might be empty)
- r: print out all sentences whose length is > 0 (from sentence on first line to sentence on last line, skip those sentence whose length is 0)
- s a b: swap the sentence on a-th line and the sentence on b-th line (they might be empty.)
It's guaranteed that
1. 0 <= x < 10000
2. the length of the "string" in each instruction < 50
3. the total length of each sentence might > 50, because of append
4. N <= 1,000,000
Output
If there's a read instruction, output all the setence have so far.
if there's no any words in sentence , do not output anything , even a "\n".