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

   /2005-05-08

//************************
//*        up.cpp        *
//*                      *
//************************
# include "myhead.h"
void up()
{
        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;
}

//************************
//*       main.cpp       *
//*                      *
//************************
# include "myhead.h"
void main()
{
        all();
        down();
        up();
        return;
}

第7章

//**************
//*   7.1      *
//*  a[5] : 93 *
//**************
# include <iostream.h>
int a[]={34,91,83,56,29,93,56,12,88,72};
int max(int m[],int n);
void main()
{
        int i;
        int j=sizeof(a)/sizeof(int);
        i=max(a,j);
        cout<<"a["<<i<<"]"<<" : "<<a<<endl;
}

int max(int m[],int n)
{
        int k=0;
        int temp=m[k];
        for(int j=0;j<n-1;j++)
        {
                if(temp<m[j])
                {
                        temp=m[j];
                        k=j;
                }
        }
                return k;
}

//**************
//*   7.2      *
//**************
# include <iostream.h>
int a[]={1,11,23,35,46,54,66,78,82,95};
int insert(int l[],int n,int p);/*n为数组的元素个数,p为要插入的数*/
void main()
{
        int q,j,k;
        cin>>q;//q为插入数
        j=sizeof(a)/sizeof(int);
        for(int x=0;x<j;x++)
        {
                cout<<a[x]<<" ";
        }
        cout<<endl;
        k=insert(a,j,q);

        for(int y=0;y<j;y++)
        {
                cout<<a[y]<<" ";
        }
        cout<<endl<<k<<endl;
}

int insert(int l[],int n,int p)
{
        int temp;

        int z;
        for(z=0;l[z]<p;z++);
        z=z-1;//定位z为p所插入位置的前一位置
        if (z==n-1)
        {
                temp=p;
                return temp;
        }
        else
        {
                temp=l[n-1];
                for(int i=n-1;i>z+1;i--)//取z+1是确保当i=z+2时,l[i-1]对应到z+1位置,z位置的元素不动
                {
                        l=l[i-1];
                }//数组元素后移动
                l[z+1]=p;//插入元素
                return temp;
        }
}


//********************
//*        7.3       *
//* 结果为第13个元素 *
//********************
# include <iostream.h>
int a[17];

void main()
{
        for (int i=0;i<18;i++)
        {
                a=1;
        }//将每个元素顺序标注为1(非零)
        int m=0;
        int j=17,l;
        while(1)//每次顺序数3个元素,同时总元素量减1
        {
                for(l=0;l<3;l++)
                {
                        if(a[m]=0)
                        {
                                l=l-1;
                                m=(m+1)%17;//取模
                        }
                        else
                                m=(m+1)%17;
                }
                a[m-1]=0;//将数为3的元素注为零
                j=j-1;
                cout<<m-1<<endl;
                if(j==1) break;
        }
        cout<<endl<<endl<<m-1<<endl;
}

//********************
//*        7.4       *
//********************
# include <iostream.h>

void bouble(int[],int);

void main()
{
        int array[]={55,2,6,4,32,12,9,73,26,37};

        int len=sizeof(array)/sizeof(int);
        for(int i=0;i<len;i++)
                cout<<array<<",";

        cout<<endl<<endl;

        bouble(array,len);
}

void bouble(int a[],int size)
{
        int i,temp;
       
        for(int pass=1;pass<size;pass++)
        {
                int exchange=0;
                for(i=0;i<size-pass;i++)
                {
                        if(a>a[i+1])
                        {
                                temp=a;
                                a=a[i+1];
                                a[i+1]=temp;
                                exchange=1;//有交换时置exchange=1
                        }
                }
                if (exchange==0)
                        break;//在没有交换时,exchange=0,退出循环
                for(i=0;i<size;i++)
                        cout<<a<<",";
                cout<<endl;

        }
}

//********************
//*        7.5       *
//********************
# include <iostream.h>

int a[3][3]={1,2,3,4,5,6,7,8,9};

void main()
{
        int temp1=0;
        for(int i=0;i<3;i++)
        {
                temp1=temp1+a;
        }
        cout<<temp1<<endl;
        int temp2=0;
        for(int j=0;j<3;j++)
        {
                temp2=temp2+a[j][2-j];
        }
        cout<<temp2<<endl;
        cout<<temp1+temp2<<endl;
}


相关话题/

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