C++程序设计(第三版)谭浩强 第五章课后习题答案

1.用筛法求100之内的素数。

#include <iostream>
#include <iomanip>
using namespace std;
#include <math.h>
int main()
 {int i,j,n,a[101];
  for (i=1;i<=100;i++)
    a[i]=i;
  a[1]=0;
  for (i=2;i<sqrt(100);i++)
    for (j=i+1;j<=100;j++)
       {if(a[i]!=0 && a[j]!=0)
	      if (a[j]%a[i]==0)
	       a[j]=0;  }
    cout<<endl;
    for (i=1,n=0;i<=100;i++)
     {if (a[i]!=0)
       {cout<<setw(5)<<a[i]<<" ";
        n++;}
      if(n==10)
        {cout<<endl;
   	     n=0;}
     }
	cout<<endl;
	return 0;
   } 

2.用选择法对10个整数排序。

#include <iostream>
using namespace std;
//#include <math.h>
int main()
  {int i,j,min,temp,a[11];
   cout<<"enter data:"<<endl;
   for (i=1;i<=10;i++)
   {cout<<"a["<<i<<"]=";
    cin>>a[i];                   //输入10个数 
   }
   cout<<endl<<"The original numbers:"<<endl;;
   for (i=1;i<=10;i++)
     cout<<a[i]<<" ";           // 输出这10个数 
   cout<<endl;;
   for (i=1;i<=9;i++)           //以下8行是对10个数排序 
     {min=i;
      for (j=i+1;j<=10;j++)
        if (a[min]>a[j]) min=j;
         temp=a[i];             //以下3行将a[i+1]~a[10]中最小者与a[i] 对换
         a[i]=a[min];
         a[min]=temp;
      }
   cout<<endl<<"The sorted numbers:"<<endl;
   for (i=1;i<=10;i++)          // 输出已排好序的10个数 
     cout<<a[i]<<" ";
   cout<<endl;
   return 0;  
}  

3.求一个3x3 矩阵对角线元素之和。

#include <iostream>
using namespace std;
int main()
 {int a[3][3],sum=0;
  int i,j;
  cout<<"enter data:"<<endl;;
   for (i=0;i<3;i++)
     for (j=0;j<3;j++)
       cin>>a[i][j];
   for (i=0;i<3;i++)
     sum=sum+a[i][i];
   cout<<"sum="<<sum<<endl;
   return 0;
  }

4.求一个己排好序的数组,输入一个数,要求按原来排序的规律将它插入数组中。

#include <iostream>
using namespace std;
int main()
 {int a[11]={1,4,6,9,13,16,19,28,40,100};
   int num,i,j;
   cout<<"array a:"<<endl;
   for (i=0;i<10;i++)
     cout<<a[i]<<" ";
   cout<<endl;;
   cout<<"insert data:";
   cin>>num;
   if (num>a[9])
     a[10]=num;
   else
    {for (i=0;i<10;i++)
     {if (a[i]>num)
       {for (j=9;j>=i;j--)
         a[j+1]=a[j];
        a[i]=num;
//		break;
       }
      }
     }
  cout<<"Now, array a:"<<endl;
  for (i=0;i<11;i++)
    cout<<a[i]<<" ";
  cout<<endl;
  return 0;
 }

5.将一个数组中的值按逆序重新存放。例如,原来顺序为8,6,5,4,1。要求改为1, 4,5, 6,8。

#include <iostream>
using namespace std;
int main()
{ const int n=5;
  int a[n],i,temp;
  cout<<"enter array a:"<<endl; 
  for (i=0;i<n;i++)
    cin>>a[i];
  cout<<"array a:"<<endl;
  for (i=0;i<n;i++)
   cout<<a[i]<<" ";
  for (i=0;i<n/2;i++)            //循环的作用是将对称的元素的值互换
    { temp=a[i];
      a[i]=a[n-i-1];
      a[n-i-1]=temp;
     }
  cout<<endl<<"Now,array a:"<<endl;
  for (i=0;i<n;i++)
    cout<<a[i]<<" ";
  cout<<endl;
  return 0;
}   

