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

2025-01-07 05:47:46
推荐回答(2个)
回答1:

第一题
#include

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

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< }
}

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

int cal1(int p,int q)
{
int k;
k=1;
for(;k<=q;k++)
{
cout< 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< 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!"< }
else if(!((l==0)||(l==1)))
{
cout<<"leap year input error!"< }
else
{

//January

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

//February

cout<<"\n";
cout<<"\t"<<"February"< 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"< p=cal(p,q);
cout<<"\n";

//March

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

//April

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

//May

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

//June

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

//July

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

//August

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

//September

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

//October

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

//November

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

//December

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

}

}

void main()
{
int i,l;
cout<<"Enter the day for Jan 1:"< 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

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 = "<
}

加分 加分 加分!~!!!!!!!!!!!

回答2:

第二题好头疼哦,最讨厌日历了