| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 10308 | 2048 |
|
| 11243 | Number Tower |
|
| 11244 | Flip |
|
| 11246 | Cheat Sheet Chen Mid2 |
|
Description
The game of 2048 is very simple. Given a 4x4 map containing tiles and spaces. The values are displayed on the tiles, and the spaces are displayed as 0. The values of tiles are always the power of 2. For example,
2 2 64 128
4 0 4 16
4 4 4 4
0 0 2 0
is a valid map.
We are able to slide the tiles in the only down direction. The rules of sliding are defined as follows:
1. All tiles slide past spaces. If you move the puzzle to the bottom, all spaces will end up on the top.
2. After sliding to collapse spaces, any tiles with the same value combine and the remainder of the column slides to fill in the hole.
3. One tile can only combine(be combined) once.
4. The combination always starts from the bottom.
5. Contrary to the famous game of 2048, there are no new tiles generated.
For instance, if we have the map above, after we slide the tiles once, the map becomes
0 0 0 0
0 0 64 128
2 2 8 16
8 4 2 4
Your job is to output the number of spaces for each column after sliding the tiles.
Hint:
#include <stdio.h>
#define WIDTH 4
#define HEIGHT 4
int map[HEIGHT][WIDTH];
void collapse(){
/* your code here */
}
void slide(){
/* your code here */
}
void show(){
/* your code here */
}
int main()
{
int N, d;
int i, j;
scanf("%d", &N);
for(i = 0; i < HEIGHT; i++){
for(j = 0; j < WIDTH; j++){
scanf("%d", &map[i][j]);
}
}
for(i = 0; i < N; i++){
slide();
}
show();
return 0;
}
Input
N (the count of slides)
The map
Output
The number of spaces of each column
Use "%d " to print each number. Print a newline '\n' at the end.
Note that the value of tiles will not exceed 8192.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
There is a number tower which consists of many positive integers, and it looks like a triangle as below :
1
4 6
6 9 3
6 3 7 1
2 6 3 1 8
The first layer contains one positive integer, and the second one contains two, and so on.
Our goal is to find a path from top to bottom which has the largest sum in all paths. The path starts from the root node (the first number in the first layer), and each node can only choose one of the nearest two descendant nodes to move to. For example, 4 in the second layer can only move to 6 or 9 in the third layer. Please, sum up all the number in the path and print the largest sum.
Input
The first line is a positive integer n, that represents the height of the tower. (n <= 10) Each line in next n lines contains m positive integers whose value are not greater than 9 in the mth layer, and all number is seperated by white spaces.
Output
Print the largest value in all paths, and remenber to print a new line in the end of the anwser.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
The input contains a string and K pairs of integers. Each pair contains two integers N and M. The operation is to flip the order of M characters that begins at the N-th position of the input string.
For example, consider the input string "SRVisHandsome", and the pair N = 3, M = 5. The character at position 3 is 'V' and the following 5 characters are "VisHa". Hence you have to flip the 5 characters into "aHsiV", and thus the string becomes "SRaHsiVndsome". Note that the input string contains no more than 1000 characters, and N+M-1 will not exceed the length of the string. A full example: If the input is
ABCDEFGH
3
3 5
5 4
2 6
The string will be changed as follows: ABGFEDCH => ABGFHCDE => ADCHFGBE
Input
The first line is the input string. The second line is an integer K indicating there are K pairs of integers in the following K lines. Each pair of integers N and M represents the position of the starting character and the number of characters to be flipped.
Output
Print the final string after K rounds of flipping.
Sample Input Download
Sample Output Download
Tags
Discuss
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);}