在这里插入图片描述

#include <iostream>
#include <iomanip>
using namespace std;
int main()
 {const int n=11;
  int i,j,a[n][n];
  for (i=1;i<n;i++)
    {a[i][i]=1;
     a[i][1]=1;
    }
  for (i=3;i<n;i++)
    for (j=2;j<=i-1;j++)
       a[i][j]=a[i-1][j-1]+a[i-1][j];
  for (i=1;i<n;i++)
    {for (j=1;j<=i;j++)
       cout<<setw(5)<<a[i][j]<<" ";
     cout<<endl;
    }
  cout<<endl;
  return 0;
}

7.找出一个二维数组中的鞍点,即该位置上的元素在该行上最大,在该列上最小(也可能没有鞍点)。

#include <iostream>
using namespace std;
int main()
{ const int n=4,m=5;        //假设数组为4行5列     
  int i,j,a[n][m],max,maxj;
  bool flag;
  for (i=0;i<n;i++)         //输入数组
     for (j=0;j<m;j++)
      cin>>a[i][j];
  for (i=0;i<n;i++)         
   {max=a[i][0]; maxj=0;    
    for (j=0;j<m;j++)      //找出第i行中的最大数
	  if (a[i][j]>max)
	    {max=a[i][j];      //将本行的最大数存放在max中
	     maxj=j;           //将最大数所在的列号存放在maxj中
	    }
    flag=true;            //先假设是鞍点,以flag为真代表
    for (int k=0;k<n;k++)
	  if (max>a[k][maxj])  //将最大数和其同列元素相比
	     {flag=false;     //如果max不是同列最小,表示不是鞍点令flag1为假
	      continue;}
    if(flag)             //如果flag1为真表示是鞍点
	{cout<<"a["<<i<<"]["<<"["<<maxj<<"]="<<max<<endl;
	                     //输出鞍点的值和所在行列号
	 break;
	}
  } 
  if(!flag)	            //如果flag为假表示鞍点不存在
    cout<<"It does not exist!"<<endl;
   return 0;
  } 

8.有15个数按由大到小顺序存放在一个数组中,输入一个数,要求用折半查找法找出该数是数组中第几个元素的值。如果该数不在数组中,则打印出"无此数"。

#include <iostream>
using namespace std;
int main()
{ const int n=7;
  int i,number,top,bott,mid,loca,a[n];
  bool flag=true,sign;
  char c;
  cout<<"enter data:"<<endl;;
  cin>>a[0];
  i=1;
  while(i<n)
   {cin>>a[i];
    if (a[i]>=a[i-1])
      i++;
    else
      cout<<"enter this data again:";
   }
  cout<<endl;
  for (i=0;i<n;i++)
    cout<<a[i]<<" ";
  cout<<endl;
  while(flag)
    {cout<<"input number to look for:";
     cin>>number;
     sign=false;
     top=0;            //top是查找区间的起始位置
     bott=n-1;         //bott是查找区间的最末位置
     if ((number<a[0])||(number>a[n-1]))  //要查的数不在查找区间内
       loca=-1;        // 表示找不到
     while ((!sign) && (top<=bott))
       {mid=(bott+top)/2;
        if (number==a[mid])
         {loca=mid;
          cout<<"Find "<<number<<", its position is "<<loca+1<<endl;
		  sign=true;
         }
        else if (number<a[mid])
         bott=mid-1;
        else
        top=mid+1;
       }
     if(!sign||loca==-1)
       cout<<number<<" has not found."<<endl;;
     cout<<"continu or not(Y/N)?";
     cin>>c;
     if (c=='N'||c=='n')
	   flag=false;
    }
   return 0;
 } 

9.给出年,月、日、计算该日是该年的第几天。

