| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 10806 | Prime number calculator (function) |
|
| 10833 | Permutations of Set |
|
| 10851 | Equivalent relation (Find maximum) |
|
| 11183 | The Longest Palindrome - partial |
|
| 11198 | GCD & LCM |
|
| 11199 | Tower of Hanoi |
|
| 11921 | Reorder |
|
Description
Given a range, you need to count the number of prime numbers in the range.
Note that "1" is not the prime number.
For example:
The range is 1~10
You need to print 4.
Beacuse of there are 4 numbers (2, 3, 5, 7 are the prime numbers) in the range 1~10.
Note that
1. This problem involves three files.
- function.h: Function definition of numPrime.
- function.cpp: Function describe of numPrime.
- main.cpp: A driver program to test your implementation.
You will be provided with main.cpp and function.h, and asked to implement function.cpp.
2. For OJ submission:
Step 1. Submit only your function.cpp into the submission block. (Please choose c++ 11 compiler)
Step 2. Check the results and debug your program if necessary.
function.h
#ifndef FUNCTION_H
#define FUNCTION_H
int numPrime(int start, int end);
#endif
main.cpp
#include <stdio.h>
#include "function.h"
int main(){
int start, end, num;
scanf("%d %d", &start, &end);
num = numPrime(start, end);
printf("%d", num);
return 0;
}
Input
Given the start and end of the range: S E.
Fisrt number S is the start number of the range.
Second number E is the end number of the range.
And the range limit is 1 <= S <= E <= 5000.
For example:
when input is "1 10", it means the range is 1 to 10.
Output
Count the number of prime numbers in the range.
Note that you do not need to print '\n' at the end of the output.
Sample Input Download
Sample Output Download
Partial Judge Code
10806.cppPartial Judge Header
10806.hTags
Discuss
Description
Given a set of n≧1 elements, the problem is to print all possible permutations of this set. For example, if the set is (1,2,3), then the set of permutations is {(1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,2,1), (3,1,2)}.
<Hint1>
Looking at the case of four elements (1,2,3,4). The answer can be constructed by writing
- ‘1’ followed by all the permutations of (2,3,4)
- ‘2’ followed by all the permutations of (1,3,4)
- ‘3’ followed by all the permutations of (1,2,4)
- ‘4’ followed by all the permutations of (1,2,3)
<Hint2>
A recursive method to implement the above idea is as follows:
Consider the case of (1,2,3,4), that is, n=4.
- Place the set elements in a global array, and set the position index “k” as 0.
- Use a for-loop to “swap” (or exchange) the 1st element with the 1st element, the 2nd element, the 3rd element, and the 4th element, respectively.
- In each loop-iteration:
- increment the position index “k” by 1 (for considering only the remaining elements in the following recursive call);
- use the updated k to recursively call your permutation function;
- note that because you use a global array, remember to swap back the two elements after the iteration.
- In each loop-iteration:
- In a recursive-call path, when k reaches n, it means that you get a possible permutation.
Note that
1. This problem involves three files.
- function.h: Function definition of show, Swap and Perm.
- function.c: Function describe of show, Swap and Perm.
- 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.
function.h
main.c
function.c
Input
The decimal number n that represents the number of elements in the set.
(1≦n≦5)
Output
In the output you should print all the permutations.
Be sure to add a newline character '\n' at the end of each line.
Sample Input Download
Sample Output Download
Partial Judge Code
10833.cPartial Judge Header
10833.hTags
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.
1. “S n k” : n is the index of the pointer, and k is an integer value; this operation is to assign the value k to the integer pointed by pointer n; after this operation, the integer pointed by pointer n will have a value k.
2. “M n k”: n is the index of the pointer, and k is an integer value; this operation is to multiply the integer pointed by pointer n by k times; after this operaiton, the integer pointed by pointer n will be k times larger.
3. “P n m”: n and m are the indexes of two pointers; this operation is to make pointer n point to the same integer as pointer m; after this operation, m and n will point at the same address.
After some manipulation following the given instructions, we want to find out the maximum of what pointer array points to in some specific range, which is also given by the input.
Note that
1. This problem involves three files.
- function.h: Function definition of execInst, findMax.
- function.c: Function describe of execInst, findMax.
- 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 start, end;
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 %d %d", &inst, ¶m1, ¶m2);
execInst(ptrArr, inst, param1, param2);
}
scanf("%d %d", &start, &end);
/* output */
for (i = 0; i < dataNum - 1; i++) {
printf("%d ", dataArr[i]);
}
printf("%d\n", dataArr[i]);
for (i = 0; i < dataNum - 1; i++) {
printf("%d ", *ptrArr[i]);
}
printf("%d\n", *ptrArr[i]);
printf("%d\n", findMax(ptrArr , start, end));
return 0;
}
function.h
#ifndef FUNCTION_H
#define FUNCTION_H
void execInst(int *ptrArr[], char inst, int param1, int param2);
int findMax(int *ptrArr[], int start, int end);
#endif
function.c
#include "function.h"
void execInst(int *ptrArr[], char inst, int param1, int param2){
if(inst=='S'){
/--your code--/
}
else if(inst=='M'){
/--your code--/
}
else if(inst=='P'){
/--your code--/
}
}
int findMax(int *ptrArr[], int start, int end){
/--your code--/
return max;
}
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.
The last line contain two integers, start and end respectively.
Output
First line: All the values that dataArr[0] to dataArr[N-1] stored in order. Each value is seperated by a blank ' '.
Second line: All the values that ptrArr[0] to ptrArr[N-1] point to in order. Each value is seperated by a blank ' '.
Final line: The max value that ptrArr[start] to ptrArr[end] point to.
Sample Input Download
Sample Output Download
Partial Judge Code
10851.cPartial Judge Header
10851.hTags
Discuss
Description
A palindrome is a string that is identical to its reverse, like "level" or "aba". In this problem, some strings are given, and the longest palindrome in each string should be computed.
This problem involves three files:
1. 11183.c: the source file containing the main function and calls ispal().
2. 11183.h: the header file that gives the prototype of function ispal().
3. your answer as a C source file, in which you should implement the ispal() function to determine whether a given string is a palindrome or not.
Please check the provided files for details.
Input
The input consists of multiple lines, and each of them is a string that contains alphabets and numbers.
The length of each string is less than 1000 and larger or equal to 1.
The number of lines is less than or equal to 100.
Output
The longest palindrome in each string should be outputted.
If there are many palindromes of the same length, output the first one in the string.
Sample Input Download
Sample Output Download
Partial Judge Code
11183.cPartial Judge Header
11183.hTags
Discuss
Description
In this problem, you are required to write 2 finctions, gcd() and lcm(), that can help calculate the Greatest Common Divisor (最大公因數) and Least Common Multiple (最小公倍數). The main function will receive multiple positive integers as input values, and it will print the GCD and LCM of all numbers.
The definition of the 2 functions only calculate the GCD or LCM of 2 numbers. These functions are defined as following:
int gcd( int a, int b ) ; // This returns the GCD of positive integer a and b
int lcm( int a, int b ) ; // This returns the LCM of positive integer a and b
Please implement these 2 functions in a C source file, we will take care of the input and output.
Hint:
You should have already learned about how to calculate GCD by recursive or sequential ways, then we can calcute LCM of 2 numbers a and b by the result of GCD. Suppose D=gcd(a,b), and let a equals p*D and b equals q*D (p and q are relatively prime), then LCM of a and b should be p*q*D, or any other equivalent formats you prefer.
Input
The input contains several (at least 2) positive integers in a line. There are at most 10 integers in the input, and the result of Least Common Multiple can be stored by a 32-bit signed integer.
Output
Print 2 lines as the sample. The first line contains texts like "The Greatest Common Divisor is *." while the second line seems like "The Least Common Multiple is *."
In this homework, you are not required to take care the I/O, yet we hope you can stiil be inspired by the provide the main function.
Sample Input Download
Sample Output Download
Partial Judge Code
11198.cPartial Judge Header
11198.hTags
Discuss
Description
The Tower of Hanoi is a mathematical game or puzzle. It consists of 3 rods, and N disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
- Only 1 disk can be moved at a time.
- Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
- No disk may be placed on top of a smaller disk.
In this problem, you are given 3 rods named rod 1, 2 and 3, with N disks in a stack onto rod 1. Your mission is to move all disks onto disk 2 in minimal steps without breaking the riles provided. You have to print all moves.
You only have to implement the function "hanoi()" to achive the goal. Here is the defination of hanoi():
void hanoi( int rod_N, int rod_A, int rod_B, int rod_C ) ;
rod_N is the number of disks to be move right now, while rod_A, _B, and _C denoted the rod ID, which should be 1, 2, or 3. The function is to "Move the top rod_N disks from rod_A to rod_B via rod_C", and our main() function will hanoi(N, 1, 2, 3) without printing anything.
Input
The input consists of only one positive integers, denoting there are N disks given. At most 8 disks may be given.
Output
For the input, print all required steps of moving. Each move should be printed as an one-lined message like "Move from rod 1 to rod 2.\n". Please replace the rod ID on your own.
Sample Input Download
Sample Output Download
Partial Judge Code
11199.cPartial Judge Header
11199.hTags
Discuss
Description
Given four integer numbers, please rearrange the value of them in increasing order.
Hint: You may design a function:
|
void cmp_and_swap(int *first, int *second){ /* if the first element is not smaller than the second one, swap the first and second ones . */ } |
Then call cmp_and_swap() in reorder(int* n1, int* n2, int* n3, int* n4);
Input
The input has four integer numbers num1, num2, num3, and num4.
Output
Print num1, num2, num3, num4 sequentially, and the output should be in increasing order.