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



文件信息
文件来源  
文件作者  
更新时间 2005-5-8 10:40:52 
添加编辑 viewsnake 

辅助信息
打印功能 打印本文
背景颜色 杏黄 秋褐 胭红 芥绿 天蓝 雪青 炭灰 奶白
字体大小 特大号字 大号字 中号字 小号字
免责声明 本网站所有文章均来自网络,仅提供预览形式,不提供纸张形式,若涉及到版权的文章,请购买正版,毕竟在电脑上看也不舒服啊,呵呵,这是viewsnake个人网站,纯粹交流学习资料的地方。无商业行为。
选择更多免费考研资料:
阅读正文内容
 

第四章

//*******************
//*  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;
        }
}



<<<返回上一页 <<<返回网站首页
<<<您的位置:首页>考研经验>考研笔记>计算机工程笔记>正文