《c++程序设计》谭浩强 答案 - 第05章

更新时间:2024-05-27 04:46:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

5.1题

#include #include using namespace std; #include int main()

{int i,j,n,a[101]; for (i=1;i<=100;i++) a[i]=i; a[1]=0;

for (i=2;i

for (i=1,n=0;i<=100;i++) {if (a[i]!=0)

{cout< {cout<

5.2题

#include using namespace std; //#include int main()

{int i,j,min,temp,a[11];

cout<<\ for (i=1;i<=10;i++) {cout<<\

cin>>a[i]; //输入10个数 }

cout<

cout<

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<

for (i=1;i<=10;i++) // 输出已排好序的10个数 cout<

#include using namespace std; int main()

{int a[3][3],sum=0; int i,j;

cout<<\ 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<<\ return 0; } 5.4题

#include using namespace std; int main()

{int a[11]={1,4,6,9,13,16,19,28,40,100}; int num,i,j;

cout<<\ for (i=0;i<10;i++) cout<

cout<<\ 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<<\ for (i=0;i<11;i++) cout<

#include using namespace std; int main()

{ const int n=5; int a[n],i,temp;

cout<<\ for (i=0;i>a[i];

cout<<\ for (i=0;i for (i=0;i

cout<5.6题

#include #include using namespace std;

int main()

{const int n=11; int i,j,a[n][n]; for (i=1;i

for (i=3;i

for (j=2;j<=i-1;j++)

a[i][j]=a[i-1][j-1]+a[i-1][j]; for (i=1;i

cout< cout<

5.7题

#include 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>a[i][j];

for (i=0;i

for (j=0;jmax) {max=a[i][j]; //将本行的最大数存放在max中 maxj=j; //将最大数所在的列号存放在maxj中 }

flag=true; //先假设是鞍点,以flag为真代表 for (int k=0;ka[k][maxj]) //将最大数和其同列元素相比 {flag=false; //如果max不是同列最小,表示不是鞍点令flag1为 假 continue;}

if(flag) //如果flag1为真表示是鞍点 {cout<<\ //输出鞍点的值和所在行列号

break; } }

if(!flag) //如果flag为假表示鞍点不存在 cout<<\ return 0; }

5.8题

#include 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<<\ cin>>a[0]; i=1;

while(i>a[i];

if (a[i]>=a[i-1]) i++; else

cout<<\ }

cout< {cout<<\ cin>>number; sign=false;

top=0; //top是查找区间的起始位置 bott=n-1; //bott是查找区间的最末位置

if ((numbera[n-1])) //要查的数不在查找区间内 loca=-1; // 表示找不到 while ((!sign) && (top<=bott)) {mid=(bott+top)/2; if (number==a[mid]) {loca=mid;

cout<<\ sign=true;

}

else if (number

top=mid+1; }

if(!sign||loca==-1)

cout<>c;

if (c=='N'||c=='n') flag=false; }

return 0; } 5.9题

#include using namespace std; int main()

{int sum_day(int,int); int leap(int year);

int year,month,day,days=0;

cout<<\ cin>>year>>month>>day;

cout<

days=sum_day(month,day); 函数一 */

if(leap(year) && month>=3) 数二 */

days=days+1;

cout<<\ 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

}

/* 调用/* 调用函

int leap(int year) //判断是否为闰年 {int leap;

leap=year%4==0&&year0!=0||year@0==0; return(leap); }

5.10题

#include 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<<\ 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<<\ cout<<\ cout<<\ :\ cout<<\ :\ cout<<\ :\ return 0; }

5.11题

#include using namespace std; int main()

{ char a[5]={'*','*','*','*','*'}; int i,j,k;

char space=' ';

for (i=0;i<5;i++) // 输出5行

{ cout<

cout<

cout<

cout<

5.11题另一解

#include #include 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<

5.12题

#include using namespace std; int main() {int j,n;

char ch[80],tran[80];

cout<<\ gets(ch);

cout<<\ 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<<\ for (j=0;j using namespace std; int main() {int j,n;

char ch[80];

cout<<\ gets(ch);

cout<<\ j=0;

while (ch[j]!='\\0')

{ if ((ch[j]>='A') && (ch[j]<='Z')) ch[j]=155-ch[j];

else if ((ch[j]>='a') && (ch[j]<='z')) ch[j]=219-ch[j]; else

ch[j]=ch[j]; j++; } n=j;

cout<<\ for (j=0;j #include using namespace std; int main() {int j;

string ch=\

tran=ch;

cout<<\ j=0;

while (j<=ch.size())

{ 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++; }

cout<<\ cout< #include using namespace std; int main() {int j;

string ch=\ cout<<\ j=0;

while (j<=ch.size())

{ if ((ch[j]>='A') && (ch[j]<='Z')) ch[j]=155-ch[j];

else if ((ch[j]>='a') && (ch[j]<='z')) ch[j]=219-ch[j]; j++; }

cout<<\ cout< #include using namespace std; int main()

{int j;

string ch=\ cout<<\ j=0;

while (j<=ch.size())

{ if ((ch[j]>='A') && (ch[j]<='Z')) ch[j]=155-ch[j];

else if ((ch[j]>='a') && (ch[j]<='z')) ch[j]=219-ch[j]; j++; }

cout<<\ cout<

#include #include using namespace std; int main()

{char s1[80],s2[40]; int i=0,j=0;

cout<<\ cin>>s1;

cout<<\ cin>>s2;

while (s1[i]!='\\0') i++;

while(s2[j]!='\\0') s1[i++]=s2[j++]; s1[i]='\\0';

cout<<\ return 0; }

5.13另一解

#include using namespace std; int main()

{char s1[80],s2[40];

cout<<\ cin>>s1;

cout<<\ cin>>s2; strcat(s1,s2);

cout<<\ return 0; }

5.13另一解

#include #include using namespace std; int main()

{ string s1=\ cout<<\ cout<<\ s1=s1+s2;

cout<<\ return 0; }

5.14题

#include #include using namespace std; int main()

{ const int n=5; int i,j;

string str[n],temp;

cout<<\ for(i=0;i>str[i]; for(i=0;istr[j+1]) {temp=str[j];str[j]=str[j+1];str[j+1]=temp;} cout<

5.15题

#include

#include using namespace std; int main()

{ const int n=5; string str;

for(int i=0;i

{cout<<\ cin>>str; if(str[0]=='A') cout<

5.16题

#include using namespace std; int main() { const n=10; int i;

char a[n],temp;

cout<<\ for(i=0;i>a[i]; for(i=0;i

{temp=a[i];a[i]=a[n-i-1];a[n-i-1]=temp;} for(i=0;i 5.16题另一解 #include #include using namespace std; int main() { string a; int i,n; char temp;

cout<<\

cin>>a; n=a.size();

for(i=0;i

{temp=a[i];a[i]=a[n-i-1];a[n-i-1]=temp;} cout<

5.17题

#include #include 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<

cout<

void input_data() {int i;

for (i=0;i

{cout<<\ cin>>name[i]>>num[i]>>score[i];} }

本文来源:https://www.bwwdw.com/article/6yg7.html

Top