| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 10915 | cheatsheet |
|
| 12203 | Prefix to truth table 2 |
|
| 12204 | Binary Search Tree Operation 2 |
|
| 12210 | rotate linked list |
|
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)
#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:
|
a[0][0] |
a[0][1] |
a[0][2] |
a[0][3] |
a[0][4] |
|
a[1][0] |
a[1][1] |
a[1][2] |
a[1][3] |
a[1][4] |
|
a[2][0] |
a[2][1] |
a[2][2] |
a[2][3] |
a[2][4] |
|
a[3][0] |
a[3][1] |
a[3][2] |
a[3][3] |
a[3][4] |
|
a[4][0] |
a[4][1] |
a[4][2] |
a[4][3] |
a[4][4] |
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.
#include <string.h>
strstr(str1,str2)
return a pointer to the first occurrence in str1 of the entire sequence of characters specified in str2, or a null pointer if the sequence is not present in str1.
strcmp(str1,str2)
Compare two strings.
return
|
|
if the first character that does not match has a lower value in ptr1 than in ptr2 |
|
|
if the contents of both strings are equal |
|
|
if the first character that does not match has a greater value in ptr1 than in ptr2 |
strncmp(str1,str2,num)
Compares up to num characters of the C string str1 to those of the C string str2
return
|
|
if the first character that does not match has a lower value in str1 than in str2 |
|
|
if the contents of both strings are equal |
|
|
if the first character that does not match has a greater value in str1 than in str2 |
#include <stdlib.h>
malloc(size)
return a pointer to the memory block allocated by the function.
Input
Output
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Give a prefix Boolean expression, which only has at most 4 variables ‘A’, ‘B’, ‘C’, and ‘D’, and 2 operators, AND ‘&’ and OR ‘|’, print its truth table which output equal to 1.
For example, if input is "&|AB|CA", then result will be
A B C D output
0 1 1 0 1
0 1 1 1 1
1 0 0 0 1
1 0 0 1 1
1 0 1 0 1
1 0 1 1 1
1 1 0 0 1
1 1 0 1 1
1 1 1 0 1
1 1 1 1 1
Input
The input contains a sequences of prefix expression. It only has at most 4 variables ‘A’, ‘B’, ‘C’, and ‘D’, and 2 operators, AND ‘&’ and OR ‘|’. The length of prefix expression is shorter than 30.
Output
It has 4 variables 'A', 'B', 'C', 'D', and output, each variable is separated by a space.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
The problem will ask you to create a binary search tree, and there will be 3 kinds of commands to complete.
1. P : please print out the binary search tree in in-order form in a line. There is an whitespace between each number, also after the last number.
2. GetMax : print out the maximum height of the binary search tree. (There need no space after output number)
3. AverLevel [input] : print out the average value of the nodes at the input level in the line, please show the answer only 3 digits after decimal point, if the input level is bigger than the maximum height, please print out 0.000. You can simply use %.3f for printf to display the answer. (There need no space after output number)
* the root level will represent as 1
Input
The first line contains an integer N , which indicates the number of nodes of the binary search tree.
The second line is the data for create binary search tree. (It's meaning insert the elements into tree by input order)
The third line contains an integer M , which indicates the number of commands.
Following M line will be the input instruction as problem description.
- 1 < N <= 1000
- 1 < M <= 1500
- No same value when create the tree.
- For AverLevel , 0 < input <= 1000.
Output
Print the answer for each input instruction in a line. it's always a newline in the end of the output.
Only P command should left whitespace after each output value. The other commands DO NOT output whitespace.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Given a link list structure named Node.
typedef struct _Node {
int data;
struct _Node *next;
} Node;
Given a list, rotate the list to the left by k places, where k is non-negative and k is smaller than the count of nodes in linked list.
For example:
Given 1->2->3->4->5->NULL and k = 3,
return 4->5->1->2->3->NULL.
Input
The input contains 2 sequence of positive integers.The first sequence is to create a linked list of integers, except the last one, which is -1, indicating the end of the sequence. The second line is an integer k.
Output
The output contains the sequence of resulting linklist.
You will not need to worry about the format of the output.