1) The expression C(n,r) denotes the number of r-element subsets of an n-element set.
For example, C(4,2) is 6 because there are six 2-element subsets of a 4-element set.
(Illustrating, if the elements are {a, b, c, d}, then the six 2-element subsets are
{(a, b), (a, c), (a, d), (b, c), (b, d), (c, d)}
The value of C(n,r) is given by the formula
c(n,r) = n! / ( r! * (n-r)! )
Write a program that computes C(n,r) using the following component functions.
(a) main: prompts the user for two numbers and accents them into n and r, respectively.
(b) check: compares r and n. If r > n, check invokes the function err_msg,
which prints an appropriate error message.
(c) comb: computes C(n,r).
(d) fact: computes factorial.
--------------
Sample Output:
(i)
Enter n in C(n,r) : 4
Enter r in C(n,r) : 7
n must be greater than or equal to r in C(n,r)
(ii)
Enter n in C(n,r) : 5
Enter r in C(n,r) : 2
10
(iii)
Enter n in C(n,r) : 25
Enter r in C(n,r) : 20
53130
(iV)
Enter n in C(n,r) : 52
Enter r in C(n,r) : 48
270725
2) Write a program that prints a calendar for a year. Prompt the user for which day of the week
January 1 is on and whether the year is a leap year. The day that January 1 is on is coded as
follows:
Sun 1
Mon 2
Tue 3
Wed 4
Thu 5
Fri 6
Sat 7
-----------------
Sample Output:
Enter the day for Jan 1:
[1 - Su, 2 - Mo, 3 - Tu, 4 - We, 5 - Th, 6 - Fr, 7 - Sa] : 5
Is this a leap year: [0 - No, 1 - Yes] : 1
January
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
(以此类推)
3) Write a recursive function that given n people and they all shake each other's hands,
how many different handshakes are there.
Your recursive function must implement the following algorithm:
int numHandshakes(int num) {
if number of people is 1, return 0. // termination
else if number of people is 2, return 1. // termination
else return (n - 1 + numHandshakes(n - 1)). // recursion
}
------------------
Sample Output:
Enter number of people : 7
Total number of handshakes = 21
最好能直接可以运行的,保证加分!!!