| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 12396 | Walking Up Stairs |
|
| 12448 | Hu_cheatsheet |
|
| 12967 | Evaluate the expression |
|
Description
Your friend likes walking up stairs.
He likes walking up stairs so much that everytime, he attempts to walk up stairs in a way he never did before.
When walking up stairs, he either takes one step up or three steps up at once.
Specifically, given a staircase with N levels, he would decide a sequence of steps (composed of 1s and 3s) to take, such that the sum of the sequence is N.
Two ways of walking up stairs are different if and only if the sequence of steps differ.
For example, given a stairs with 5 floors, he has 4 unique ways of walking up the stairs:
a. [1, 1, 3]
b. [1, 3, 1]
c. [3, 1, 1]
d. [1, 1, 1, 1, 1]
Given N, find out how many times your friend may enjoy walking the stairs up in a unique way.
Smart as your friend is, figured out that f(n), the number of ways to walk up n levels, may be described as the following:
f(n) = f(n - 1) + f(n - 3), if n > 2
f(n) = 1, otherwise
Input
N
(N is a positive integer in between 1 and 116)
Output
A positive integer indicating the number of unique ways to walk up the stairs, with a trailing newline character.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
printf() and scanf() format
printf("%d", n);
FORMAT ARGUMENT TYPE
%d, %i int decimal
%lld long long
%llu unsigned long long
%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");
}
logical and comparison operators operators:
! && || == != > < >= <=
arithmetic operators:
+ - * / %
bitwise operators:
& | ^ << >> ~
int strcmp(const char *lhs, const char *rhs);
int strcat(const char *lhs, const char *rhs);
int strcpy(const char *lhs, const char *rhs);
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.
Note : If you are using visual studio, add #pragma warning(disable:4996) in the first line so that you can use scanf on your local machine.
Input
Output
Sample Input Download
Sample Output Download
Tags
Discuss
Description
You are given a string of legal mathematical expression, which contains integers and arithmetic signs (only addition or subtraction) between them, with at most one parenthesis.
Please evaluate and output the result.
Input
The input contains a single line of math expression, which has N integers and N-1 signs ('+' or '-') between them.
1 ≤ N ≤ 100, integers are ranged from 0 to 100000000, and there will be no leading zeros.
In the first five testcases, there will be no parenthesis. We encourage you to solve these testcases first.
Output
Please output the evaluated result of the expression, followed by a newline character.