第一题
#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<
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<
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"<
p=1;
q=31;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<
{
cout<<"\t";
p++;
}
p=cal1(p,q);
cout<<"\n";
//February
cout<<"\n";
cout<<"\t"<<"February"<
if(l==0)
q=28;
else
q=29;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<
cout<<"\n";
//March
cout<<"\n";
cout<<"\t"<<"March"<
q=31;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<
cout<<"\n";
//April
cout<<"\n";
cout<<"\t"<<"April"<
q=30;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<
cout<<"\n";
//May
cout<<"\n";
cout<<"\t"<<"May"<
q=31;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<
cout<<"\n";
//June
cout<<"\n";
cout<<"\t"<<"June"<
q=30;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<
cout<<"\n";
//July
cout<<"\n";
cout<<"\t"<<"July"<
q=31;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<
cout<<"\n";
//August
cout<<"\n";
cout<<"\t"<<"August"<
q=31;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<
cout<<"\n";
//September
cout<<"\n";
cout<<"\t"<<"September"<
q=30;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<
cout<<"\n";
//October
cout<<"\n";
cout<<"\t"<<"October"<
q=31;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<
cout<<"\n";
//November
cout<<"\n";
cout<<"\t"<<"November"<
q=30;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<
cout<<"\n";
//December
cout<<"\n";
cout<<"\t"<<"December"<
q=31;
cout<<"Sun\t"<<"Mon\t"<<"Tue\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat\t"<
cout<<"\n";
}
}
void main()
{
int i,l;
cout<<"Enter the day for Jan 1:"<
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 = "<
}
加分 加分 加分!~!!!!!!!!!!!
第二题好头疼哦,最讨厌日历了