⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c++.txt

📁 标准答案习题1 1.1 简述C++语言程序的结构特点。 答: (1)C++程序由一个或多个函数组成
💻 TXT
📖 第 1 页 / 共 5 页
字号:
   ave=sum/N;
   cout<<"ave="<<ave<<'\t'<<"count="<<count<<endl;
}        
 
4.8设有一个数列,它的前四项为0、0、2、5,以后每项分别是其前四项之和,编程求此数列的前20项。用一维数组完成此操作。(提示:a[i]=a[i-1]+a[i-2]+a[i-3]+a[i-4];)
解:
#include <iostream.h>
#include <iomanip.h>
#define N 20
void main(void)
{  long i,a[N+1];
   a[1]=a[2]=0;
   a[3]=2;
   a[4]=5; 
   for (i=5;i<=N;i++)
     a[i]=a[i-1]+a[i-2]+a[i-3]+a[i-4];
   for (i=1;i<=N;i++)
   {  cout<<setw(8)<<a[i];
      if (i%5==0)  
      cout<<endl;
   }
}
   
4.9某班有30个学生,进行了数学考试,编写程序将考试成绩输入一维数组,并将数学成绩用冒泡法、选择法与擂台法三种排序算法,由高到低的顺序排序后输出。
解:方法一:冒泡法
#include <iostream.h>
#include <iomanip.h>
#define N 30
void main(void)
{  float math[N];
   int i,j,temp;
   cout<<"Input math score:"<<endl;
   for(i=0;i<N;i++)
      cin>>math[i] ;
   for(i=0;i<N-1;i++)
     for(j=0;j<=N-1-i;j++)
        {  if  (math[j]<math[j+1])
              {  temp=math[j];
           math[j]=math[j+1];
           math[j+1]=temp;
              }
       }
       for (i=0;i<N;i++)
       {  cout<<setw(6)<<math[i]<<endl;
    }
}
方法二:选择法
#include <iostream.h>
#include <iomanip.h>
#define N 30
void main(void)
{  float math[N];
   int i,j,temp;
   cout<<"Input math score:"<<endl;
   for(i=0; i<N;i++)
      cin>>math[i];
   for(i=0;i<N-1;i++)
     for(j=i+1; j<N;j++)
        {  if  (math[i]<math[j]) 
              {     temp=math[i];
                     math[i]=math[j];
                     math[j]=temp;
              }
       }
       for (i=0;i<N;i++)
         cout<<setw(6)<<math[i]<<endl;
}
方法三:擂台法
#include <iostream.h>
#include <iomanip.h>
#define N 30
void main(void)
{  float math[N];
   int i,j,temp,k;
   cout<<"Input math score:"<<endl;
   for(i=0; i<N;i++)
      cin>>math[i];
   for(i=0;i<N-1;i++)
    { k=i;
      for(j=i+1; j<N;j++)
           if  (math[k]<math[j]) k=j;
        if (k>i)
     { temp=math[i]; math[i]=math[k]; math[k]=temp;}
}
for (i=0;i<N;i++)
         cout<<setw(6)<<math[i]<<endl;
}
 
4.10.已有一按从小到大次序排序好的数组,现输入一数,要求按原来排序的规律将它插入到数组中。
解:先定位、向后移,再插入
#include <iostream.h>
#include <iomanip.h>
#define N 10
void main(void)
{  float a[N];
   int i,b,j;
   cout<<"Input sort array a[9]:"<<endl;
   for(i=0;i<N-1;i++)
      cin>>a[i];
   cout<<"Input number b:";
   cin>>b;
   i=0;
   while (a[i]<b) i++; 
   for(j=N-1;j>i;j--)  a[j]=a[j-1];
   a[i]=b;
   for (i=0;i<N;i++)
     cout<<setw(6)<<a[i]<<endl;
}
 
4.11定义一个二维数组,用编程的方法形成如下矩阵(数据不能通过键盘输入),并按下列格式输出矩阵。(提示:当i>=j时,a[i][j]=1;当i<j时,a[i][j]=j-i+1)
 
     A =
 
 
 
解:
#include <iostream.h>
#include <iomanip.h>
#define N 5
void main(void)
{  float a[N][N];
   int i,j;
   for(i=0; i<N;i++)
   for(j=0; j<N;j++)
     {  if (i>=j)  a[i][j]=1;
        else  a[i][j]=j-i+1;
     } 
   for(i=0; i<N;i++)
   {  for(j=0; j<N;j++)
        cout<<setw(6)<<a[i][j];
         cout<<endl;
   }
}
 
