C++第三章课后习题标准答案谭浩强.docx

上传人:b****5 文档编号:11671646 上传时间:2023-03-30 格式:DOCX 页数:54 大小:45.94KB
下载 相关 举报
C++第三章课后习题标准答案谭浩强.docx_第1页
第1页 / 共54页
C++第三章课后习题标准答案谭浩强.docx_第2页
第2页 / 共54页
C++第三章课后习题标准答案谭浩强.docx_第3页
第3页 / 共54页
C++第三章课后习题标准答案谭浩强.docx_第4页
第4页 / 共54页
C++第三章课后习题标准答案谭浩强.docx_第5页
第5页 / 共54页
点击查看更多>>
下载资源
资源描述

C++第三章课后习题标准答案谭浩强.docx

《C++第三章课后习题标准答案谭浩强.docx》由会员分享,可在线阅读,更多相关《C++第三章课后习题标准答案谭浩强.docx(54页珍藏版)》请在冰豆网上搜索。

C++第三章课后习题标准答案谭浩强.docx

C++第三章课后习题标准答案谭浩强

3.2题

#include

#include<iomanip>

usingnamespacestd;

intmain()

{float h,r,l,s,sq,vq,vz;

constfloat pi=3.1415926;

cout<<"please enter r,h:

";

cin>>r>>h;

l=2*pi*r;

s=r*r*pi;

 sq=4*pi*r*r;

vq=3.0/4.0*pi*r*r*r;

vz=pi*r*r*h;

cout<

:

fixed)<<setiosflags(ios:

:

right)

 <<setprecision

(2);

cout<<"l="<

cout<<"s="<

cout<<"sq="<<setw(10)<

cout<<"vq="<

 cout<<"vz="<

 return 0;

}  

3.3题

#include

usingnamespacestd;

intmain()

{floatc,f;

cout<<"请输入一个华氏温度:

";

 cin>>f;

c=(5.0/9.0)*(f-32);//注意5和9要用实型表示,否则5/9值为0

cout<<"摄氏温度为:

"<

return0;

};

3.4题

#include 

usingnamespace std;

int main ( )

{charc1,c2;

cout<<"请输入两个字符c1,c2:

";

c1=getchar();   //将输入的第一个字符赋给c1

c2=getchar();   //将输入的第二个字符赋给c2

cout<<"用putchar函数输出结果为:

";

 putchar(c1);

putchar(c2);

cout<<endl;

cout<<"用cout语句输出结果为:

";

cout<

return0;

}

3.4题另一解

#include

using namespacestd;

int main()

{charc1,c2;

cout<<"请输入两个字符c1,c2:

";

 c1=getchar();   //将输入的第一个字符赋给c1

c2=getchar();   //将输入的第二个字符赋给c2

 cout<<"用putchar函数输出结果为:

";

putchar(c1);

 putchar(44);

putchar(c2);

cout<

cout<<"用cout语句输出结果为:

";

 cout<

return0;

}

3.5题

#include

usingnamespacestd;

intmain()

{char c1,c2;

inti1,i2;        //定义为整型

 cout<<"请输入两个整数i1,i2:

";

cin>>i1>>i2;

c1=i1;

 c2=i2;

cout<<"按字符输出结果为:

"<

return0;

}

3.8题

#include

usingnamespacestd;

intmain()

{inta=3,b=4,c=5,x,y;

cout<<(a+b>c&&b==c)<<endl;

cout<<(a||b+c&&b-c)<<endl;

cout<<(!

(a>b) &&!

c||1)<

 cout<<(!

(x=a)&&(y=b)&&0)<<endl;

cout<<(!

(a+b)+c-1&& b+c/2)<<endl;

return0;

} 

3.9题

include

using namespacestd;

intmain ( )

