c++作业2
1 /* 2 //ASCII表中的下一个字符 3 #include<iostream> 4 using namespace std; 5 int xiyage(char a) 6 { 7 char b = a + 1; 8 return b; 9 } 10 int main() 11 { 12 char a; 13 cin >> a; 14 cout << (char)xiyage(a) << endl; 15 return 0; 16 }*/ 17 /* 18 //输入一个浮点数,输出这个浮点数的绝对值 19 #include<iostream> 20 #include<iomanip> 21 using namespace std; 22 float abs(float x) 23 { 24 float f = x; 25 if (x < 0) 26 { 27 return -f; 28 } 29 return f; 30 } 31 int main() 32 { 33 float x; 34 cin >> x; 35 cout << setprecision(2) << fixed << abs(x) << endl; 36 }*/ 37 //给定一个整数N,判断其正负。 38 /* 39 #include<iostream> 40 using namespace std; 41 void fun(int a) 42 { 43 if (a>0) 44 { 45 cout << "positive"; 46 } 47 else if (a<0) 48 { 49 cout << "negative"; 50 } 51 else 52 { 53 cout << "zero"; 54 } 55 } 56 int main() 57 { 58 int a; 59 cin >> a; 60 fun(a); 61 return 0; 62 }*/ 63 //给定一个整数,判断该数是奇数还是偶数 64 /* 65 #include <iostream> 66 using namespace std; 67 void fun(int a) 68 { 69 if (a%2==0) 70 { 71 cout<< "even"; 72 } 73 else 74 { 75 cout<<"odd"; 76 } 77 } 78 int main() 79 { 80 int a; 81 cin >> a; 82 fun(a); 83 return 0; 84 }*/ 85 /* 86 编写程序,计算下列分段函数y = f(x)的值。 87 88 y = -x + 2.5; 0 <= x < 5 89 90 y = 2 - 1.5(x - 3)(x - 3); 5 <= x < 10 91 92 y = x / 2 - 1.5; 10 <= x < 20 93 */ 94 /* 95 #include <iostream> 96 #include<iomanip> 97 using namespace std; 98 double fun1(double x) 99 { 100 return -x + 2.5; 101 } 102 double fun2(double x) 103 { 104 return 2 - 1.5*(x - 3)*(x - 3); 105 } 106 double fun3(double x) 107 { 108 return x / 2 - 1.5; 109 } 110 111 double judge(double x) 112 { 113 double z = 0; 114 if (x>=0&&x<5) 115 { 116 z = fun1(x); 117 } 118 else if (x>=5&&x<10) 119 { 120 z = fun2(x); 121 } 122 else 123 { 124 z = fun3(x); 125 } 126 return z; 127 } 128 int main() 129 { 130 double x; 131 cin >> x; 132 cout << setprecision(3) << fixed << judge(x); 133 }*/ 134 //给定三个正整数,分别表示三条线段的长度,判断这三条线段能否构成一个三角形。 135 /* 136 #include <iostream> 137 #include<cmath> 138 using namespace std; 139 void judge(int a, int b, int c) 140 { 141 if (a+b>c&&a+c>b&&b+c>a&&abs(a-b)<c&&abs(a-c)<b&&abs(b-c)<a) 142 { 143 cout << "yes"; 144 } 145 else 146 { 147 cout << "no"; 148 } 149 } 150 int main() 151 { 152 int a, b, c; 153 cin >> a >> b >> c; 154 judge(a, b, c); 155 return 0; 156 }*/ 157 //判断某年是否是闰年。 158 /* 159 #include <iostream> 160 using namespace std; 161 void judge(int a) 162 { 163 if (a%100!=0&&a%4==0||a%400==0) 164 { 165 cout << "Y"; 166 } 167 else 168 { 169 cout << "N"; 170 } 171 } 172 int main() 173 { 174 int y; 175 cin >> y; 176 judge(y); 177 }*/ 178 //一个最简单的计算器,支持+, -, *, / 四种运算。仅需考虑输入输出为整数的情况,数据和运算结果不会超过int表示的范围。 179 /* 180 #include <iostream> 181 using namespace std; 182 int fun(int a, int b, char c) 183 { 184 if (c=='+') 185 { 186 return a + b; 187 } 188 if (c == '-') 189 { 190 return a - b; 191 } 192 if (c == '*') 193 { 194 return a * b; 195 } 196 if (c == '/') 197 { 198 return a / b; 199 } 200 else 201 { 202 return 0; 203 } 204 } 205 int main() 206 { 207 int a=0, b=0; 208 char c=0; 209 cin >> a >> b >> c; 210 if (b==0) 211 { 212 cout << "Divided by zero!"; 213 } 214 if (fun(a,b,c)) 215 { 216 cout << fun(a, b, c); 217 } 218 else 219 { 220 cout << "Invalid operator!"; 221 } 222 return 0; 223 }*/ 224 /* 225 #include <iostream> 226 using namespace std; 227 void fun(int a, int b, char c) 228 { 229 switch (c) { 230 case '+': cout << a + b; 231 break; 232 case '-': cout << a - b; 233 break; 234 case '*': cout << a * b; 235 break; 236 case '/': if (b == 0) { 237 cout << "Divided by zero!"; 238 break; 239 } 240 else { 241 cout << a / b; 242 break; 243 } 244 default: cout << "Invalid operator!"; 245 } 246 cout << endl; 247 } 248 int main() { 249 int a, b; 250 char c; 251 cin >> a >> b >> c; 252 fun(a, b, c); 253 return 0; 254 }*/ 255 //任意输入一个字符,判断其ASCII是否是奇数,若是,输出YES,否则,输出NO 256 /* 257 #include <iostream> 258 using namespace std; 259 void judge(int a) 260 { 261 if (a%2==0) 262 { 263 cout << "NO"; 264 } 265 else 266 { 267 cout << "YES"; 268 } 269 } 270 int main() 271 { 272 char a; 273 cin >> a; 274 judge((int)a); 275 return 0; 276 }*/ 277 /* 278 #include <iostream> 279 using namespace std; 280 int main() 281 { 282 char ch; 283 ch = getchar(); 284 if (ch % 2 == 0) cout << "NO" << endl; 285 else cout << "YES" << endl; 286 return 0; 287 }*/ 288 //根据邮件的重量和用户是否选择加急计算邮费。计算规则 289 //重量在1000克以内(包括1000克), 基本费8元。超过1000克的部分 290 //每500克加收超重费4元,不足500克部分按500克计算;如果用户选择加急,多收5元。 291 /* 292 #include <iostream> 293 #include<cmath> 294 using namespace std; 295 int main() 296 { 297 int x,y=0; 298 char c; 299 cin >> x >> c; 300 if (x <= 1000) 301 { 302 y = 8; 303 } 304 else 305 { 306 y = 8 + ceil((x - 1000) / 500.0) * 4; 307 } 308 if (c=='y') 309 { 310 y += 5; 311 } 312 cout << y << endl; 313 return 0; 314 }*/ 315 //某饮料公司最近推出了一个“收集瓶盖赢大奖”的活动:如果你拥有10个印有“幸运” 316 //或20个印有“鼓励”的瓶盖,就可以兑换一个神秘大奖。 317 //现分别给出你拥有的印有“幸运”和“鼓励”的瓶盖数,判断是否可以去兑换大奖。 318 /* 319 #include <iostream> 320 using namespace std; 321 int main() 322 { 323 int x, y; 324 cin >> x >> y; 325 if (x>=10||y>=20) 326 { 327 cout << 1<<endl; 328 } 329 else 330 { 331 cout << 0<<endl; 332 } 333 return 0; 334 }*/ 335 /*小金打牌,当对方手里纸牌的张数大于等于10张时,他会观察对方的神情,若对方神情紧张,他就出“炸”,若对方在笑,他就什么牌也不出。 336 337 字符 'J'表述对方申请紧张,'H'表述对方在笑。小金出“炸”,则输出“Z”,如果什么牌都不出,则输出“pass”。 338 339 当对方手里纸牌的张数小于10张的时候,他会“接”对方出的牌,也就是对方出“单”,他也出“单”,对方出“双”,他也出双。 340 341 字符 'D’代表对方出“单”,'S'代表对方出“双”。小金出“单”,则输出“D”,如果出“双”,则输出“S 342 */ 343 /*#include<iostream> 344 using namespace std; 345 int main() 346 { 347 int a; 348 char b,c; 349 cin>>a>>b>>c; 350 if(a>=10) 351 { 352 353 if(b=='J') 354 { 355 cout<<"Z"; 356 } 357 else 358 { 359 cout<<"pass"; 360 } 361 } 362 else 363 { 364 if(c=='D') 365 { 366 cout<<"D"; 367 } 368 else 369 { 370 cout<<"S"; 371 } 372 } 373 return 0; 374 }*/ 375 //判断一个数n 能否同时被3和5整除 376 /*#include<iostream> 377 using namespace std; 378 int main() 379 { 380 int n; 381 cin>>n; 382 if(n%3==0&&n%5==0) 383 { 384 cout<<"YES"; 385 } 386 else 387 { 388 cout<<"NO"; 389 } 390 return 0; 391 } 392 */ 393 /*给定一个整数,判断它能否被3,5,7整除,并输出以下信息: 394 1、能同时被3,5,7整除(直接输出3 5 7,每个数中间一个空格); 395 2、只能被其中两个数整除(输出两个数,小的在前,大的在后。例如:3 5或者 3 7或者5 7,中间用空格分隔); 396 3、只能被其中一个数整除(输出这个除数); 397 4、不能被任何数整除,输出小写字符‘n’,不包括单引号。 398 */ 399 /*#include<iostream> 400 using namespace std; 401 int main() 402 { 403 int a; 404 cin>>a; 405 if(a%5==0&&a%7==0&&a%3==0) 406 { 407 cout<<3<<" "<<5<<" "<<7<<endl; 408 } 409 else if(a%3==0&&a%5==0) 410 { 411 cout<<3<<" "<<5<<endl; 412 } 413 else if(a%3==0&&a%7==0) 414 { 415 cout<<3<<" "<<7<<endl; 416 } 417 else if(a%5==0&&a%7==0) 418 { 419 cout<<5<<" "<<7<<endl; 420 } 421 else if(a%3==0) 422 { 423 cout<<3<<endl; 424 } 425 else if(a%5==0) 426 { 427 cout<<5<<endl; 428 } 429 else if(a%7==0) 430 { 431 cout<<7<<endl; 432 } 433 else 434 { 435 cout<<"n"<<endl; 436 } 437 return 0; 438 } 439 */ 440 //晶晶的朋友贝贝约晶晶下周一起去看展览, 441 //但晶晶每周的1、3、5有课必须上课,请帮晶晶判断她 442 //能否接受贝贝的邀请,如果能输出YES;如果不能则输出NO。 443 /*#include<iostream> 444 using namespace std; 445 int main() 446 { 447 int a; 448 cin>>a; 449 if(a==1||a==3||a==5) 450 { 451 cout<<"NO"; 452 } 453 else 454 { 455 cout<<"YES"; 456 } 457 return 0; 458 } 459 */ 460 /*在北大校园里,没有自行车,上课办事会很不方便.但实际上, 461 并非去办任何事情都是骑车快,因为骑车总要找车、开锁、 462 停车、锁车等,这要耽误一些时间.假设找到自行车,开锁并车 463 上自行车的时间为27秒;停车锁车的时间为23秒;步行每秒 464 行走1.2米,骑车每秒行走3.0米。请判断走不同的距离去办事 465 ,是骑车快还是走路快。 466 */ 467 /*#include<iostream> 468 using namespace std; 469 int main() 470 { 471 double s; 472 cin>>s; 473 double bs=s/1.2; 474 double qs=s/3.0+23+27; 475 if(bs<qs) 476 { 477 cout<<"Walk"; 478 } 479 else if(bs>qs) 480 { 481 cout<<"Bike"; 482 } 483 else 484 { 485 cout<<"All"; 486 } 487 return 0; 488 } 489 */ 490 //给出一名学生的语文和数学成绩, 491 //判断他是否恰好有一门课不及格(成绩小于60分) 492 /*#include<iostream> 493 using namespace std; 494 int main() 495 { 496 int a,b; 497 cin>>a>>b; 498 if(a<60&&b>=60) 499 { 500 cout<<1; 501 } 502 else if(b<60&&a>=60) 503 { 504 cout<<1; 505 } 506 else 507 { 508 cout<<0; 509 } 510 return 0; 511 } 512 */ 513 //利用公式x1 = (-b + sqrt(b*b-4*a*c))/(2*a), 514 //x2 = (-b - sqrt(b*b-4*a*c))/(2*a) 515 //求一元二次方程ax2+ bx + c =0的根,其中a不等于0。 516 /*#include<iostream> 517 #include<cmath> 518 #include<iomanip> 519 #include <cstdio> 520 using namespace std; 521 int main() 522 { 523 double a,b,c; 524 cin>>a>>b>>c; 525 double d=b*b-4*a*c; 526 double der=sqrt(d); 527 double x1=(-b+der)/(2*a); 528 double x2=(-b-der)/(2*a); 529 if(d==0) 530 { 531 cout<<setprecision(5)<<fixed<<"x1=x2="<<x1; 532 } 533 else if (d>0) 534 { 535 if (a>0) 536 { 537 cout << setprecision(5) << fixed << "x1=" << x1 << ";" << "x2=" << x2; 538 } 539 else 540 { 541 cout << setprecision(5) << fixed << "x2=" << x2 << ";" << "x1=" << x1; 542 } 543 } 544 else 545 { 546 double m, n; 547 double esp = pow(10, -7); 548 m = -b / (2 * a) + esp; 549 n = sqrt(-d) / (2 * a); 550 printf("x1=%.5lf+%.5lfi;x2=%.5lf-%.5lfi\n", m, n, m, n); 551 /*der = sqrt(-d); 552 double x3 = -b / 2.0 / a; 553 double x4 = der / 2.0 / a; 554 if (b==0) 555 { 556 x3 = 0; 557 } 558 if (a>0) 559 { 560 cout << setprecision(5) << fixed << "x1=" << x3 << "+" << x4 << "i" << ";" << "x2=" << x3 << "-" << x4 << "i" << endl; 561 } 562 else 563 { 564 cout<<setprecision(5)<< fixed<<"x1="<<x3<<"+"<<-x4<<"i"<<";"<<"x2="<<x3<<x4<<"i"<<endl; 565 }// 566 567 568 } 569 570 return 0; 571 }*/ 572 573 //虫子与苹果2 574 /*#include<iostream> 575 using namespace std 576 { 577 int n = 0, x = 0, y = 0; 578 cin >> n >> x >> y; 579 if (y <= n*x) 580 { 581 if (y%x == 0) 582 { 583 cout << n - y / x << endl; 584 } 585 else 586 { 587 cout << n - y / x - 1 << endl; 588 } 589 } 590 else 591 { 592 cout << 0; 593 } 594 return 0; 595 }*/
道阻且长,行则将至