4.12设计一个程序,打印杨辉三角形。
1
1   1
1   2   1
1           3   3   1
1           4   6   4   1
1           5  10  10   5    1
1           6  15  20  15    6    1
1           7  21  35  35    21   7   1
1           8  28  56  70    56   28  8  1
1   9  36  84  126  126   84  36  9  1
解:方法一
杨辉三角形第n行第m个元素值为多项式(1+x)n 的系数 值。
如第4行各元素值为:
        所以只要计算出组合数 即可得到第n行第m个元素值。
用二重循环即可打印出杨辉三角形。在输出时每行控制输出的空格,以达到图示的效果。
#include <iostream.h>
#include <iomanip.h>
#define N 11
void main(void)
{  int c[N][N],m,n,k,m1,n1,n_m;
   for(n=0; n<N;n++)
     for(m=0; m<=n;m++)
     {  for(m1=1,k=1;k<=m;k++)  m1*=k;
        for(n1=1,k=1;k<=n;k++)  n1*=k;
        for(n_m=1,k=1;k<=n-m;k++)  n_m*=k;
        c[n][m]=n1/(m1*n_m);
     }
    for(n=0;n<N;n++)
    { if(n%2==0)cout<<”   ” ;
      for (m=0;m<(N-n)/2;m++)
        cout<<setw(6)<< ‘ ‘;
for(m=0; m<=n;m++)
         cout<<setw(6)<<c[n][m];
      cout<<endl;
    }
}
方法二
从杨辉三角形各元素数值可以看出如下规律:
(1)第一列和对角线元素值为1
(2)其它其元素值为c[n][m]=c[n-1][m-1]+c[n-1][m]
由此可编写程序如下:
#include <iostream.h>
#include <iomanip.h>
#define N 11
void main(void)
{  int c[N][N],m,n,k,m1,n1,nm1;
   for(n=1;n<N;n++)
   {  c[n][n]=1;
       c[n][1]=1;
   }
   for(n=3; n<N;n++)
     for(m=2; m<=n-1;m++)
       c[n][m]=c[n-1][m-1]+c[n-1][m];
   for(n=1; n<N;n++)
   {  if(n%2==0)cout<<”   ” ;
      for (m=0;m<(N-n)/2;m++)
        cout<<setw(6)<< ‘ ‘;
for(m=1;m<=n;m++)
         cout<<setw(6)<<c[n][m];
      cout<<endl;
   }
}
 
4.13 输入一个字符串,求出字符串长度(不能用strlen函数),输出字符串及其长度。
解:
#include <iostream.h>
#include <iomanip.h>
#define N 30
void main(void)
{  char str[N];
   int k,l,len;
   cout<<"Input a String"<<endl;
   cin>>str;
   len=0;
   k=0;
   while(str[k]!=0) 
   {  k++;
      len++;
   }
   cout<<str<<endl;
   cout<<" Length of String:"<<len<<endl;
}
 
4.14输入一行字符,分别统计出其中英文字母、空格、数字字符和其它字符的个数。
解: 
#include <iostream.h>
#include <iomanip.h>
#define N 30
void main(void)
{   char str[N];
    int f,i,k1,k2,k3,k4;
    cout<<"Input a String"<<endl;
    cin.getline(str,N);
    k1=0;
    k2=0;
    k3=0;
    k4=0;
    i=0;
    while(str[i]!=0)
    {  if ((str[i]>=0x41) && (str[i]<=0x5a) || (str[i]>=0x61) && (str[i]<=0x7a)) 
k1++;
          else if ((str[i]>=0x30) && (str[i]<=0x39)) k2++;
          else if (str[i]==0x20) k3++;
          else k4++;
       i++;
   }
   cout<<"字母:"<<k1<<"个\t数字:"<<k2<<"个\t空格:"<<k3<<"个\t其它:"<<k4<<"个"<<endl;
}
 
4.15编一程序,对从键盘输入的两个字符串进行比较,然后输出两个字符串中第一个不同字符的ASCII码之差。例如:输入的两个字符串分别为“abcdef”和“abceef”,则第一个不同字符为“d”和“e”,输出为-1。
解:
#include <iostream.h>
#include <string.h>
#define N 30
void main(void)
{  char s1[N],s2[N];
   int i,len;
   cout<<"Input  first String"<<endl;
   cin>>s1;
   cout<<"Input  second  String"<<endl;
   cin>>s2;
   if (strlen(s1)>strlen(s2))  len=strlen(s2);
   else len=strlen(s1);
   i=0;
   while (i<len && s1[i]==s2[i]) i++;
     if (i==len) 
       cout<<"s1=s2";
     else
       cout<<"s1["<<i<<"]-s2["<<i<<"]="<<s1[i]-s2[i]<<endl;
}
 
4.16从键盘输入三个字符串,将其合并成一个字符串,并求出合并后字符串的长度。
解:
#include <iostream.h>
#include <string.h>
#define N 30
void main(void)
{   char s1[N],s2[N],s3[N],s[3*N];
    cout<<"Input  first String"<<endl;
    cin>>s1;
    cout<<"Input  second  String"<<endl;
    cin>>s2;
    cout<<"Input  third  String"<<endl;
    cin>>s3;
    strcpy(s,s1);
    strcat(s,s2);
    strcat(s,s3);
    cout<<"s="<<s<<endl;
 }
 
