| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 11643 | I2P_CS_MID1_Cheatsheet |
|
| 12067 | CS_2018_MID1-1 |
|
| 12068 | CS_2018_MID1-2 |
|
| 12069 | CS_2018_MID1-3 |
|
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
%c char to print a character
%s char * string (character array ended with '\0')
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.
Input
Output
Sample Input Download
Sample Output Download
Tags
Discuss
Description
The input contains three numbers, which are the 2nd, the 3rd and the 4th number of an arithmetic progression (等差數列) or a geometric progression (等比數列). Your task is to distinguish which progression it is from this numbers and print out the first number and the 5th number.
For example, if the input is 3 5 7, then you know this is an arithmetic progression and the common difference is 2. So the 1st and 5th number are 1 and 9 respectively.
There is NO progression like 1 1 1 or 2 2 2 which are both an arithmetic progression and a geometric progression.
Input
Three integers
Output
The 1st and the 5th number of the progression. The two numbers should be separated by a blank.
You DO NOT need to print ‘\n’ at the end of the output.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Given a sequence of n integers, sort the integers in increasing order.
Find and print the largest difference between two neighboring integers in the sorted sequence.
For example, if the original sequence is 7 -3 2 5 12 8, the output should be 5.
Because the sorted sequence is -3, 2, 5, 7, 8, 12 and the largest difference is between -3 and 2, which is 5.
Input
The first line is an integer T (T <= 20) denoting the number of test cases.
Each test case consists of two lines. The first line contains a number n (n <= 104) indicating the length of the sequence, and the second line contains the sequence of n integers V1, V2, ..., Vn. (-2^31 < Vi < 2^31 - 1 for 1 <= i <= n )
Output
The output of each test case contains one integer ended by '\n'.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Given two strings A and B, find the longest symmetric substrings (palindrome) of both A and B.
For example, if A is "aabcbadd" and B is "eebcbe", the answer should be "bcb"
Note that there might be more than one longest symmetric substring of A and B.
In that case, you only need to print leftmost substring of A.
Also, note that there is at least one longest symmetric substring of A and B for each test case.
Input
The first two lines of the input are the strings A and B (0 < length of A and B <= 20).
Output
Print the longest symmetric substrings of A and B (leftmost substring of A)
You need to print ‘\n’ at the end of the output.