c++课后答案耿晓庆版第三章.docx

上传人:b****0 文档编号:9708305 上传时间:2023-05-20 格式:DOCX 页数:22 大小:18.45KB
下载 相关 举报
c++课后答案耿晓庆版第三章.docx_第1页
第1页 / 共22页
c++课后答案耿晓庆版第三章.docx_第2页
第2页 / 共22页
c++课后答案耿晓庆版第三章.docx_第3页
第3页 / 共22页
c++课后答案耿晓庆版第三章.docx_第4页
第4页 / 共22页
c++课后答案耿晓庆版第三章.docx_第5页
第5页 / 共22页
c++课后答案耿晓庆版第三章.docx_第6页
第6页 / 共22页
c++课后答案耿晓庆版第三章.docx_第7页
第7页 / 共22页
c++课后答案耿晓庆版第三章.docx_第8页
第8页 / 共22页
c++课后答案耿晓庆版第三章.docx_第9页
第9页 / 共22页
c++课后答案耿晓庆版第三章.docx_第10页
第10页 / 共22页
c++课后答案耿晓庆版第三章.docx_第11页
第11页 / 共22页
c++课后答案耿晓庆版第三章.docx_第12页
第12页 / 共22页
c++课后答案耿晓庆版第三章.docx_第13页
第13页 / 共22页
c++课后答案耿晓庆版第三章.docx_第14页
第14页 / 共22页
c++课后答案耿晓庆版第三章.docx_第15页
第15页 / 共22页
c++课后答案耿晓庆版第三章.docx_第16页
第16页 / 共22页
c++课后答案耿晓庆版第三章.docx_第17页
第17页 / 共22页
c++课后答案耿晓庆版第三章.docx_第18页
第18页 / 共22页
c++课后答案耿晓庆版第三章.docx_第19页
第19页 / 共22页
c++课后答案耿晓庆版第三章.docx_第20页
第20页 / 共22页
亲,该文档总共22页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

c++课后答案耿晓庆版第三章.docx

《c++课后答案耿晓庆版第三章.docx》由会员分享,可在线阅读,更多相关《c++课后答案耿晓庆版第三章.docx(22页珍藏版)》请在冰点文库上搜索。

c++课后答案耿晓庆版第三章.docx

c++课后答案耿晓庆版第三章

/*编写程序,从键盘输入姓名,如“Kobe”,屏幕显示“Hello,Kobe!

”。

*重复输入不同的姓名XX并显示“Hello,XX!

”,直到输入“##”时结束。

*/

#include

usingnamespacestd;

intmain()

{

stringname;

while(cin>>name){

if(name!

="##")

cout<<"Hello,"<

"<

else

break;

}

}

/*3.编写程序,打印如下图所示的1到9的阶梯。

*/

#include

usingnamespacestd;

intmain()

