| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 12448 | Hu_cheatsheet |
|
| 12559 | Characters Sort By Frequency With Specific Order |
|
| 12560 | Permutation With Repetition |
|
| 12561 | Equivalent relation 2 |
|
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
(modified by leetcode)
Given a string, sort it in decreasing order based on the frequency of characters and with following rule:
If the frequency of characters is the same, then sort it with Uppercase > Lowercase > Digit.
if the frequency of characters is the same and have the same type(uppercase or lowercase or digit), then sort uppercase and lowercase type with dictionary order, and sort digit type with increasing order.
There are few examples below:
Input: "tree" Output: "eert" Explanation: 'e' appears twice while 'r' and 't' both appear once. So 'e' must appear before both 'r' and 't'. And 'r' is before 't' since it is dictionary order
Input: "aAbb54" Output: "bbAa45" Explanation: 'b' appears twice while 'a', 'A', '5' and '4' are appear once. So 'b' must appear before both 'a','A','5' and '4'. And Since the order with different type is upercase > lowercase > digit, then 'A' must appear before 'a','5' and '4', and 'a' must appear before '5' and '4'. Finally since '5' and '4' have same frequency then sort it by increasing order.
Note : you need to implement the sorting by yourself without using standard library!!!
Please raise you hand to notify TA when you get AC.
We will check all the student's code after the exam. If you violate the rule, we will find out !!!!!!!!!
Input
T
S1
...
Si
T: the number of string testcase ( 5 < T < 20)
Si : the given string (1 < length(S) < 400)
Note : the input string only contain three type of character (uppercase alpha , lowercase alpha and digit)
Note : Testcase hint
Testcase 1: same type with distinct different frequency. ex: 122333, abbccc, ABBCCC
Testcase 2: different type with distinct different frequency. ex: a22BBB
Testcase 3: same type with same frequency. ex: 321, cba, CBA
Testcase 4: differnt type with same frequency. ex: 012abcABC, ABC012abc
Testcase 5-8: random testcase with all possible combination
Output
Output the string is sorted by frequency with specific order. (each output string followed by a newline characters.)
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Given a string of length n, print all permutation of the given string. and repetition of characters is allowed!.
There are few examples below:
Case1:
Input: A
output: A
Case2:
Input: AB
output: AA AB BA BB
Input
S
S: the input string ( 2 <= len(S)) < 7)
Note : the input string only contain alpha characters included upercase and lowercase.
Note : each element of input string is different.
Output
Ouput the permutation with repetition pattern (each output string followed by a newline characters)
Sample Input Download
Sample Output Download
Tags
Discuss
Description
There are N integer pointers, indexed from 0 to N-1 (N<100). Each pointer initially points to an integer of value 0.
There are three kinds of instructions. The instruction “S n k” is used to set the integer, which pointer n points to, to be k. For example, S 1 10 means that the integer that pointer 1 points to is set to 10. And the instruction “P n m” means that pointer n points to the integer that pointer m points. For example, P 2 1 means that pointer 2 points to the integer that pointer 1 points to. After P 2 1, pointer 2 and pointer 1 point to the same integer, which is pointed by pointer 1. The Instruction "M" is used to move all pointer to point the next pointer, and the last pointer will point to the first pointer before the first pointer point to the next pointer.
The instruction 'M' :
Take 3 elements for example:
before M : pointer0 -> integer 10, pointer1 -> integer 20, pointer2 -> integer30. output : ==> 10 20 30
after M: pointer0-> integer 20, pointer1 -> integer30, pointer2 -> integer 10. output ==> 20 30 10
Note that you don't have to change all the pointers if one pointer changes its target. The following table is an example. The instructions are P 1 2 and then P 2 3. You do not have to change the target of pointer 1.
|
instruction |
Description |
|
P 1 2 |
Pointer 1 points to the integer that pointer 2 points to.
|
|
P 2 3 |
Pointer 2 points to the integer that pointer 3 points to. And you don’t have to change the target of pointer 1. |
Finally, output all the values that pointers 0 to N-1 point to in order.
Note that
1. This problem involves three files.
- function.h: Function definition of execInstruct.
- function.c: Function describe of execInstruct.
- main.c: A driver program to test your implementation.
You will be provided with main.c and function.h, and asked to implement function.c.
2. 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.
Hints:
main.c
#include <stdio.h>
#include "function.h"
#define SIZE 100
int main() {
int *ptrArr[SIZE];
int dataArr[SIZE] = {0};
char inst;
int dataNum, instNum;
int param1, param2;
int i;
/* input */
scanf("%d %d", &dataNum, &instNum);
/* initialize the ptrArr */
for (i = 0; i < dataNum; i++)
ptrArr[i] = &dataArr[i];
for (i = 0; i < instNum; i++) {
scanf(" %c", &inst);
if(inst == 'M'){
execInst(ptrArr, inst, -1, -1, dataNum);
}else{
scanf("%d %d",¶m1, ¶m2);
execInst(ptrArr, inst, param1, param2, dataNum);
}
}
/* output */
for (i = 0; i < dataNum - 1; i++) {
printf("%d ", *ptrArr[i]);
}
printf("%d", *ptrArr[i]);
return 0;
}
function.h
#ifndef FUNCTION_H
#define FUNCTION_H
void execInst(int *ptrArr[], char inst, int param1, int param2, int dataNum);
#endif
Input
The first line contains two positive X and Y. X indicates the size of data. Y indicates that there are Y instructions needed to be done.
The next Y lines contain the instructions.
Note:
Test case 1 - 5 will not contain the new instruction 'M'
Test case 6-7 will contain the new instruction 'M'
Output
All the values that pointers 0 to pointer N-1 point to in order. Each value is seperated by a blank ' '.
# Note that there is no '\n' at the end of the output.