if(a1>a4)(t=a1ia1=a4ia2=t:中if(a2>a3)t=a2;a2=a3;a3=t;)if(a2>a4)(t=a2a2=a4;a4=t;)ifF(a3>a4)(t=a3;a3=a4;a4=t;cout<<al<<a2<<a3<<a4;18)输入3个数,判断能否构成三角形void main()(int al,a2,a3;cin>>a1>>a2>>a3;a2+a3>al)cout<<"构成三角形";if(al+a2>a3 &al+a3>a2&&elsecout<<"不能构成三角形”;9)编写一个程序,提示用户输入两个日期,然后显示哪一个日期更早。Enter first date (mm/dd/yy):3/6/08Enterseconddate(mm/dd/yy):5/17/075/17/07isearlierthan3/6/08void main()(intdl,ml,yl,d2,m2,y2;bool bl=true;if(yl>y2)bl =false;else if(yl=-y2&&m1>m2 )bl =flase;else if ( (yl==y2&&ml==m2) && d1>d2 ) bl=false;if(bl)(cout<<ml<<"/"<<dl<<"/"<<yl<<" is earlierthan";cout<<m2<<"/"<<d2<<"/"<<y2;Jelse(cout<<m2<<"/"<<d2<<"/"<<y2<<"isearlierthan";cout<<ml<<"/"<<d1<<"/"<<yl;11
11 if (a1>a4){ t=a1;a1=a4;a2=t; } if (a2>a3){ t=a2;a2=a3;a3=t; } if (a2>a4){ t=a2;a2=a4;a4=t; } if (a3>a4){ t=a3;a3=a4;a4=t; } cout<<a1<<a2<<a3<<a4; } 8)输入 3 个数,判断能否构成三角形。 void main(){ int a1,a2,a3; cin>>a1>>a2>>a3; if ( a1+a2>a3 && a1+a3>a2 && a2+a3>a1 ) cout<<”构成三角形”; else cout<<”不能构成三角形”; } 9)编写一个程序,提示用户输入两个日期,然后显示哪一个日期更早。 Enter first date (mm/dd/yy): 3/6/08 Enter second date (mm/dd/yy): 5/17/07 5/17/07 is earlier than 3/6/08 void main(){ int d1,m1,y1,d2,m2,y2; bool b1=true; if ( y1>y2 ) b1 = false; else if(y1==y2&&m1>m2 ) b1 = flase; else if ( (y1==y2&&m1==m2) && d1>d2 ) b1=false; if ( b1 ){ cout<<m1<<”/”<<d1<<”/”<<y1<<” is earlier than”; cout<<m2<<”/”<<d2<<”/”<<y2; }else{ cout<<m2<<”/”<<d2<<”/”<<y2<<” is earlier than”; cout<<m1<<”/”<<d1<<”/”<<y1;
10)编写一个程序,要求用户输入一个两位数,然后显示该数字的英文单词:Enter a two-digit number:45You entered the numberforty-fivevoidmain()int a,b;cin>>a;b = a%10; a =a/10;switch(a)(case0: break;case l:break;2: cout<<"twenty",break;case3:cout<<"thirty";break;casecase 4: cout<<"forty";break;case5:cout<<"fifty";break;6:cout<<"sixty";break;casecase 7:cout<<"seventy";break;case8:cout<<"eighty";break;9:cout<<"ninety";break;caseJswitch(b)(0:caseifcout<<"ten";(a==1)break;case l:if(a==l)cout<<"eleven";else if(a==o)cout<<"one";else cout<<"-one";12
12 } } 10)编写一个程序,要求用户输入一个两位数,然后显示该数字的英文单词: Enter a two-digit number: 45 You entered the number forty-five void main(){ int a,b; cin>>a; b = a%10; a = a/10; switch(a){ case 0: break; case 1: break; case 2: cout<<”twenty”;break; case 3: cout<<”thirty”;break; case 4: cout<<”forty”;break; case 5: cout<<”fifty”;break; case 6: cout<<”sixty”;break; case 7: cout<<”seventy”;break; case 8: cout<<”eighty”;break; case 9: cout<<”ninety”;break; } switch(b){ case 0: if (a==1) cout<<”ten”; break; case 1: if (a==1) cout<<”eleven”; else if(a==0) cout<<”one”; else cout<<”-one”;
break;case2:(a==l)cout<<"twelve";ifelse if(a==0)cout<<"two";else cout<<"-two";break;3:caseif(a==1)cout<<"thirteen";else if(a==0)cout<<"three";else cout<<"-three";break;4:caseif(a==1)cout<<"fourteen";else if(a==0)cout<<"four";else cout<<"-four";break;case5:(a==l) cout<<"fifteen";ifelseiif(a==0)cout<<"five";else cout<<"-five";break;6:caseif(a==1)cout<<"sixteen";else if(a==o)cout<<"six";else cout<<"-six";break;case 7:if(a==1)cout<<"seventeen";else if(a==0)cout<<"seven";else cout<<"-seven";break;13
13 break; case 2: if (a==1) cout<<” twelve”; else if(a==0) cout<<”two”; else cout<<”-two”; break; case 3: if (a==1) cout<<” thirteen”; else if(a==0) cout<<”three”; else cout<<”-three”; break; case 4: if (a==1) cout<<” fourteen”; else if(a==0) cout<<”four”; else cout<<”-four”; break; case 5: if (a==1) cout<<” fifteen”; else if(a==0) cout<<”five”; else cout<<”-five”; break; case 6: if (a==1) cout<<” sixteen”; else if(a==0) cout<<”six”; else cout<<”-six”; break; case 7: if (a==1) cout<<” seventeen”; else if(a==0) cout<<”seven”; else cout<<”-seven”; break;
8:case(a==1)cout<<"eighteen";ifelse if(a==0) cout<<"eight";cout<<"-eight";elsebreak;case9:if(a==1)cout<<"nineteen"cout<<"nine";elseif(a==0)else cout<<"-nine";break;H14
14 case 8: if (a==1) cout<<” eighteen”; else if(a==0) cout<<”eight”; else cout<<”-eight”; break; case 9: if (a==1) cout<<” nineteen”; else if(a==0) cout<<”nine”; else cout<<”-nine”; break; } }
第6章循环结构习题答案1)在for语句中,假定循环体被执行次数为n,则<表达数1>共被计算1次,<表达数2>共被计算n+1次,<表达数3>共被计算n次。2)执行for和while循环时,每次是先进行条件判断,然:再执行循环体,执行do循环时则相反。3)计算1~999中能被3整除,且至少有一位数字是5的所有整数。void main()(inta,al,a2,a3;for(a=l;a<999;a++ )(if((a%3)==0 )(al =a/100;a2=(a%100)/10;a3=a%10;if(al==5511a2==511a3==5(cout<<a<<"\n";子4)编程计算2+4+6++98+100的值。void main()(int i,s=0;for(i=2;i<=100;i+=2 )s += i;1cout<<s;115
15 第 6 章 循环结构 习题答案 1)在 for 语句中,假定循环体被执行次数为 n,则<表达式 1>共被计算_1_次,<表 达式 2>共被计算_n+1_次,<表达式 3>共被计算_n_次。 2)执行 for 和 while 循环时,每次是先进行_条件_判断,然后再执行_循环体 _,执行 do 循环时则相反。 3)计算 1~999 中能被 3 整除,且至少有一位数字是 5 的所有整数。 void main(){ int a,a1,a2,a3; for( a=1;a<999;a++ ){ if ( (a%3)==0 ){ a1 = a/100; a2 = (a%100)/10; a3 = a%10; if ( a1==5 || a2==5 || a3==5 ){ cout<<a<<”\n”; } } } 4)编程计算 2+4+6+.+98+100 的值。 void main(){ int i,s=0; for( i=2;i<=100;i+=2 ){ s += i; } cout<<s; }