{inta,b,c;

 cout<<"pleaseenterthreeintegernumbers:

";

cin>>a>>b>>c;

 if(a<b)

  if(b<c)

  cout<<"max="<

else

   cout<<"max="<<b;

 elseif(a

  cout<<"max="<

 else

   cout<<"max="<

 cout<<endl;

ﻩreturn0;

 }

3.9题另一解

#include 

using namespacestd;

intmain( )

{int a,b,c,temp,max ;

 cout<<"please enterthreeintegernumbers:

";

cin>>a>>b>>c;

  temp=(a>b)?

a:

b;  /*将a和b中的大者存入temp中*/

 max=(temp>c)?

temp:

c; /*将a和b中的大者与c比较,最大者存入max

*/

cout<<"max="<<max<<endl;

return0;

3.10题

#include

usingnamespacestd;

intmain()

{intx,y;

cout<<"enter x:

";

cin>>x;

  if(x<1)   

 {y=x;

  cout<<"x="<

  }

 elseif(x<10)   // 1≤x<10

 {y=2*x-1;

  cout<<"x="<

}

 else     // x≥10

    {y=3*x-11;

  cout<<"x="<<x<<", y=3*x-11="<

   }

  cout<<endl;

ﻩreturn0;

}

3.11题

#include<iostream>

usingnamespacestd;

intmain()

{float score;

  chargrade;

cout<<"pleaseenterscoreofstudent:

";

 cin>>score;

while(score>100||score<0)

{cout<<"data error,enter data again.";

 cin>>score;

 }

 switch(int(score/10))

{case10:

 case 9:

grade='A';break;

 case8:

 grade='B';break;

  case7:

grade='C';break;

 case 6:

grade='D';break;

  default:

grade='E';    

  }

cout<<"scoreis"<

 return 0;

}

3.12题

#include <iostream>

usingnamespace std;

intmain()

{longintnum;

 intindiv,ten,hundred,thousand,ten_thousand,place;

            /*分别代表个位,十位,百位,千位,万位和位数

*/

 cout<<"enteran integer(0~99999):

";

cin>>num;

 if(num>9999)

 place=5;

else if(num>999)

  place=4;

elseif (num>99)

    place=3;

else if(num>9)

place=2;

 elseplace=1;

 cout<<"place="<<place<

//计算各位数字

ten_thousand=num/10000;

thousand=(int)(num-ten_thousand*10000)/1000;

 hundred=(int)(num-ten_thousand*10000-thousand*1000)/100;

ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10;

 indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);

cout<<"originalorder:

";

 switch(place)

   {case 

5:

cout<<ten_thousand<<","<

dl;

ﻩ  cout<<"reverseorder:

";

  cout<<indiv<

ﻩbreak;

case4:

cout<

 cout<<"reverse order:

";

ﻩ cout<<indiv<

ﻩ  break;

 case3:

cout<<hundred<<","<

ﻩ  cout<<"reverseorder:

";

ﻩ cout<<indiv<<ten<

ﻩ   break;

  case2:

cout<

ﻩcout<<"reverseorder:

";

 cout<<indiv<

ﻩ  break;

  case 1:

cout<

ﻩ  cout<<"reverseorder:

";

ﻩcout<

break;

return0;

}ﻩ

3.13题

#include<iostream>

usingnamespacestd;

intmain()

{longi;       //i为利润

 floatbonus,bon1,bon2,bon4,bon6,bon10;

bon1=100000*0.1; //利润为10万元时的奖金

bon2=bon1+100000*0.075; //利润为20万元时的奖金

bon4=bon2+100000*0.05;  //利润为40万元时的奖金

 bon6=bon4+100000*0.03; //利润为60万元时的奖金

 bon10=bon6+400000*0.015; //利润为100万元时的奖金

cout<<"enteri:

"; 

cin>>i;

if(i<=100000)

bonus=i*0.1;     //利润在10万元以内按10%提成奖金

else if(i<=200000)

 bonus=bon1+(i-100000)*0.075;  //利润在10万元至20万时的奖金

 elseif (i<=400000)

  bonus=bon2+(i-200000)*0.05; //利润在20万元至40万时的奖金

 elseif(i<=600000)

  bonus=bon4+(i-400000)*0.03; //利润在40万元至60万时的奖金

elseif(i<=1000000)

    bonus=bon6+(i-600000)*0.015;//利润在60万元至100万时的奖金

 else

  bonus=bon10+(i-1000000)*0.01; //利润在100万元以上时的奖金

cout<<"bonus="<

 return0;

}

3.13题另一解

#include

using namespacestd;

intmain()

{longi;

floatbonus,bon1,bon2,bon4,bon6,bon10;

intc;

bon1=100000*0.1;

bon2=bon1+100000*0.075;

bon4=bon2+200000*0.05;

 bon6=bon4+200000*0.03;

bon10=bon6+400000*0.015;

cout<<"enteri:

";

 cin>>i;

c=i/100000;

if(c>10)c=10;

switch(c)

 {case 0:

bonus=i*0.1;break;

case 1:

 bonus=bon1+(i-100000)*0.075;break;

 case 2:

 case3:

bonus=bon2+(i-200000)*0.05;break;

 case 4:

case 5:

bonus=bon4+(i-400000)*0.03;break;

 case6:

case7:

case 8:

case 9:

bonus=bon6+(i-600000)*0.015;break;

case10:

 bonus=bon10+(i-1000000)*0.01;

}

 cout<<"bonus="<

return0;

}

3.14题

#include<iostream>

usingnamespacestd;

intmain ()

{intt,a,b,c,d;

cout<<"enterfournumbers:

";

 cin>>a>>b>>c>>d;

 cout<<"a="<<a<<",b="<<b<<", c="<<c<<",d="<<d<

if(a>b)

{t=a;a=b;b=t;} 

if(a>c)

 {t=a;a=c; c=t;}

if(a>d)

 {t=a; a=d; d=t;}

if(b>c)

 {t=b;b=c; c=t;}

if (b>d)

{t=b;b=d; d=t;}

if(c>d)

 {t=c;c=d;d=t;}

cout<<"thesortedsequence:

"<<endl;

 cout<<a<<","<

return0;

}

3.15题

#include<iostream>

usingnamespacestd;

intmain()

{int p,r,n,m,temp;

cout<<"pleaseentertwopositiveintegernumbersn,m:

"; 

cin>>n>>m;

if (n

  {temp=n;

 n=m;

 m=temp;    //把大数放在n中,小数放在m中

 }

p=n*m;  //先将n和m的乘积保存在p中,以便求最小公倍数时用

while(m!

=0)   //求n和m的最大公约数

    {r=n%m;

n=m;

  m=r;

}

 cout<<"HCF="<

 cout<<"LCD="<<p/n<

return 0;

 }

 

3.16题

#include 

using namespacestd;

intmain()

{char c;

intletters=0,space=0,digit=0,other=0;

cout<<"enterone line:

:

"<

 while((c=getchar())!

='\n')

{if(c>='a'&&c<='z'||c>='A'&& c<='Z')

 letters++;

elseif (c==' ')

 space++;

 else if(c>='0'&& c<='9')

  digit++;

else

ﻩ  other++;

 }

 cout<<"letter:

"<<letters<<",space:

"<<space<<", digit:

"<<digit<<",

other:

"<<other<<endl;

return 0;

} 

3.17题

#include <iostream>

usingnamespacestd;

intmain()

{inta,n,i=1,sn=0,tn=0;

 cout<<"a,n=:

";

 cin>>a>>n;

while (i<=n)

  {tn=tn+a; //赋值后的tn为i个a组成数的值

sn=sn+tn;  //赋值后的sn为多项式前i项之和

  a=a*10;

 ++i;

}

 cout<<"a+aa+aaa+...="<

return0;

} 

3.18题

#include

usingnamespacestd;

intmain()

{floats=0,t=1;

 intn;

 for(n=1;n<=20;n++)

 t=t*n; //求n!

s=s+t;//将各项累加

}

cout<<"1!

+2!

+...+20!

="<

 return 0;

}

3.19题

#include

usingnamespace std;

int main()

{inti,j,k,n;

 cout<<"narcissus numbersare:

"<

  for (n=100;n<1000;n++)

   {i=n/100;

   j=n/10-i*10;

 k=n%10;

if(n==i*i*i+j*j*j+ k*k*k)

  cout<<n<<"";

 }

  cout<

return0;

}

3.20题

#include <iostream>

usingnamespacestd;

intmain()

{constintm=1000;// 定义寻找范围

intk1,k2,k3,k4,k5,k6,k7,k8,k9,k10;

inti,a,n,s;

  for(a=2;a<=m;a++) // a是2~1000之间的整数,检查它是否为完数

  {n=0;   // n用来累计a的因子的个数

s=a;      //s用来存放尚未求出的因子之和,开始时等于a

  for (i=1;i

    if(a%i==0)  //如果i是a的因子

ﻩ{n++;  //n加1,表示新找到一个因子

ﻩs=s-i;   //s减去已找到的因子,s的新值是尚未求出的因子

之和

ﻩ switch(n)  //将找到的因子赋给k1,...,k10

ﻩ{case 1:

ﻩ  k1=i;break;//找出的笫1个因子赋给k1

 case 2:

ﻩ  k2=i; break;// 找出的笫2个因子赋给k2 

ﻩ case 3:

  k3=i; break; //找出的笫3个因子赋给k3

 case 4:

ﻩ    k4=i; break;//找出的笫4个因子赋给k4

case5:

 k5=i; break;//找出的笫5个因子赋给k5

case6:

k6=i; break;//找出的笫6个因子赋给k6 

ﻩ  case 7:

  k7=i; break;  //找出的笫7个因子赋给k7 

 case8:

  k8=i;break;//找出的笫8个因子赋给k8 

ﻩcase9:

    k9=i; break; //找出的笫9个因子赋给k9

case10:

  k10=i; break; //找出的笫10个因子赋给k10

ﻩ}

}

 if(s==0)     //s=0表示全部因子都已找到了

   {cout<<a<<"isa完数"<

ﻩ cout<<"itsfactorsare:

";

 if(n>1)cout<

  if(n>2)cout<<","<2表示至少有3个因子,故应再输出一个因子

  if(n>3)cout<<","<3表示至少有4个因子,故应再输出一个因子 

 if(n>4) cout<<","<

  if(n>5)  cout<<","<<k6;   

   if(n>6)cout<<","<<k7;   

 if(n>7)cout<<","<<k8;  

   if (n>8)cout<<","<

  if (n>9)ﻩcout<<","<<k10;

 cout<<endl<<endl;

 }

 }

 return0;

}

usingname

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1