4.17已知某运动会上男子百米赛跑决赛成绩。要求编写程序,按成绩排序并按名次输出排序结果,包括输出名次、运动员号码和成绩三项内容。
解:
用M行3列数组存放运动员号码、成绩与名次,对决赛成绩降序排序,最后按排序后的位置输入名次。
# include <iostream.h>
#include <iomanip.h>
# define M 5
void main(void)
{   int a[M][3],i,j,k,temp;
    cout<<"Input No and final grade:";
    for (i=0;i<M;i++)
      cin>>a[i][0]>>a[i][1];  
    for (i=0;i<M-1;i++)
       {  k=i;
       for(j=i+1;j<M;j++)
       if (a[k][1]<a[j][1]) k=j; 
       if (k!=i) 
          {  temp=a[i][0];a[i][0]=a[k][0];a[k][0]=temp;
          temp=a[i][1];a[i][1]=a[k][1];a[k][1]=temp;
       }
       }
    for (i=0;i<M;i++)  a[i][2]=i+1;
    cout<<setw(6)<<"No." <<setw(6)<<"Grade" <<setw(6)<<"Order"<<endl;
    
cout<<"-------------------------------------------------------------------"<<endl;
       for (i=0;i<M;i++)
       { for (j=0;j<3;j++)    
        cout<<setw(6)<<a[i][j];
      cout<<endl;
    }
    
cout<<"-------------------------------------------------------------------"<<endl;
}
 
4.18 
某小组有5个学生,考了3门课程,他们的学号及成绩如表4.2所示,试编程求每个学生的总成绩及每门课的平均成绩,并按表格形式输出每个学生的学号、3门课程成绩、总成绩及各门课程的平均成绩。要求用一个6行5列的数组完成上述操作。
表4.3  学生成绩情况表
      学 号数 学语 文外 语总成绩
      1001908085 
      1002707580 
      1003657075 
      1004855060 
      1005809070 
      最高分    

 
#include <iostream.h>
#include <iomanip.h>
#define M 6
#define N 5
void main(void)
{   float s[M][N],sum,max;
    int i,j;
    cout<<"Input data:\n";                                //输入数据
    for (i=0;i<M-1;i++)                                          
//输入5个学生的学号与3门课成绩
    {  for (j=0;j<N-1;j++)
          cin>>s[i][j];
    }
    for (i=0;i<M-1;i++)                                          //处理数据
    {  sum=0.0;
       for (j=1;j<N-1;j++)                               //计算每个学生的总成绩
          sum=sum+s[i][j];
       s[i][N-1]=sum;                                //计算每个学生的总成绩
    }
     for (j=1;j<N;j++)                                       //处理数据
    {  max=s[0][j];
       for (i=0;i<M-1;i++)                              //处理计算每门课程
          if (max<s[i][j])
                       max=s[i][j];
           s[M-1][j]=max;                             //计算每门课程的最高分
    }
    cout<<setw(5)<<" Num. "<<"  Math.  Chin.  Engl.  Sum."<<endl;      //输出数据
    cout<<"-----------------------------------------------------------------\n";
    for (i=0;i<M;i++)
    {  for (j=0;j<N;j++)                              //输出学号、3门课程的成绩与总分
          if (i==M-1 && j==0) cout<<setw(6)<<" 最高分 ";
                else cout<<setw(6)<<s[i][j];
       cout<<endl;
    } 
    
cout<<"------------------------------------------------------------------\n";
 }
 
4.19 对4.18题中学生成绩表用擂台法按总成绩排序后输出,并输出每门课程不及格学生的成绩信息。
#include <iostream.h>
#include <iomanip.h>
#define M 6
#define N 5
void main(void)
{   float s[M][N],sum,ave,temp;
    int i,j,k;
    cout<<"Input data:\n";                                //输入数据
    for (i=0;i<M-1;i++)                                          
//输入5个学生的学号与3门课成绩
    {  for (j=0;j<N-1;j++)
          cin>>s[i][j];
    }
    for (i=0;i<M;i++)                                      //处理数据
    {  sum=0.0;
       for (j=1;j<N-1;j++)                               //计算每个学生的总成绩
          sum=sum+s[i][j];
       s[i][N-1]=sum;                                //计算每个学生的总分
    }
    for (j=1;j<N;j++)                                       //处理数据
    {  max=s[0][j];
       for (i=0;i<M-1;i++)                              //处理计算每门课程
          if (max<s[i][j])
                       max=s[i][j];
           s[M-1][j]=max;                             //计算每门课程的最高分
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -