| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 11246 | Cheat Sheet Chen Mid2 |
|
| 11302 | Caesar's Cipher - The Simple Cipher |
|
| 11303 | delete linked list |
|
| 11304 | Final Score Sorting |
|
| 11305 | Array Row Swapping |
|
| 11306 | Multivariable Linear Inequalities - Lite |
|
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
Caesar's cipher is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence.

The cipher illustrated here uses a left shift of 3,
so that each occurrence of E in the plaintext becomes B in the ciphertext.
(https://en.wikipedia.org/wiki/Caesar_cipher)
In this problem, several lines of plaintext made of only uppercases and spaces are given, and you have to determine what is the correct left shift to make the first letter to be 'A'. Then you have to use the same left shift to generate the ciphertext.
Hint:
- You can use char ch = getchar() ; to get the first character in the input.
- Write a function char cipher( char plain, int shift ) can make your code simpler. You just think about what the new character should be after processing by cipher. For example, the characters ' ' (space) and '\n' (newline) should remain the same.
- After you print the first character, use while to process all the other characters:
while( EOF != (ch = getchar() ) ){
putchar( cipher( ch, left_shift) ) ;
}
Input
The input is the plaintext. There are several lines of input. All the characters shoud only be one of 'A'-'Z', spaces, and newline('\n') symbols.
There are at most 10 lines, and each line contains less than 100 characters.
The first character must be an alphabet.
Output
Print the ciphertext. You should find out the correct shift to make the first character be an 'A'.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
This problem will give you a sequence of positive integers. Use this sequence to create a linked list to store those integers. Next, the problem will give you another sequence of positive integers, p0, p1,…pk-1. Delete the nodes in the position at p0, p1, …pk-1 of the linked list, where 1<=p0<p1<…pk-1<=N. If the node is not existing,do nothing. And show the final results.
For example, if the first sequence is 1, 2, 3, 4, 5, 6,7, a linked list is created as
If the second sequence is 1, 1, 4, do the follows.
After p1=1, the list becomes
because the first node is 1. After p2 = 1, the list becomes
because the first node is 2. After p3 = 4, the list becomes
because the fourth node is 6.
The framework of the program is provided.
- Create a linked list from the input (createList)
- while there are still some data pi
- read in pi
- delete node at pi (deleteNode)
- print the remaining list (printList)
- free the list (freeList)
You will be provided with main.c and function.h. main.c contains the implementation of function printList, and freeList, and function.h contains the definition of node and the interface of createList(&head) and deleteNode(&head, pi). You only need to implement createList(&head) and deleteNode(&head, pi) in function.c, in which head is the head of the linked list, and pi is the node at pi to be deleted.
You will be provided with main.c and function.h, and asked to implement function.c.
For OJ submission:
Step 1. Submit only your function.c into the submission block. (Please choose c compiler)
Step 2. Check the results and debug your program if necessary.
Input
The input contains 2 sequence of positive integers as the linklist and the order, except the last one, which is -1, indicating the end of the sequence.
Output
The output contains the sequence of resulting linklist.
Sample Input Download
Sample Output Download
Partial Judge Code
11303.cPartial Judge Header
11303.hTags
Discuss
Description
We assume the final score of this semester is scored by midterm I, midterm II and final exam.
Each exam worth 100 points, so there is 300 points in total.
There are n students in this class, and their student ID are counted from 1 to n.
This problem is sort the students according to the following rules.
1. sort them by 0.3*(midterm I)+0.3*(midterm II)+0.4*(final exam)
2. If there are two students which get same order in rule 1, the one with more final exam scores would get higher order.
3. If there are two students which get same order in rule 2, the one with less student ID would get higher order.
In this problem, you are asked to implement a function that compares two students
Input
The first line contains a integer n.
There are n lines following, each contains the score of midterm I, midterm II and final exam.
Line i of the n lines represent the score of student whose ID is i.
Output
Print the order of the students in terms of student ID.
Sample Input Download
Sample Output Download
Partial Judge Code
11304.cPartial Judge Header
11304.hTags
Discuss
Description
This problem might be the most challenging one in this contest. Challenge this one when you are ready.
In this problem, an NxN array is given. After applying several tiems of "Row Swappings", you have to print the final value at specified coordinate.
The array is composed of N rows, and each row has N values.
Row 0: a0,0 a0,1 a0,2 a0,3 a0,4 ... a0,N-1
Row 1: a1,0 a1,1 a1,2 a1,3 a1,4 ... a1,N-1
Row 2: a2,0 a2,1 a2,2 a2,3 a2,4 ... a2,N-1
Row 3: a3,0 a3,1 a3,2 a3,3 a3,4 ... a3,N-1
Row 4: a4,0 a4,1 a4,2 a4,3 a4,4 ... a4,N-1
. . . . . ... .
Row N-1: aN-1,0 aN-1,1 aN-1,2 aN-1,3 aN-1,4 ... aN-1,N-1
A "Row Swapping" in this problem is to "swap all the values in the K rows following row S and row T". For example, if we "swap all the values in the 2 rows following row 3 and row 0", the result will be:
(new) Row 0: a3,0 a3,1 a3,2 a3,3 a3,4 ... a3,N-1
(new) Row 1: a4,0 a4,1 a4,2 a4,3 a4,4 ... a4,N-1
Row 2: a2,0 a2,1 a2,2 a2,3 a2,4 ... a2,N-1
(new) Row 3: a0,0 a0,1 a0,2 a0,3 a0,4 ... a0,N-1
(new) Row 4: a1,0 a1,1 a1,2 a1,3 a1,4 ... a1,N-1
. . . . . ... .
Row N-1: aN-1,0 aN-1,1 aN-1,2 aN-1,3 aN-1,4 ... aN-1,N-1
7 1 2 3 4 5 6
6 7 1 2 3 4 5
5 6 7 1 2 3 4
4 5 6 7 1 2 3
3 4 5 6 7 1 2
2 3 4 5 6 7 1
Suppose 2 row swapings {K, S, T}, which are {2, 3, 0} and {3, 1, 4}, are applied to this array. After the row swapping {2, 0, 3} is applied, the array becomes:
4 5 6 7 1 2 3
6 7 1 2 3 4 5
1 2 3 4 5 6 7
7 1 2 3 4 5 6
3 4 5 6 7 1 2
2 3 4 5 6 7 1
7 1 2 3 4 5 6
3 4 5 6 7 1 2
2 3 4 5 6 7 1
4 5 6 7 1 2 3
6 7 1 2 3 4 5
1 2 3 4 5 6 7
Suppose the specified coordinate is [2][3], the final values at the coordinate will be printed, which is the value 6:
5 6 7 1 2 3 4
7 1 2 3 4 5 6
3 4 5 6 7 1 2
2 3 4 5 6 7 1
4 5 6 7 1 2 3
6 7 1 2 3 4 5
1 2 3 4 5 6 7
Input
The first line is an integer X, 1 ≤ X ≤ 10. There will be X independent cases in the input.
In each case, the first line contains 2 integers N and M, 2 ≤ N ≤ 1000 and 0 ≤ M ≤ 1000, which denote the array size and the number of swappings. Each of the following M lines contains 3 integers K, S and T, which means to "swap all the values in the K rows following row S and row T". After these swappings, the final line in the case contains 2 integers R and C, which is the specified coordinate that we have to report its final value.
In each row swapping, there is no overlapping between the 2 ranges to be swapped. And these ranges will never excceed the array size.
Output
For each case, there is one line contains only one integer, which is the final value at the specified coordinate. There will be X values with X newline symbols in the output.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
An example of a multivariable linear inequality is like
where the ai's are coefficients, the xi's are unknowns, and the operation can be "less than(<)", "greater than(>)", or "greater than or equal to."
This problem ask you to list all the solutions of a specific kind of multivariable Linear Inequality, where all the unknowns are non-negative integers, all coefficients are 1, and it has constraints at both side. A mathematical representation is like:
Input
Two numbers in a line, U and n.
- U is the inclusive upper bound, is always larger than 0, and less than 20.
- n is the total number of unknowns, which is less than 20.
Output
The output should be all the solutions listed in ascending order. See the definition and example output below.
Solution
A solution is a set of non-negative numbers separated by space and ended with a newline(\n).
Ascending order
For any solution S1(x1, x2, ...) and the solution S2(y1, y2, ...) next to it,
If they share the same prefix of numbers,
then the first number not in the common prefix in S2 MUST larger than that in S1.
Otherwise,
Appendix: Properties and Facts
The total number of solutions of the multivariable integer linear inequality
is
since it is the number of combinations with repetition(重複組合).
So you can verify if the number of lines of your output with this equation.
Hint: the implementation
#include<stdio.h>
#define MAX_UNKNOWN 20
int solution[MAX_UNKNOWN] = {0};
int upper;
void show(int stop){
printf("%d", solution[0]);
for(int i = 1; i < stop; i++)
printf(" %d", solution[i]);
printf("\n");
}
void count(int max, int sum, int n_unknown, int stop){
int i, j;
if(n_unknown == stop){
show(stop);
return;
}
// for all possible values, recursively call count()
for(i = 0; i <= max; i++){
// implement this part
}
}
int main(){
int n;
scanf("%d %d", &upper, &n);
count(upper, 0, 0, n);
}