#include <iostream>
using namespace std;
int main()
  {int sum_day(int,int);
   int leap(int year);
   int year,month,day,days=0;
   cout<<"input date(year,month,day):";
   cin>>year>>month>>day;
   cout<<year<<"/"<<month<<"/"<<day;
   days=sum_day(month,day);                                   /* 调用函数一 */
   if(leap(year) && month>=3)                                 /* 调用函数二 */
     days=days+1;
   cout<<" is the "<<days<<"th day in this year."<<endl;
   return 0;
   }


int sum_day(int month,int day)          //计算日期 
  {int i; 
   int day_tab[12]={31,28,31,30,31,30,31,31,30,31,30,31};  
   for (i=0;i<month-1;i++)
      day+=day_tab[i];
   return(day);
  }                    
     
int leap(int year)                     //判断是否为闰年 
 {int leap;
  leap=year%4==0&&year%100!=0||year%400==0;
  return(leap);
 } 

10.有一篇文章,共有3行文字,每行有80个字符。要求分别统计出英文大写字母,小写字母,数字,空格以及其他字符的个数。

#include <iostream>
using namespace std;
int main()
{int i,j,upper,lower,digit,space,other;
 char text[3][80];
 upper=lower=digit=space=other=0;
 for (i=0;i<3;i++)
   {cout<<"please input line "<<i+1<<endl;
    gets(text[i]);
    for (j=0;j<80 && text[i][j]!='\0';j++)
      {if (text[i][j]>='A'&& text[i][j]<='Z')
         upper++;
       else if (text[i][j]>='a' && text[i][j]<='z')
         lower++;
       else if (text[i][j]>='0' && text[i][j]<='9')
         digit++;
       else if (text[i][j]==' ')
         space++;
       else
         other++;
     }
   }
   cout<<"upper case:"<<upper<<endl;
   cout<<"lower case:"<<lower<<endl;
   cout<<"digit     :"<<digit<<endl;
   cout<<"space     :"<<space<<endl;
   cout<<"other     :"<<other<<endl;
   return 0;
} 

11.在这里插入图片描述
(1)用字符数组方法

#include <iostream>
using namespace std;
int main()
{ char a[5]={'*','*','*','*','*'};
  int i,j,k;
  char space=' ';
  for (i=0;i<5;i++)              // 输出5行 
   { cout<<endl;                 // 输出每行前先换行 
     cout<<"    ";               // 每行前面留4个空格 
     for (j=1;j<=i;j++)
       cout<<space;              // 每行再留一个空格 
     for (k=0;k<5;k++)
       cout<<a[k];               // 每行输出5个*号
	}   
  cout<<endl;
   return 0;
} 

(2)用string方法

#include <iostream>
#include <string>
using namespace std;
int main()
{ string stars="*****";
  int i,j;
  for (i=0;i<5;i++)              // 输出5行 
   { cout<<"    ";               // 每行前面留4个空格
     for (j=1;j<=i;j++)
       cout<<" ";               // 每行再插入i个空格    
     cout<<stars<<endl;         // 输出5个*号
  }   
  return 0;
} 

在这里插入图片描述

#include <iostream>
using namespace std;
int main()
 {int j,n;
  char ch[80],tran[80];
  cout<<"input cipher code:";
  gets(ch);
  cout<<"cipher code:"<<ch<<endl;
  j=0;
  while (ch[j]!='\0')
  { if ((ch[j]>='A') && (ch[j]<='Z'))
      tran[j]=155-ch[j];
    else if ((ch[j]>='a') && (ch[j]<='z'))
      tran[j]=219-ch[j];
    else
      tran[j]=ch[j];
    j++;
  }
  n=j;
  cout<<"original text:";
  for (j=0;j<n;j++)
    putchar(tran[j]);
  cout<<endl;
  return 0;
 }	

13.编写一程序,将两个字符串连接起来,结果取代第一个字符串。
(1)用字符数组,不用 strcat函数(即自己写一个具有strcat函数功能的函数)

