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

   /2005-05-08

閻愮懓鍤粩瀣祮閹兼粎鍌�2娑撳洨顫掗懓鍐埡閻㈤潧鐡欓悧鍫gカ閺傛瑱绱�
婢堆囧劥閸掑棛顏㈤棄瀣厴閺勵垳顑囨稉鈧▎陇鈧啰鐖洪敍灞筋嚠娴滃骸顩ф担鏇熺叀閹靛彞绗撴稉姘愁嚦閹稿洤鐣鹃弫娆愭綏閿涘本鍨ㄧ拋鍛婃箒瀵板牆顦块悿鎴︽6閵嗕境ree婢归€涙〃閸掑棗顒熸稊鐘电秹閼板啰鐖哄ǎ杈偓鏇氱瑩娑撴俺顕虫潏鍛嚤20楠炶揪绱濋幀鑽ょ波娴滃棜绉寸€圭偟鏁ら惃鍕瘹鐎规碍鏆€閺夋劖鐓$拠銏℃煙濞夋洖寮锋径宥勭瘎閺傝纭堕敍灞炬箒闂団偓鐟曚胶娈戦惇瀣箖閺夛拷
 

//************************
//*        4_9(3).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;j++)
                {
                        cout<<setw(6)<<setiosflags(ios::right)<<" ";
                }
                for (j=i;j<10;j++)
                {
                        cout<<setw(6)<<setiosflags(ios::right)<<i*j;
                }

                cout<<endl;
        }
}

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

int total(int a);
void main()
{
        int n,m;
        cout<<"please input years:"<<endl;
        cin>>n;
        m=total(n);
        cout<<"the total of cows:"<<m<<endl;
}
int total(int a)
{
        int b=0;
        int i;
        i=a/4;
        b=int(pow(2,i));
        return b;
}

第五章

//************************
//*        5_1.cpp       *
//************************
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
int isprime(long n);
void main()
{
        long a,b,l=0;
        cout<<"please input two numbers:/n";
        cin>>a>>b;
        cout<<"primes from "<<a<<" to "<<b<<"is:/n";
        if(a%2==0)
                a++;
        for(long m=a;m<=b;m+=2)
        {
                if(isprime(m))
                {
                        if(l++%10==0)
                                cout<<endl;
                        cout<<setw(5)<<m;
                }
        }
        cout<<endl;
}
int isprime(long n)
{
        //int sqrtn=int(sqrt(n));
        int i,j=0;
        for(i=2;i<n/*sqrtn*/;i++)
        {
                if(n%i==0)
                {
                        j=0;
                        break;
                }
        j=1;
        }
        return j;
}
//************************
//*        5_2.cpp       *
//*    计算定积分        *
//************************
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
double f(double);
double integral(double,double);
void main()
{
        double m,n,c;
        cout<<"please input two point:"<<endl;
        cin>>m>>n;
        c=integral(m,n);
cout<<endl<<"the integral of f(x) from"
        <<m<<" to "<<n<<"is /n"
        <<setiosflags(ios::fixed)
        <<setprecision(8)
        <<setw(10)<<c<<endl;
}
double integral(double a,double b)
{
int n=1;
double h,Tn,T2n,In,I2n;
const double eps=1e-8;
h=b-a;
T2n=I2n=h*(f(a)+f(b))/2;
In=0;
while(fabs(I2n-In)>=eps)
{
        Tn=T2n;
        In=I2n;

       double sigma=0.0;
        for (int k=0;k<n;k++)
        {
                double x=a+(k+0.5)*h;
                sigma+=f(x);
        }
        T2n=(Tn+h*sigma)/2.0;
        I2n=(4*T2n-Tn)/3.0;
        n*=2;
        h/=2;
}
return I2n;
}
double f(double x)
{
        return exp(x)/(1+x*x);
}

//************************
//*        5_3.chh       *
//*                      *
//************************
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
void p1();
void p2();
void p3();
void main()
{
        int a;
        cout<<"please input print sty(1-all,2-down,3-up):"<<endl;
        cin>>a;
        cout<<endl;
        switch(a)
        {
        case 1:p1();break;
        case 2:p2();break;
        case 3:p3();break;
        }
}
void p1()
{
        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;
        }
}
void p2()
{
        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;
        }
        return;
}
void p3()
{
        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;j++)
                {
                        cout<<setw(6)<<setiosflags(ios::right)<<" ";
                }
                for (j=i;j<10;j++)
                {
                        cout<<setw(6)<<setiosflags(ios::right)<<i*j;
                }

               cout<<endl;
        }
        return;
}

