钱能C++教材习题答案二

   /2005-05-08

 

第四章

//*******************
//*  4_1_while.chh  *
//*******************
# include <iostream.h>
# include <math.h>

void main()
{
double x,p=1,total=1;
int i=1,sign=1;
cout<<"please input a double:"<<endl;
cin>>x;
while (fabs(p)>1e-8 )
{
p=sign*p*x/i;
total=total+p;
sign*=-1;
i++;
}
cout<<"when you input a "<<x<<",you can get:"<<total<<endl;
}

//**********************
//*  4_1_do_while.chh  *
//**********************
# include <iostream.h>
# include <math.h>

void main()
{
double x,p=1,total=0;
int i=0,sign=-1;
cout<<"please input a double:"<<endl;
cin>>x;
do
{
total=total+p;
sign*=-1;
i++;
p=sign*p*x/i;
}while (fabs(p)>1e-8);
cout<<"when you input a "<<x<<",you can get:"<<total<<endl;
}


//*******************
//*  4_1_for.chh    *
//*******************
# include <iostream.h>
# include <math.h>

void main()
{
double x,p=1,total=1;
int i=1,sign=1;
cout<<"please input a double:"<<endl;
cin>>x;
for (;fabs(p)>1e-8 ; )
{
p=sign*p*x/i;
total=total+p;
sign*=-1;
i++;
}
cout<<"when you input a "<<x<<",you can get:"<<total<<endl;
}

//************************
//*        4_2.chh       *
//*输入15,得到2004310016*
//************************
# include <iostream.h>
# include <math.h>

void main()
{
long total=1;
int i=1,x;
cout<<"please input a int:"<<endl;
cin>>x;
for (;i<=x ; )
{
total*=i;
i++;
}
cout<<"when you input a "<<x<<",you can get:"<<total<<endl;
}

//************************
//*        4_3.chh       *
//*   153 370 371 407    *
//************************
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
void main()
{

int i,j,k;
int p;
for (p=100;p<1000;p++)
{
i=p/100;
j=(p-i*100)/10;
k=p-i*100-j*10;
        if (i*i*i+j*j*j+k*k*k==p)
                cout<<setw(5)<<p;
       
}

cout<<"/n"<<"game over"<<endl;
}

//************************
//*        4_4.chh       *
//*       6 28 496       *
//************************
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
void main()
{

int i;
for (i=1;i<1000;i++)
{
int p;
p=0;
int j;
for(j=1;j<i;j++)
        {
        if(i%j==0)
                p=p+j;
        }
if(p==i)
        cout<<setw(5)<<i;
}

cout<<"/n"<<"game over"<<endl;
}

//************************
//*        4_5.chh       *
//*      294   0         *
//************************
# include <iostream.h>
# include <math.h>
void main()
{
int i;
int h=100;
int s=-100;//为了确保循环中计算距离的一致
for (i=1;i<10;i++)
        {
        s=s+2*h;
        h=h/2;
        }
cout<<"total instance:"<<s<<";"<<"high at this time:"<<h<<endl;
}

//************************
//*        4_6.chh       *
//*        1023          *
//************************
# include <iostream.h>
# include <math.h>
void main()
{
int i,p,q;
for (i=1;p!=1;i++)
        {
        p=i;
        q=i;
        int j;
        for (j=1;j<10;j++)
                {
                p=p-p/2-1;       
                }
        }
cout<<q<<endl;       
}
 

//************************
//*        4_7.chh       *
//*                      *
//************************
# include <iostream.h>
# include <math.h>
void main()
{
double a;
cin>>a;
double p,q;
const double eps=1e-7;
q=1;
p=(q+a/q)/2;
while(fabs(p-q)>eps)
        {
        q=p;
        p=(q+a/q)/2;
        }
cout<<"the sqrt of"<<a<<"is:"<<p<<endl;
}

//************************
//*        4_8(1).chh    *
//*                      *
//************************
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
void main()
{
        int i;
        for(i=1;i<11;i++)
        {
                int k;
                for(k=1;k<10+11-i;k++)
                        {
                                cout<<" ";               
                        }
                int j;
                for(j=1;j<2*i;j++)
                        {
                                cout<<"*";
                        }
        cout<<endl;
        }
}

//************************
//*        4_8(2).chh    *
//*                      *
//************************
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
void main()
{
        int i;
        int p=18;
        for(i=1;i<9;i++)
        {
                p=p-1;
                int k;
                for(k=1;k<10+i;k++)
                        {
                                cout<<" ";               
                        }
                int j;
                for(j=1;j<p+1;j++)
                        {
                                cout<<"# ";
                        }
        cout<<endl;
        }
}

//************************
//*        4_9(1).chh    *
//*                      *
//************************
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
void main()
{
        cout<<setw(6)<<setiosflags(ios::right)<<"*";
        int i;
        for (i=1;i<10;i++)
        {
                cout<<setw(6)<<setiosflags(ios::right)<<i;
        }
cout<<endl;
        for (i=1;i<11;i++)
        {
                cout<<setw(6)<<setiosflags(ios::right)<<".......";
        }
cout<<endl;

        for (i=1;i<10;i++)
        {
                cout<<setw(6)<<setiosflags(ios::right)<<i;
                int j;
                for (j=1;j<10;j++)
                {
                        cout<<setw(6)<<setiosflags(ios::right)<<i*j;
                }
                cout<<endl;
        }

//************************
//*        4_9(2).chh    *
//*                      *
//************************
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
void main()
{
        cout<<setw(6)<<setiosflags(ios::right)<<"*";
        int i;
        for (i=1;i<10;i++)
        {
                cout<<setw(6)<<setiosflags(ios::right)<<i;
        }
cout<<endl;
        for (i=1;i<11;i++)
        {
                cout<<setw(6)<<setiosflags(ios::right)<<".......";
        }
cout<<endl;

        for (i=1;i<10;i++)
        {
                cout<<setw(6)<<setiosflags(ios::right)<<i;
                int j;
                for (j=1;j<i+1;j++)
                {
                        cout<<setw(6)<<setiosflags(ios::right)<<i*j;
                }
                cout<<endl;
        }
}


相关话题/

  • 领限时大额优惠券,享本站正版考研考试资料!
    大额优惠券
    优惠券领取后72小时内有效,10万种最新考研考试考证类电子打印资料任你选。涵盖全国500余所院校考研专业课、200多种职业资格考试、1100多种经典教材,产品类型包含电子书、题库、全套资料以及视频,无论您是考研复习、考证刷题,还是考前冲刺等,不同类型的产品可满足您学习上的不同需求。 ...
    本站小编 Free壹佰分学习网 2022-09-19