{

constintmax=10;

cout<

for(intnum=1;num

{

for(intj=max-num;j>0;j--)

cout<<"";

for(intj=1;j<=num;j++)

cout<

cout<

}

}

4.编写程序,打印100以内的素数。

*/

#include

usingnamespacestd;

intmain()

{

constintmax=100;

intnum=0;

inti,j;

for(i=2;i<=max;i++){

for(j=2;j

if(i%j)

continue;

else

break;

}

if(j==i){

cout<

num++;

cout<<((num%10)?

"\t":

"\n");

}

}

}

4.编写程序,打印100以内的素数。

*/

#include

usingnamespacestd;

intmain()

{

constintmax=100;

intnum=0;

inti,j;

for(i=2;i<=max;i++){

for(j=2;j

if(i%j)

continue;

else

break;

}

if(j==i){

cout<

num++;

cout<<((num%10)?

"\t":

"\n");

}

}

}

6.编写程序,计算e=1+1/1!

+1/2!

+1/3!

+...+1/n!

...的近似值

要求误差小于0.00001。

*/

#include

usingnamespacestd;

constdoublelimit=1e-5;

intmain()

{

doublee=1;

doublefactorial=1;

intnum=1;

do{

factorial/=num++;

e+=factorial;

}while(factorial>limit);

cout<<"e="<

}

7.编写程序,打印如下图所示的九九乘法表。

*/

#include

usingnamespacestd;

intmain()

{

for(inti=1;i<=9;++i){

for(intj=1;j<=i;++j)

cout<

cout<<"\n";

}

}

8.编写程序,打印如下图所示的反向九九乘法表。

*/

#include

usingnamespacestd;

intmain()

{

for(inti=9;i>=1;--i){

for(intk=9-i;k>=1;k--)

cout<<""<<"\t";

for(intj=i;j>=1;--j)

cout<

cout<<"\n";

}

}

9.编写程序列出由数字1、2、3、4组成的互不相同且无重复数字的三位数。

*/

#include

usingnamespacestd;

intmain()

{

for(inti=1;i<=4;i++){

for(intj=1;j<=4;j++)

for(intk=1;k<=4;k++)

if(i!

=j&&i!

=k&&j!

=k)

cout<

cout<<"\n";

}

}

10.利用switch-case结构完成此题:

判断学生成绩等级

*学习成绩≥90分输出“优秀”

80分≤学生成绩<90分输出“良好”

60分≤学生成绩<80分输出“及格”,

学习成绩<60分输出“不及格”。

*/

#include

usingnamespacestd;

intmain()

{

intscore;

cout<<"Pleaseinputyourscore:

";

cin>>score;

chargrade=0;

if(score>=90)

grade='a';

elseif(score>=80)

grade='b';

elseif(score>=60)

grade='c';

else

grade='d';

switch(grade){

case'a':

cout<<"优秀"<

case'b':

cout<<"良好"<

case'c':

cout<<"及格"<

case'd':

cout<<"不及格"<

default:

cerr<<"ScoreError!

!

!

"<

}

}

11.利用多维数组编写程序实现以下操作:

*对给定的两名学生的各三门成绩,

*输出所有成绩中的最高分和最低分

*并输出每个学生的平均分。

*/

#include

usingnamespacestd;

constintstu=2,scr=3;

intmain()

{

intscore[stu][scr]={

{80,95,86},

{75,89,91}

};

doubleaverage[stu];

intmin=score[0][0],max=score[0][0];

for(inti=0;i

{

doublesum=0;

for(intj=0;j

{

if(min>score[i][j])

min=score[i][j];

if(max

max=score[i][j];

sum+=score[i][j];

}

average[i]=sum/3;

cout<<"学生"<

"<

}

cout<<"所有成绩中的最低分:

"<

cout<<"所有成绩中的最高分:

"<

}

12.编写程序判断两个数组是否相等,

*然后利用vector编写一段类似的程序。

*/

#include

#include

usingnamespacestd;

constintN=3;

intmain()

{

inta[N],b[N];

cout<<"Pleaseinputarrayelements:

"<

for(inti=0;i

cin>>a[i]>>b[i];

boolequal=true;

for(inti=0;i

if(a[i]==b[i])

continue;

else

equal=false;

}

cout<<"\n数组1:

";

for(inti=0;i

cout<

cout<<"\n数组2:

";

for(inti=0;i

cout<

cout<<"两个数组是否相等?

"<

//vector

cout<<"Pleaseinputvectorelements:

"<

vectorva,vb;

for(inti=0;i

{

intx,y;

cin>>x>>y;

va.push_back(x);

vb.push_back(y);

}

cout<<"\nvector1:

";

for(inti=0;i

cout<

cout<<"\nvector2:

";

for(inti=0;i

cout<

cout<<"\n两个vector是否相等?

"<<(va==vb)<

}

13.编写程序比较两个string类型的字符串,

*然后编写另一个程序比较两个C风格字符串。

*/

#include

#include

usingnamespacestd;

constintLen=128;

intmain()

{

//Cconstchar*

charstr1[Len],str2[Len];

cout<<"PleaseinputCstring1:

"<

cin>>str1;

cout<<"PleaseinputCstring2:

"<

cin>>str2;

boolequal=true;

for(inti=0;str1[i]||str2[i];i++){

if(str1[i]!

=str2[i]){

equal=false;

break;

}

}

cout<<"两个C风格字符串相等吗?

\t"<

//C++string

stringstring1,string2;

cout<<"PleaseinputC++string1:

"<

cin>>string1;

cout<<"PleaseinputC++string2:

"<

cin>>string2;

cout<<"两个string相等吗?

\t"<

}

14.编写程序将读入的浮点数值转换为中文金额的大写格式。

*/

#include

#include

#include

usingnamespacestd;

stringCH[]={"","壹","贰","叁","肆","伍","陆","柒","捌","玖"};

stringUnit[]={"元","拾","佰","仟","万",

"拾","佰","仟","亿",

"拾","佰","仟","万","兆"};

stringjiao="角";

stringfen="分";

stringzheng="整";

stringzero="零";

stringyuan="元";

stringwanyuan="万";

stringyiyuan="亿";

intmain()

{

intintegeral,decimal;

chardc1,dc2;

charpoint;

cout<<"请输入金额数值:

";

cin>>integeral;

cin.get(point);

if(point=='.'){

cin.get(dc1);

cin.get(dc2);

if(dc1>='0'&&dc1<='9'){

decimal=(dc1-'0')*10;

if(dc2>='0'&&dc2<='9')

decimal+=dc2-'0';

}

}

else

decimal=0;

cout<

stringipart(""),dpart("");

if(decimal==0)

{

dpart=zheng;

}

else

{

if(decimal<10){

dpart+=CH[decimal];

dpart+=fen;

}

else{

dpart+=CH[decimal/10];

dpart+=jiao;

if(decimal%10!

=0){

dpart+=CH[decimal%10];

dpart+=fen;

}

}

}

intn=integeral;

vectornumber;

while(n){

number.push_back(n%10);

n/=10;

}

intpos=0;

while(number[pos]==0)

pos++;

for(inti=number.size()-1;i>=pos;--i){

if(number[i]){

ipart+=CH[number[i]];

ipart+=Unit[i];

}

else{

ipart+=zero;

}

}

if(pos>=8)

ipart+=yiyuan;

if(pos>4&&pos<8)

ipart+=wanyuan;

if(pos>0)

ipart+=yuan;

cout<<"中文金额:

"<<"\t"<

}

14.编写程序将读入的浮点数值转换为中文金额的大写格式。

*/

#include

#include

#include

usingnamespacestd;

stringCH[]={"","壹","贰","叁","肆","伍","陆","柒","捌","玖"};

stringUnit[]={"元","拾","佰","仟","万",

"拾","佰","仟","亿",

"拾","佰","仟","万","兆"};

stringjiao="角";

stringfen="分";

stringzheng="整";

stringzero="零";

stringyuan="元";

stringwanyuan="万";

stringyiyuan="亿";

intmain()

{

intintegeral,decimal;

chardc1,dc2;

charpoint;

cout<<"请输入金额数值:

";

cin>>integeral;

cin.get(point);

if(point=='.'){

cin.get(dc1);

cin.get(dc2);

if(dc1>='0'&&dc1<='9'){

decimal=(dc1-'0')*10;

if(dc2>='0'&&dc2<='9')

decimal+=dc2-'0';

}

}

else

decimal=0;

cout<

stringipart(""),dpart("");

if(decimal==0)

{

dpart=zheng;

}

else

{

if(decimal<10){

dpart+=CH[decimal];

dpart+=fen;

}

else{

dpart+=CH[decimal/10];

dpart+=jiao;

if(decimal%10!

=0){

dpart+=CH[decimal%10];

dpart+=fen;

}

}

}

intn=integeral;

vectornumber;

while(n){

number.push_back(n%10);

n/=10;

}

intpos=0;

while(number[pos]==0)

pos++;

for(inti=number.size()-1;i>=pos;--i){

if(number[i]){

ipart+=CH[number[i]];

ipart+=Unit[i];

}

else{

ipart+=zero;

}

}

if(pos>=8)

ipart+=yiyuan;

if(pos>4&&pos<8)

ipart+=wanyuan;

if(pos>0)

ipart+=yuan;

cout<<"中文金额:

"<<"\t"<

}

15.将100元钱兑换为20元、10元、5元、1元,编写程序求不同的兑法。

要求每种兑

法中都要有20元、10元、5元和1元。

*/

#include

usingnamespacestd;

intmain()

{

intnum=0;

for(inta=1;a<5;a++)

for(intb=1;b<8;b++)

for(intc=1;c<14;c++)

for(intd=1;d<=65;d++)

if((a*20+b*10+c*5+d)==100)

{

cout<

<

<

<

num++;

}

cout<<"共"<

}

14.将1~100共一百个自然数,放入一个有99个元素的数组a[99]中,编写程序找出

没有被放入数组中的数。

*/

#include

usingnamespacestd;

boolflag[101];

intmain()

{

inta[99];

//....

for(inti=0;i<99;i++)

flag[a[i]]=true;

for(inti=1;i<=100;i++)

if(!

flag[i])

cout<

}

15.8,64,256都是2的阶次方数(如64是2的6次方),

*用两种方法编写程序判断一个整数是不是2的阶次方数,

*并说明哪种方法更好。

*/

#include

usingnamespacestd;

intmain()

{

intnum;

cout<<"Pleaseinputanumber:

"<

cin>>num;

//algorithm1

inti=num;

do{

if(i%2==0)

i/=2;

else

break;

}while(i);

if(i==1)

cout<<"yes"<

else

cout<<"no"<

//algorithm2

i=num;

while(i){

if(!

(i&1))

i>>=1;

else

break;

}

if(i==1)

cout<<"yes"<

else

cout<<"no"<

}

18.structA构成了一个单向链表,编写程序,检测单向链表是否形成环。

*

*/

#include

usingnamespacestd;

structA{

structA*next;

};

boolcycle(A*head)

{

if(head==0)

returnfalse;

A*p=head->next;

while(p){

if(p==head)

returntrue;

else

p=p->next;

}

returnfalse;

}

intmain()

{

A*h1=0;

A*h2=newA;

h2->next=0;

A*h3=newA;

h3->next=h3;

A*h4=newA;

h4->next=newA;

h4->next->next=h4;

cout<<"h1"<

cout<<"h2"<

cout<<"h3"<

cout<<"h4"<

delete

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

当前位置:首页 > 考试认证 > 公务员考试

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2