//************************
//*        5_4           *
//*                      *
//************************
Main--x=5,y=1,n=1
Func--x=6,y=21,n=11
Main--x=5,y=1,n=11
Func--x=8,y=21,n=31
//************************
//*        5_5.chh       *
//*                      *
//************************
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
int fib(int);
void main()
{
        int n;
        int m;
        cin>>n;
        m=fib(n);
        cout<<"fib("<<n<<""<<"="<<m<<endl;
}
int fib(int a)
{
        if(a>2)
        {
                int tp1=1;
                int tp2=1;
                int temp=0;
                int b;
                for(b=3;b<(a+1);b++)
                {                       
                        temp=tp1+tp2;
                        tp1=tp2;
                        tp2=temp;
                }
                return tp2;
        }
        else return 1;
}
//************************
//*        5_6.chh       *
//*                      *
//************************
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
double poly(int,double);
void main()
{
        int n;
        double x;
        double m;
        cout<<"please input n and x"<<endl;
        cin>>n>>x;
        m=poly(n,x);
        cout<<"poly("<<n<<","<<x<<""<<"="<<m<<endl;
}
double poly(int a,double b)
{
        if(a>1)
        {
                return ((2*a-1)*b*poly((a-1),b)-(a-1)*poly((a-2),b))/2;
        }
        else
        {
                if (a=1)
                        return b;
                else
                        return 1;
        }
}

閻愮懓鍤粩瀣祮閹兼粎鍌�2娑撳洨顫掗懓鍐埡閻㈤潧鐡欓悧鍫gカ閺傛瑱绱�
婢堆囧劥閸掑棛顏㈤棄瀣厴閺勵垳顑囨稉鈧▎陇鈧啰鐖洪敍灞筋嚠娴滃骸顩ф担鏇熺叀閹靛彞绗撴稉姘愁嚦閹稿洤鐣鹃弫娆愭綏閿涘本鍨ㄧ拋鍛婃箒瀵板牆顦块悿鎴︽6閵嗕境ree婢归€涙〃閸掑棗顒熸稊鐘电秹閼板啰鐖哄ǎ杈偓鏇氱瑩娑撴俺顕虫潏鍛嚤20楠炶揪绱濋幀鑽ょ波娴滃棜绉寸€圭偟鏁ら惃鍕瘹鐎规碍鏆€閺夋劖鐓$拠銏℃煙濞夋洖寮锋径宥勭瘎閺傝纭堕敍灞炬箒闂団偓鐟曚胶娈戦惇瀣箖閺夛拷

相关话题/

  • 领限时大额优惠券,享本站正版考研考试资料!
    大额优惠券
    优惠券领取后72小时内有效,10万种最新考研考试考证类电子打印资料任你选。涵盖全国500余所院校考研专业课、200多种职业资格考试、1100多种经典教材,产品类型包含电子书、题库、全套资料以及视频,无论您是考研复习、考证刷题,还是考前冲刺等,不同类型的产品可满足您学习上的不同需求。 ...
    本站小编 Free壹佰分学习网 2022-09-19
閹存劒璐熺拠鍙ュ敩鐞涱煉绱濋崚鍡曢煩鐠囧墽鈻肩挧鍕灐闁剧偓甯寸亸杈厴閼惧嘲褰�40%閹绘劖鍨氱挧姘舵尪閿涳拷
閹恒劌绠嶇挧姘舵尪閺夊啰娉妴鍌濐嚦娴狅綀銆冮崣顖炩偓姘崇箖娴滄帟浠堢純鎴犵搼闁柨绶炴稉鐑樻拱缁旀瑦甯归獮鍨吅娴肩姭鈧钒IP娴兼艾鎲抽垾婵撶礉閻€劍鍩涢柅姘崇箖鐠囧彞鍞悰銊ф畱閸掑棔闊╅柧鐐复閹存牗鎹i幎銉ㄥ枠娑旀澘鎮楅敍宀冾嚦娴狅綀銆冮懢宄板絿40%閹绘劖鍨氶妴鍌濐嚦娴狅綀銆冪拹顓濇嫳閺堫剛鐝禒璁崇秿娴溠冩惂閿涘苯娼庢禍顐㈠綀9閹舵ǜ鈧倸鐨㈤崚鍡曢煩闁剧偓甯撮妴浣规崳閹躲儱娴橀悧鍥╃搼閿涘苯褰傞崚鏉款劅閺嵚ゎ啈閸ф稏鈧胶娅ㄦ惔锕佸垱閸氀佲偓浣镐簳閸楁哎鈧礁浜曟穱掳鈧傅Q缁屾椽妫块妴浣虹叀娑斿簺鈧浇鐪撮悺锝囩搼閸氬嫬銇囬獮鍐插酱閵嗭拷