| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 11246 | Cheat Sheet Chen Mid2 |
|
| 11257 | Segment Sorting |
|
| 11258 | Pour Water |
|
| 11260 | Find Palindrom |
|
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:
|
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.
*(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
Given m (m<=5) segments, each of which contains n (n<=10) integers.
You are asked to sort each segment individually in increasing order.
For example:
Given m = 3, n = 4 and the segments
[ 1 9 3 4 ] [ 7 11 6 2 ] [ 12 8 5 10 ]
the result after the sorting should be:
[ 1 3 4 9 ] [ 2 6 7 11 ] [ 5 8 10 12 ]
and therefore you should print:
1 3 4 9 2 6 7 11 5 8 10 12
Input
The first line contains two integers m (m<=5) and n (n<=10),
where m means the number of segments
and n means the size of each segment.
The second line contains n*m integers seperated by a space.
Output
You need to print the final sequence.
There should be one space between two adjacent integers.
Note that you DO NOT need to print a space(' ') or a newline (‘\n’) at the end of the line.
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 |
S |
H |
H |
H |
|
H |
H |
H |
S |
S |
|
H |
H |
S |
H |
H |
|
H |
S |
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 four directions: up, down, left, 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 |
S |
H |
H |
H |
|
H |
H |
H |
S |
S |
|
H |
H |
S |
H |
H |
|
H |
S |
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 |
S |
W |
W |
W |
|
W |
W |
W |
S |
S |
|
W |
W |
S |
H |
H |
|
W |
S |
S |
S |
S |
where W means the corresponding hole is filled with water.
You will be provided with the following sample code, and asked to implement function "flooding".
#include <stdio.h>
void flooding(int R, int C, int pr, int pc);
char Map[505][505];
int count; //for counting the number of holes that can be filled
int main(void){
int N,R,C,X,i,j,k;
scanf("%d", &N);
for(k=0;k<N;k++){
scanf("%d%d%d", &R, &C, &X);
for(i=0;i<R;i++){
for(j=0;j<C;j++){
Map[i][j] = ' ';
}
}
for(i=0;i<R;i++){
scanf("%s", Map[i]);
}
count = 0;
flooding(R,C,0,X);
printf("%d\n", count);
}
return 0;
}
void flooding(int R, int C, int pr, int pc){
/*your code*/
}
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 row and column 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 the number of holes in the land that can be filled with water, and there is a '\n' at the end of each line.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
A palindrome is a string that is identical to its reverse, like "level" or "aa".
Given a string, find the longest palindrome in the string.
Hint.
You can use the following partial code to check each possible substring.
for(start = 0; start < strlen(input); start++){
for(end = start; end < strlen(input); end++){
ToDo
}
}
Input
The input has only one line, which contains a string.
The length of the string is less than 1000.
Output
Output the longest palindrome in the string.
If there are many palindromes with the same length, output the first one in the string.
Note that you DO NOT need to print a newline character (‘\n’) at the end of the line.