C++程序设计(第2版)课后习题答案--第4章
C++程序设计(第2版)课后习题答案--第4章 控制结构
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //4.10 设计一个程序,输入实型变量x和y,若x>y,则输出 x-y;若x<y,则输出 y-x。 2 #include <iostream.h> 3 #include <stdio.h> 4 void main() 5 { 6 int x,y; 7 printf("请输入两个数,中间用空格隔开:\n"); 8 cin>>x>>y; 9 if(x>y) 10 cout<<x-y<<endl; 11 else 12 cout<<y-x<<endl; 13 } 14 // 4.11 15 #include <iostream.h> 16 #include <stdio.h> 17 #include <math.h> 18 void main() 19 { 20 int x,i,j,b,c,d,e; 21 int a=0; 22 printf("请输入一个数:\n"); 23 cin>>x; 24 b=x; 25 while(b>0) 26 { 27 i=b%10; 28 b=b/10; 29 a++; 30 } 31 cout<<"输入数为"<<a<<"位数"<<endl; 32 printf("输入数的每一位数为:\n"); 33 c=x; 34 while(c>0&&a>0) 35 { 36 d=c/pow(10,(a-1)); 37 c-=d*pow(10,(a-1)); 38 cout<<d<<endl; 39 a--; 40 } 41 cout<<"与输入数逆序为:"<<endl; 42 while(x>0) 43 { 44 j=x%10; 45 x/=10; 46 cout<<j; 47 } 48 cout<<endl; 49 } 50 51 // 4.12 52 #include <iostream.h> 53 void main() 54 { 55 float i,j,a,b; 56 cout<<"请输入若干个数,中间用空格隔开,并以“.”号结束"<<endl; 57 cin>>i>>j; 58 while(i>0&&j>0){ 59 if (i>j) 60 { 61 cin>>j; 62 a=i; 63 } 64 else 65 { 66 cin>>i; 67 b=j; 68 } 69 } 70 if(a>b) 71 cout<<a<<endl; 72 else 73 cout<<b<<endl; 74 } 75 76 // 4.13 77 #include <iostream.h> 78 void main() 79 { 80 float a,b,c; 81 cout<<"请输入若3个数,中间用空格隔开"<<endl; 82 cin>>a>>b>>c; 83 if(a+b>c&&a+c>b&&b+c>a) 84 cout<<"这三个数能构成三角形的三边"<<endl; 85 else 86 cout<<"这三个数不能构成三角形的三边"<<endl; 87 } 88 89 // 4.14 90 #include <iostream.h> 91 void main() 92 { 93 int a,b,c; 94 cout<<"请输入若3个数,中间用空格隔开"<<endl; 95 cin>>a>>b>>c; 96 if(a+b>c&&a+c>b&&b+c>a) 97 cout<<"这三个数能构成三角形的三边"<<endl; 98 else 99 cout<<"这三个数不能构成三角形的三边"<<endl; 100 } 101 102 // 4.15 103 #include <iostream.h> 104 void main() 105 { 106 int i,j; 107 int a=1; 108 cout<<"二进制"<<'\t'<<"十进制"<<'\t'<<"八进制"<<'\t'<<"十六进制"<<endl; 109 for(;a<=156;a++) 110 { 111 i=a; 112 while (i>0) 113 { 114 j=i%2;i=i/2; 115 cout<<j; 116 } 117 cout<<'\t'<<dec<<a<<'\t'<<oct<<a<<'\t'<<hex<<a<<endl; 118 } 119 120 } 121 // 4.16 122 #include <iostream.h> 123 void main() 124 { 125 int a,b,c,d; 126 int max1,max2,min1,min2; 127 cout<<"请输入四个数"<<endl; 128 cin>>a>>b>>c>>d; 129 if(a>b) 130 { 131 max1=a; 132 min1=b; 133 } 134 else 135 { 136 max1=b; 137 min1=a; 138 } 139 if(c>d) 140 { 141 max2=c; 142 min2=d; 143 } 144 else 145 { 146 max2=d; 147 min2=c; 148 } 149 cout<<"这四个数由大到小的排列顺序为:"<<endl; 150 if(max1>max2) 151 { 152 cout<<max1<<" "; 153 if(min1>min2) 154 { 155 if(max2>min1) 156 cout<<max2<<" "<<min1<<" "<<min2<<endl; 157 else 158 if(max2>min2) 159 cout<<min1<<" "<<max2<<" "<<min2<<endl; 160 else 161 cout<<min1<<" "<<min2<<" "<<max2<<endl; 162 } 163 else 164 { 165 if(max2>min2) 166 cout<<max2<<" "<<min2<<" "<<min1<<endl; 167 else 168 if(max2>min1) 169 cout<<min2<<" "<<max2<<" "<<min1<<endl; 170 else 171 cout<<min2<<" "<<min1<<" "<<max2<<endl; 172 } 173 } 174 else 175 { 176 cout<<max2<<" "; 177 if(min1>min2) 178 { 179 if(max1>min1) 180 cout<<max1<<" "<<min1<<" "<<min2<<endl; 181 else 182 if(max1>min2) 183 cout<<min1<<" "<<max1<<" "<<min2<<endl; 184 else 185 cout<<min1<<" "<<min2<<" "<<max1<<endl; 186 } 187 else 188 { 189 if(max1>min2) 190 cout<<max1<<" "<<min2<<" "<<min1<<endl; 191 else 192 if(max1>min1) 193 cout<<min2<<" "<<max1<<" "<<min1<<endl; 194 else 195 cout<<min2<<" "<<min1<<" "<<max1<<endl; 196 } 197 } 198 199 } 200 // 4.17 201 #include <iostream.h> 202 void main() 203 { 204 cout<<"请按照下面方程的格式,分别输入六个参数,中间用空格隔开"<<endl; 205 cout<<"a1x+b1y=c1"<<endl<<"a2x+b2y=c2"<<endl; 206 float a1,a2,b1,b2,c1,c2,x,y; 207 cout<<"请按顺序输入 a1,b1,c1,a2,b2,c2"<<endl; 208 cin>>a1>>b1>>c1>>a2>>b2>>c2; 209 x=(b2*c1-b1*c2)/(a1*b2-a2*b1); 210 y=(a2*c1-a1*c2)/(a2*b1-a1*b2); 211 cout<<"x="<<x<<endl; 212 cout<<"y="<<y<<endl; 213 } 214 // 4.18 215 #include <iostream.h> 216 void main() 217 { 218 cout<<"请输入一个百分制的成绩"<<endl; 219 int a; 220 cin>>a; 221 if(a<0||a>100) 222 { 223 cout<<"你输入一个错误的数字,请重新输入"<<endl; 224 } 225 switch(a/10) 226 { 227 case 9: 228 cout<<"A"<<endl; 229 break; 230 case 8: 231 cout<<"B"<<endl; 232 break; 233 case 7: 234 cout<<"C"<<endl; 235 break; 236 case 6: 237 cout<<"D"<<endl; 238 break; 239 case 5: 240 case 4: 241 case 3: 242 case 2: 243 case 1: 244 case 0: 245 cout<<"E"<<endl; 246 break; 247 } 248 } 249 250 // 4.19 251 #include <iostream.h> 252 void main() 253 { 254 cout<<"请输入一个正整数"<<endl; 255 int a,b; 256 int c=0; 257 cin>>a; 258 int i=2; 259 cout<<"该正数的因子为:"<<endl; 260 for(;i<=a;i++) 261 { 262 b=c; 263 if(a%i==0) 264 c=a/i; 265 if(c!=b) 266 cout<<c<<endl; 267 } 268 269 } 270 // 4.20 271 #include <iostream.h> 272 void main() 273 { 274 cout<<"请输入一个正整数"<<endl; 275 int a,i; 276 int sum=0; 277 cin>>a; 278 for (;a>0;) 279 { 280 i=a%10; 281 sum+=i; 282 a/=10; 283 } 284 cout<<"这个正整数的各位数字的和为:"<<sum<<endl; 285 } 286 287 // 4.21 288 #include <iostream.h> 289 #include <stdio.h> 290 void main() 291 { 292 int a=2; 293 int b=1; 294 float sum=0; 295 for(int i=1;i<=20;i++){ 296 sum+=(float)(a)/b; 297 int c; 298 c=a+b; 299 b=a; 300 a=c; 301 } 302 cout<<"数列的和为:"<<sum<<endl; 303 } 304 305 // 4.22 306 #include <iostream.h> 307 void main() 308 { 309 int a,b,c,sum; 310 for(int i=100;i<1000;i++) 311 { 312 a=(i/100)%10;b=(i/10)%10;c=i%10; 313 sum=a*a*a+b*b*b+c*c*c; 314 if(sum==i) 315 cout<<"水仙花数为:"<<sum<<endl; 316 } 317 } 318 319 // 4.23 320 #include <iostream.h> 321 void main() 322 { 323 int b; 324 int a=1; 325 cout<<"1000以内的完数为:"<<endl; 326 while(a<1000) 327 { 328 int sum=0; 329 for(int i=2;i<=a;i++) 330 { 331 int c=0; 332 b=c; 333 if(a%i==0) 334 c=a/i; 335 if(c!=b) 336 sum+=c; 337 } 338 if(sum==a) 339 cout<<sum<<endl; 340 a++; 341 } 342 } 343 344 // 4.24 345 #include <iostream.h> 346 void main() 347 { 348 float d; 349 float a=0; 350 float b=1; 351 float c=2; 352 cout<<a<<endl<<b<<endl<<c<<endl; 353 for(int n=3;n<=19;n++) 354 { 355 d=b*c+a; 356 a=b; 357 b=c; 358 c=d; 359 cout<<c<<endl; 360 } 361 } 362 363 // 4.25 364 #include <iostream.h> 365 void main() 366 { 367 float c,F; 368 cout<<"请输入一个华氏度"<<endl; 369 cin>>F; 370 c=(F-32)*5/9; 371 cout<<"这个华氏度所对应的摄氏度为:"<<c<<endl; 372 } 373 4.26 374 // 4.26 375 #include <iostream.h> 376 void main() 377 { 378 int i; 379 int a=1; 380 int sum=0; 381 cout<<"请输入若干个整数,中间用空格隔开"<<endl; 382 cin>>i; 383 while(i>=0) 384 { 385 sum+=i; 386 cin>>i; 387 cout<<"sum="<<sum<<' '<<a<<endl; 388 a++; 389 } 390 } 391 // 4.27 392 #include <iostream.h> 393 void main() 394 { 395 cout<<" C++程序设计 p103 4.27 第一个图"<<endl<<endl; 396 for(int i=1;i<=10;i++) 397 { 398 for(int j=1;j<=i;j++) 399 cout<<'*'; 400 cout<<endl; 401 } 402 cout<<endl<<" C++程序设计 p103 4.27 第二个图"<<endl<<endl; 403 for (int a=1;a<=10;a++) 404 { 405 for(int b=10;b>=a;b--) 406 cout<<'*'; 407 cout<<endl; 408 } 409 cout<<endl<<" C++程序设计 p103 4.27 第三个图"<<endl<<endl; 410 for (int c=1;c<=10;c++) 411 { 412 for(int d=1;(c-d)>0;d++) 413 cout<<" "; 414 for(int e=10;e>=c;e--) 415 cout<<'*'; 416 cout<<endl; 417 } 418 cout<<endl<<" C++程序设计 p103 4.27 第四个图"<<endl<<endl; 419 for (int f=1;f<=10;f++) 420 { 421 for(int g=10;g>=f;g--) 422 cout<<" "; 423 for(int h=1;(f-h)>=0;h++) 424 cout<<'*'; 425 cout<<endl; 426 } 427 cout<<endl<<" C++程序设计 p103 4.27 四个图并排输出"<<endl<<endl; 428 // while的用法和for差不多,最后一个就用while,做一遍 429 int x=1; 430 while(x<=10) 431 { 432 int a1=1; 433 while(a1<=x) 434 { 435 cout<<'*'; 436 a1++; 437 } 438 int b1=10; 439 while(b1>=x) 440 { 441 cout<<" "; 442 b1--; 443 } 444 int b2=10; 445 while(b2>=x) 446 { 447 cout<<'*'; 448 b2--; 449 } 450 int a2=1; 451 while(a2<=x) 452 { 453 cout<<" "; 454 a2++; 455 } 456 int a3=1; 457 while(a3<=x) 458 { 459 cout<<" "; 460 a3++; 461 } 462 int b3=10; 463 while(b3>=x) 464 { 465 cout<<'*'; 466 b3--; 467 } 468 int b4=10; 469 while(b4>=x) 470 { 471 cout<<" "; 472 b4--; 473 } 474 int a4=1; 475 while(a4<=x) 476 { 477 cout<<'*'; 478 a4++; 479 } 480 x++; 481 cout<<endl; 482 } 483 cout<<endl; 484 } 485 // 4.28 486 #include <iostream.h> 487 int main() 488 { 489 char A,B,C; 490 int count=1; 491 for(A='X';A<='Z';A++) 492 for(B='X';B<='Z';B++) 493 for(C='X';C<='Z';C++) 494 if((A!=B && B!=C && C!=A) && (C!='Y') && (A!='X')) 495 { 496 cout<<"第"<<count<<"种名单:"<<endl; 497 cout<<"A-"<<A<<endl; 498 cout<<"B-"<<B<<endl; 499 cout<<"C-"<<C<<endl; 500 count++; 501 } 502 }