三道基础C++题,哪位大侠帮我解答下啊!!! 40

1)TheexpressionC(n,r)denotesthenumberofr-elementsubsetsofann-elementset.Forexample,C(... 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

最好能直接可以运行的,保证加分!!!
展开
 我来答
百度网友36ecabb00
2010-03-21 · 超过32用户采纳过TA的回答
知道答主
回答量:136
采纳率:0%
帮助的人:98.4万
展开全部
第一题
#include <iostream.h>

void err_msg()
{
cerr<<"n must be greater than or equal to r in C(n,r)"<<endl;
}

long fact(int a)
{
long t;
t=a;
for(int i=a-1;i>=1;i--)
{
t=t*i;
}
return t;
}

int check(int n,int r)
{
if(r>n)
{
err_msg();
return 0;
}
else
return 1;
}

void comb(int n,int r)
{
if(!check(n,r));
else
{
long s;
s=fact(n)/(fact(r)*fact(n-r));
cout<<s<<endl;
}
}

void main()
{
int n,r;
cout<<"Enter n in C(n,r) : ";
cin>>n;
cout<<"Enter r in C(n,r) : ";
cin>>r;
comb(n,r);
}

第二题:

#include <iostream.h>

int cal1(int p,int q)
{
int k;
k=1;
for(;k<=q;k++)
{
cout<<k<<"\t";
p++;
if(p>7)
{
cout<<"\n";
p=1;
}
}
return p;
}

int cal(int p,int q)
{
int k,c;
k=1;
for(c=p;c>1;c--)
{
cout<<"\t";
}
for(;k<=q;k++)
{
cout<<k<<"\t";
p++;
if(p>7)
{
cout<<"\n";
p=1;
}
}
return p;
}

void calendar(int i,int l)
{
int p,q;
int *a;
a=&p;
if(!((i>=1)&&(i<=7)))
{
cout<<"the day for Jan 1 is error!"<<endl;
}
else if(!((l==0)||(l==1)))
{
cout<<"leap year input error!"<<endl;
}
else
{

//January

cout<<"\n";
cout<<"\t"<<"January"<<endl;
cout<<"\n";
p=1;
q=31;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<<endl;
for(i;i>1;i--)
{
cout<<"\t";
p++;
}
p=cal1(p,q);
cout<<"\n";

//February

cout<<"\n";
cout<<"\t"<<"February"<<endl;
cout<<"\n";
if(l==0)
q=28;
else
q=29;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<<endl;
p=cal(p,q);
cout<<"\n";

//March

cout<<"\n";
cout<<"\t"<<"March"<<endl;
cout<<"\n";
q=31;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<<endl;
p=cal(p,q);
cout<<"\n";

//April

cout<<"\n";
cout<<"\t"<<"April"<<endl;
cout<<"\n";
q=30;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<<endl;
p=cal(p,q);
cout<<"\n";

//May

cout<<"\n";
cout<<"\t"<<"May"<<endl;
cout<<"\n";
q=31;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<<endl;
p=cal(p,q);
cout<<"\n";

//June

cout<<"\n";
cout<<"\t"<<"June"<<endl;
cout<<"\n";
q=30;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<<endl;
p=cal(p,q);
cout<<"\n";

//July

cout<<"\n";
cout<<"\t"<<"July"<<endl;
cout<<"\n";
q=31;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<<endl;
p=cal(p,q);
cout<<"\n";

//August

cout<<"\n";
cout<<"\t"<<"August"<<endl;
cout<<"\n";
q=31;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<<endl;
p=cal(p,q);
cout<<"\n";

//September

cout<<"\n";
cout<<"\t"<<"September"<<endl;
cout<<"\n";
q=30;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<<endl;
p=cal(p,q);
cout<<"\n";

//October

cout<<"\n";
cout<<"\t"<<"October"<<endl;
cout<<"\n";
q=31;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<<endl;
p=cal(p,q);
cout<<"\n";

//November

cout<<"\n";
cout<<"\t"<<"November"<<endl;
cout<<"\n";
q=30;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<<endl;
p=cal(p,q);
cout<<"\n";

//December

cout<<"\n";
cout<<"\t"<<"December"<<endl;
cout<<"\n";
q=31;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<<endl;
p=cal(p,q);
cout<<"\n";

}

}

void main()
{
int i,l;
cout<<"Enter the day for Jan 1:"<<endl;
cout<<"[1 - Su, 2 - Mo, 3 - Tu, 4 - We, 5 - Th, 6 - Fr, 7 - Sa] :";
cin>>i;
cout<<"Is this a leap year: [0 - No, 1 - Yes] : ";
cin>>l;
calendar(i,l);
}

第三题:

#include <iostream.h>

int numHandshakes(int num)
{
int k=0;
if(num==1)
return 0;
else if(num==2)
return 1;
else
{
k=num-1+numHandshakes(num-1);
return k;
}
}

void main()
{
int i,j;
cout<<"Enter number of people : ";
cin>>i;
j=numHandshakes(i);
cout<<"Total number of handshakes = "<<j<<endl;

}

加分 加分 加分!~!!!!!!!!!!!
匿名用户
2010-03-21
展开全部
第二题好头疼哦,最讨厌日历了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式