| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 11739 | cheat_sheet |
|
| 12023 | Fill water |
|
| 12073 | character replacement |
|
| 12077 | Nice permutation |
|
| 12078 | Hurry! |
|
Description
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]
Input
Output
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Suppose that you have an infinite number of containers with different sizes, already filled with water. Given another empty container with size N liters, you need to find out all the possible methods to fill this N-liter empty container using the provided smaller containers. Note that, to avoid water wastage, if you choose to use a container, all the water in this container should be poured.
For example, assume that we have containers in 1, 5, and 10 liters. To get the 17 liters of water, all the possible methods are listed below:
- 1 container of 10 liters, 1 container of 5 liters, and 2 containers of 1 liter.
- 1 container of 10 liters, 0 containers of 5 liters, and 7 containers of 1 liter.
- 0 containers of 10 liters, 3 containers of 5 liters, and 2 containers of 1 liter.
- 0 containers of 10 liters, 2 containers of 5 liters, and 7 containers of 1 liter.
- 0 containers of 10 liters, 1 container of 5 liters, and 12 containers of 1 liter.
- 0 containers of 10 liters, 0 containers of 5 liters, and 17 containers of 1 liter.
You will be provided with the following sample code, and asked to implement function "filling".
#include <stdio.h>
#define MAXNC 5
int liters[MAXNC];
int solution[MAXNC];
void showResult(int n) {
int i;
printf("(%d", solution[0]);
for (i=1; i<n ;i++) {
printf(",%d", solution[i]);
}
printf(")\n");
return;
}
void filling(int amount, int cur, int n) {
/*Your code*/
}
int main(void) {
int n, i;
int water;
scanf("%d", &n);
for (i=0; i<n ;i++) {
scanf("%d", &liters[i]);
}
scanf("%d", &water);
filling(water, 0, n);
return 0;
}
Input
The input contains three lines.
The first line contains an integer M (0<M<=5) which represents the number of different size containers.
The second line contains M numbers, S1 ,S2 , …, SM. Si represents the size of the i-th container. Note that these M numbers are in decreasing order.
The third line contains an integer N (0<N<100) which represents the size of the empty container.
Output
Display all the possible methods to fill the empty container. Each line represents a method in the format “(C1,C2,…,CM)”, where Cirepresents the number of the size Si container.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
There will be some sample code for this problem .
The first line contains a string s.
There will be only lowercase Latin characters in s.
The second line will be an integer T, which is the change time. And T< 107.
For the next T line , each line will contains two character x, y , which means for any x in s should be replacement as y .
- function.h: Function definition of changeCharacter.
- function.c: Function describe of changeCharacter.
- main.c: A driver program to test your implementation.
Input
| s | < 10000
x, y will be lowercase Latin characters
For the testcase one , there is no change to original string s.
If you get any Runtime error in testcase one, think twice what's wrong with your pointer :D .
Output
In the end , output the string s one time !
'\n' at the end of output.Sample Input Download
Sample Output Download
Partial Judge Code
12073.cPartial Judge Header
12073.hTags
Discuss
Description
Permutation EX (Hard Version)
Given N digits, we can write them down one by one (in some order) to form a number, say X.
For example, given 1 0 2 3 1, we can form a number 11032.
What are the all possible X such that X can be divided by a positive integer M?
Note that X cannot start with 0. That is, 00123 is not a valid number.
Input
First line contains a number T, indicating that there are T questions follow.
Each question consists of 2 lines. In the first line, there are two positive number N and M, described in Problem Description. In the second line, there are N digits (0~9), each of them seperated by a space.
It is guaranteed that
- T <= 10
- Testcase 1
- N < 10, M = 1, digits does not contain 0 and there is no duplicate digit in X.
- Testcase 2
- N < 10, M = 1, digits does not contain 0
- Testcase 3 & 4
- N < 10, M < 10
- Testcase 5
- N < 100, M < 106, total number of possible permutation of those N digits won't exceed 108
- Hint: think about how to store the numbers
Output
For each question, print out all number formed by those N digits such that it can be divided by M, in ascending order.
Print out an empty line at the end of each question.
For more information, please refer to Sample Input & Sample Output.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Ben is hurrying for the restroom!
As you might know, men have a habit when choosing which urinal (小便斗) to use: choose the one from other people as far as possible.
Let's define this habit more formally.
- Considering the i-th urinal (index increases from left to right), define Li and Ri as
- Li: the number of urinals between the rightmost people in the left of the i-th urinal and the i-th urinal;
- Ri: the number of urinals between the leftmost people in the right of the i-th urinal and the i-th urinal.

- By the way, there are always 2 security guards in the leftmost and rightmost of the restroom (not occupying any urinal) for some unknown security reason. They will also be treated as people mentioned in the above definition when determining distances.

- When Ben arrives at the restroom, he will pick the i-th urinal such that min(Li, Ri) is the maximum among all available urinals. If there exist multiple available choices, then he will pick the one with the smallest i (the leftmost one).
Let's give an example to demonstrate how everything's going:
-1234567- (index of urinal)
| i | Li | Ri |
| 1 | 0 | 2 |
| 2 | 1 | 1 |
| 3 | 2 | 0 |
| 4 | - | - |
| 5 | 0 | 2 |
| 6 | 1 | 1 |
| 7 | 2 | 0 |
So for the man who just arrives at the restroom, he will choose the 2nd urinal, making the status become
G0101000G
And now the question is:
If Ben is the k-th man arriving at the restroom and suppose no one ever leaves his urinal, which urinal will Ben choose?
Input
Input consists of multiple testcases. There is an integer T in the first line, indicating there are T testcases.
Each testcase consists of 1 line, containing 2 integers N and K, where N is the total number of urinals, and K means Ben is the K-th man arriving at the restroom.
It is guaranteed that
- T <= 10
- K <= N <= 3000
Hint
1. No recursion. Loop is enough!
2. Testcase
- Testcase1: T = 1, K = 1
- Testcase2: T <= 10, K = 1
Output
For each testcase, print out the index of the urinal that Ben will choose.
(Index starts from 1, and increases from left to right.)