#include <iostream>
#include <string>
using namespace std;
int main()
{char s1[80],s2[40];
  int i=0,j=0;
  cout<<"input string1:";
  cin>>s1;
  cout<<"input string2:";
  cin>>s2;
  while (s1[i]!='\0')
    i++;
  while(s2[j]!='\0')
    s1[i++]=s2[j++];
  s1[i]='\0';
  cout<<"The new string is:"<<s1<<endl;
  return 0;
}

(2)用标准库中的strcat函数

#include <iostream>
using namespace std;
int main()
{char s1[80],s2[40];
  cout<<"input string1:";
  cin>>s1;
  cout<<"input string2:";
  cin>>s2;
  strcat(s1,s2);
  cout<<"The new string is:"<<s1<<endl;
  return 0;
}

(3)用string方法定义字符串变量

#include <iostream>
#include <string>
using namespace std;
int main()
{ string s1="week",s2="end";
  cout<<"s1="<<s1<<endl;
  cout<<"s2="<<s2<<endl;
  s1=s1+s2;
  cout<<"The new string is:"<<s1<<endl;
  return 0;
}

14.输入n个字符串,将它们按字母由大到小的顺序排列输出

#include <iostream>
#include <string>
using namespace std;
int main()
{ const int n=5;
  int i,j;
  string str[n],temp;
  cout<<"please input strings:"<<endl;
  for(i=0;i<n;i++)
	  cin>>str[i];
  for(i=0;i<n-1;i++)
    for(j=0;j<n-i-1;j++)
      if(str[j]>str[j+1])
	  {temp=str[j];str[j]=str[j+1];str[j+1]=temp;}
  cout<<endl<<"sorted strings:"<<endl;
  for(i=0;i<n;i++)
	  cout<<str[i]<<endl;
  return 0;
}

15.输入n个字符串,将其中以字母A打头的字符串输出

#include <iostream>
#include <string>
using namespace std;
int main()
{ const int n=5;
  string str;
  for(int i=0;i<n;i++)
     {cout<<"please input string:";
      cin>>str;
	  if(str[0]=='A')
		  cout<<str<<endl;}
  return 0;
}

16.输入一个字符串,将其中的字符按逆序输出,如输入LIGHT,输出THGIL
(1)用字符数组方法

#include <iostream>
using namespace std;
int main()
{ const n=10;
  int i;
  char a[n],temp;
  cout<<"please input a string:";
  for(i=0;i<n;i++)
	  cin>>a[i];
  for(i=0;i<n/2;i++)
  {temp=a[i];a[i]=a[n-i-1];a[n-i-1]=temp;}
  for(i=0;i<n;i++)
	  cout<<a[i];
  cout<<endl;
  return 0;
}

(2)用string方法

#include <iostream>
#include <string>
using namespace std;
int main()
{ string a;
  int i,n;
  char temp;
  cout<<"please input a string:";
  cin>>a;
  n=a.size();
  for(i=0;i<n/2;i++)
  {temp=a[i];a[i]=a[n-i-1];a[n-i-1]=temp;}
  cout<<a<<endl;
  return 0;
}

17.输入10个学生的姓名、学号和成绩,将其中不及格者的姓名、学号和成绩输出。

#include <iostream>
#include <string>
using namespace std;
const int n=10;
string name[n];
int num[n],score[n];
int main()
{int i;
 void input_data();
 input_data();
 cout<<endl<<"The list of failed:"<<endl;
 for(i=0;i<n;i++)
   if(score[i]<60)
     cout<<name[i]<<" "<<num[i]<<"  "<<score[i]<<endl;
  return 0;
}

void input_data()
{int i;
 for (i=0;i<n;i++)
   {cout<<"input name,number and score of student "<<i+1<<":";
    cin>>name[i]>>num[i]>>score[i];}
}
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坏男孩!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值