├── .gitattributes ├── .vscode └── settings.json ├── LICENSE ├── W0301 ├── 3-b1.cpp ├── 3-b2.cpp ├── 3-b3.cpp ├── 3-b4.cpp └── 3-b5.cpp ├── W0401 ├── 3-b6.cpp ├── 3-b7.cpp └── 3-b8.cpp ├── W0501 ├── 3-b10.cpp ├── 3-b11.cpp ├── 3-b12.c ├── 3-b12.cpp ├── 3-b13.c ├── 3-b13.cpp └── 3-b9.cpp ├── W0601-Ch3 ├── 3-b10.c ├── 3-b11.c ├── 3-b6.c ├── 3-b7.c ├── 3-b8.c └── 3-b9.c ├── W0601-Ch4 ├── 4-b1.cpp ├── 4-b2.c ├── 4-b2.cpp ├── 4-b3.c ├── 4-b3.cpp ├── 4-b4.c └── 4-b4.cpp ├── W0701 ├── 4-b10.c ├── 4-b11.cpp ├── 4-b12.cpp ├── 4-b13.cpp ├── 4-b5.c ├── 4-b6.cpp ├── 4-b7.cpp ├── 4-b8.c ├── 4-b8.cpp └── 4-b9.cpp ├── W0801 ├── 5-b1.cpp ├── 5-b2.cpp ├── 5-b3.cpp ├── 5-b4.cpp ├── 5-b5.cpp ├── 5-b6-1.cpp └── 5-b6-2.c ├── W0901 ├── 4-b19.cpp ├── 4-b20.cpp ├── 4-b21-1.cpp ├── 4-b21-2.cpp ├── 5-b10.cpp ├── 5-b7-main.cpp ├── 5-b7.h ├── 5-b8.cpp └── 5-b9.cpp ├── W1001 ├── 5-b11-1.c ├── 5-b11-2.cpp ├── 5-b12-main.cpp ├── 5-b12-sub.cpp ├── 5-b12.h ├── 5-b13.cpp └── 5-b14.c ├── W1101 ├── w11-s1.cpp └── w11-s2.cpp ├── W1103 ├── 5-b15.cpp ├── 5-b16-1.c ├── 5-b16-2.cpp ├── 5-b17.cpp └── 5-b18.cpp ├── W1201 ├── 6-b1.cpp ├── 6-b2.cpp └── 6-b3.cpp ├── W1301 ├── 7-b1.cpp └── 7-b2.cpp ├── W1401 ├── 8-b2.cpp ├── 8-b3.cpp └── w14-s1.cpp ├── W_Hanoi ├── cmd_console_tools.cpp ├── cmd_console_tools.h ├── hanoi.h ├── hanoi_main.cpp ├── hanoi_menu.cpp ├── hanoi_multiple_solutions.cpp └── test-cct.cpp ├── W_SynthesisTen ├── 90-b2-base.cpp ├── 90-b2-console.cpp ├── 90-b2-main.cpp ├── 90-b2-tools.cpp ├── 90-b2.h ├── cmd_console_tools.cpp └── cmd_console_tools.h └── readme.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "stdio.h": "c" 4 | } 5 | } -------------------------------------------------------------------------------- /W0301/3-b1.cpp: -------------------------------------------------------------------------------- 1 | // 这个文件之前被错误覆盖了,原文件我正在尝试找回 2 | // Commit历史中的这一个文件是错的 3 | -------------------------------------------------------------------------------- /W0301/3-b2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | const double pi = 3.14159; 8 | double radius, height; 9 | cout << "请输入半径和高度" << endl; 10 | cin >> radius >> height; 11 | cout << setiosflags(ios::fixed); 12 | cout << "圆周长 : " << setprecision(2) << (pi * radius * 2.0) << endl; 13 | cout << "圆面积 : " << setprecision(2) << (pi * radius * radius) << endl; 14 | cout << "圆球表面积 : " << setprecision(2) << (pi * radius * radius * 4.0) << endl; 15 | cout << "圆球体积 : " << setprecision(2) << (pi * radius * radius * radius * 4.0 / 3.0) << endl; 16 | cout << "圆柱体积 : " << setprecision(2) << (pi * radius * radius * height) << endl; 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /W0301/3-b3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | cout << "请输入一个[1..30000]间的整数:" << endl; 8 | int a; 9 | cin >> a; 10 | 11 | cout << "万位 : " << a / 10000 % 10 << endl; 12 | cout << "千位 : " << a / 1000 % 10 << endl; 13 | cout << "百位 : " << a / 100 % 10 << endl; 14 | cout << "十位 : " << a / 10 % 10 << endl; 15 | cout << "个位 : " << a % 10 << endl; 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /W0301/3-b4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | double a; 9 | cout << "请输入[0-100亿)之间的数字: " << endl; 10 | cin >> a; 11 | cout << setprecision(ios::fixed); 12 | cout << "十亿位 : " << static_cast((fmod(a, 1e+10) - fmod(a, 1e+9)) / 1e+9) << endl; 13 | cout << "亿位 : " << static_cast((fmod(a, 1e+9) - fmod(a, 1e+8)) / 1e+8) << endl; 14 | cout << "千万位 : " << static_cast((fmod(a, 1e+8) - fmod(a, 1e+7)) / 1e+7) << endl; 15 | cout << "百万位 : " << static_cast((fmod(a, 1e+7) - fmod(a, 1e+6)) / 1e+6) << endl; 16 | cout << "十万位 : " << static_cast((fmod(a, 1e+6) - fmod(a, 1e+5)) / 1e+5) << endl; 17 | cout << "万位 : " << static_cast((fmod(a, 1e+5) - fmod(a, 1e+4)) / 1e+4) << endl; 18 | cout << "千位 : " << static_cast((fmod(a, 1e+4) - fmod(a, 1e+3)) / 1e+3) << endl; 19 | cout << "百位 : " << static_cast((fmod(a, 1e+3) - fmod(a, 1e+2)) / 1e+2) << endl; 20 | cout << "十位 : " << static_cast((fmod(a, 1e+2) - fmod(a, 1e+1)) / 1e+1) << endl; 21 | cout << "圆 : " << static_cast((fmod(a, 1e+1) - fmod(a, 1e+0)) / 1e+0) << endl; 22 | //由于要求不许对数整体扩大,为了获取小数部分数位,先获取小数部分数值,存于变量b中 23 | double b = fmod(a, 1.0); 24 | //由于精度误差,输入的a和实际存储的a值不同,故特别处理小数后两位的问题 25 | cout << "角 : " << static_cast(round(1e1 * fmod(b * 1e+1, 1e+1)) / 1e1) << endl; 26 | cout << "分 : " << static_cast(round((fmod(b * 1e+2, 1e+1)))) % 10 << endl; 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /W0301/3-b5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | const float pi = 3.14159f; 9 | int a, b, angle; 10 | float s; 11 | cout << setiosflags(ios::fixed); 12 | cout << "请输入三角形的两边及其夹角(角度) :" << endl; 13 | cin >> a >> b >> angle; 14 | s = 1.0f * a * b / 2 * (float)sin(angle * pi / 180); 15 | cout << "三角形面积为 : " << setprecision(3) << s << endl; 16 | return 0; 17 | 18 | } -------------------------------------------------------------------------------- /W0401/3-b6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int year, month, day, day_limit = 0, day_before = 0; 7 | int leap_year = 0; 8 | cout << "请输入,年,月,日" << endl; 9 | 10 | cin >> year >> month >> day; 11 | //先判断闰年 12 | if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) 13 | { 14 | leap_year = 1; 15 | } 16 | //判断月份 17 | if (month > 0 && month <= 12) 18 | { 19 | switch (month) 20 | { 21 | case 1: 22 | day_limit = 31; 23 | day_before = 0; 24 | break; 25 | case 2: 26 | day_limit = 28 + leap_year; 27 | day_before = 31; 28 | break; 29 | case 3: 30 | day_limit = 31; 31 | day_before = 59 + leap_year; 32 | break; 33 | case 4: 34 | day_limit = 30; 35 | day_before = 90 + leap_year; 36 | break; 37 | case 5: 38 | day_limit = 31; 39 | day_before = 120 + leap_year; 40 | break; 41 | case 6: 42 | day_limit = 30; 43 | day_before = 151 + leap_year; 44 | break; 45 | case 7: 46 | day_limit = 31; 47 | day_before = 181 + leap_year; 48 | break; 49 | case 8: 50 | day_limit = 31; 51 | day_before = 212 + leap_year; 52 | break; 53 | case 9: 54 | day_limit = 30; 55 | day_before = 243 + leap_year; 56 | break; 57 | case 10: 58 | day_limit = 31; 59 | day_before = 273 + leap_year; 60 | break; 61 | case 11: 62 | day_limit = 30; 63 | day_before = 304 + leap_year; 64 | break; 65 | case 12: 66 | day_limit = 31; 67 | day_before = 334 + leap_year; 68 | break; 69 | 70 | } 71 | if (day > day_limit || day <= 0) 72 | { 73 | cout << "输入错误-日与月的关系非法"; 74 | } 75 | else 76 | { 77 | cout << year << "-" << month << "-" << day << "是"; 78 | cout << year << "年的第" << day_before + day << "天"; 79 | } 80 | } 81 | else 82 | { 83 | cout << "输入错误-月份不正确"; 84 | } 85 | cout << endl; 86 | 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /W0401/3-b7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | double a; 8 | double b; 9 | //小数数位 10 | int d_dec1, d_dec2; 11 | //整数数位 12 | int d1, d2, d3, d4; 13 | int d5, d6, d7, d8; 14 | int d9, d10; 15 | //标记 16 | int tag1 = 0, tag2 = 0, tag3 = 0; 17 | int tag4 = 0; 18 | cout << "请输入[0-100亿)之间的数字: " << endl; 19 | cin >> a; 20 | b = fmod(a, 1); 21 | d_dec2 = static_cast(b * 100.0 + 0.5) % 10; 22 | d_dec1 = static_cast(round(1e1 * fmod(b * 1e+1, 1e+1)) / 1e1); 23 | d1 = static_cast((fmod(a, 1e+1) - fmod(a, 1e+0)) / 1e+0); 24 | d2 = static_cast((fmod(a, 1e+2) - fmod(a, 1e+1)) / 1e+1); 25 | d3 = static_cast((fmod(a, 1e+3) - fmod(a, 1e+2)) / 1e+2); 26 | d4 = static_cast((fmod(a, 1e+4) - fmod(a, 1e+3)) / 1e+3); 27 | d5 = static_cast((fmod(a, 1e+5) - fmod(a, 1e+4)) / 1e+4); 28 | d6 = static_cast((fmod(a, 1e+6) - fmod(a, 1e+5)) / 1e+5); 29 | d7 = static_cast((fmod(a, 1e+7) - fmod(a, 1e+6)) / 1e+6); 30 | d8 = static_cast((fmod(a, 1e+8) - fmod(a, 1e+7)) / 1e+7); 31 | d9 = static_cast((fmod(a, 1e+9) - fmod(a, 1e+8)) / 1e+8); 32 | d10 = static_cast((fmod(a, 1e+10) - fmod(a, 1e+9)) / 1e+9); 33 | 34 | cout << "大写结果是:" << endl; 35 | //亿位 36 | if (d10 != 0 || d9 != 0) 37 | { 38 | tag1 = 1; 39 | if (d10 > 0) 40 | { 41 | switch (d10) 42 | { 43 | case 1: 44 | cout << "壹"; 45 | break; 46 | case 2: 47 | cout << "贰"; 48 | break; 49 | case 3: 50 | cout << "叁"; 51 | break; 52 | case 4: 53 | cout << "肆"; 54 | break; 55 | case 5: 56 | cout << "伍"; 57 | break; 58 | case 6: 59 | cout << "陆"; 60 | break; 61 | case 7: 62 | cout << "柒"; 63 | break; 64 | case 8: 65 | cout << "捌"; 66 | break; 67 | case 9: 68 | cout << "玖"; 69 | break; 70 | } 71 | cout << "拾"; 72 | } 73 | if (d9 > 0) 74 | { 75 | switch (d9) 76 | { 77 | case 1: 78 | cout << "壹"; 79 | break; 80 | case 2: 81 | cout << "贰"; 82 | break; 83 | case 3: 84 | cout << "叁"; 85 | break; 86 | case 4: 87 | cout << "肆"; 88 | break; 89 | case 5: 90 | cout << "伍"; 91 | break; 92 | case 6: 93 | cout << "陆"; 94 | break; 95 | case 7: 96 | cout << "柒"; 97 | break; 98 | case 8: 99 | cout << "捌"; 100 | break; 101 | case 9: 102 | cout << "玖"; 103 | break; 104 | } 105 | } 106 | cout << "亿"; 107 | } 108 | //万位 109 | if (d5 != 0 || d6 != 0 || d7 != 0 || d8 != 0) 110 | { 111 | tag2 = 1; 112 | if (d8 > 0) 113 | { 114 | switch (d8) 115 | { 116 | case 1: 117 | cout << "壹"; 118 | break; 119 | case 2: 120 | cout << "贰"; 121 | break; 122 | case 3: 123 | cout << "叁"; 124 | break; 125 | case 4: 126 | cout << "肆"; 127 | break; 128 | case 5: 129 | cout << "伍"; 130 | break; 131 | case 6: 132 | cout << "陆"; 133 | break; 134 | case 7: 135 | cout << "柒"; 136 | break; 137 | case 8: 138 | cout << "捌"; 139 | break; 140 | case 9: 141 | cout << "玖"; 142 | break; 143 | } 144 | cout << "仟"; 145 | } 146 | else if ((d7 > 0 || d6 > 0 || d5 > 0) && tag1 == 1) 147 | { 148 | cout << "零"; 149 | } 150 | if (d7 > 0) 151 | { 152 | switch (d7) 153 | { 154 | case 1: 155 | cout << "壹"; 156 | break; 157 | case 2: 158 | cout << "贰"; 159 | break; 160 | case 3: 161 | cout << "叁"; 162 | break; 163 | case 4: 164 | cout << "肆"; 165 | break; 166 | case 5: 167 | cout << "伍"; 168 | break; 169 | case 6: 170 | cout << "陆"; 171 | break; 172 | case 7: 173 | cout << "柒"; 174 | break; 175 | case 8: 176 | cout << "捌"; 177 | break; 178 | case 9: 179 | cout << "玖"; 180 | break; 181 | } 182 | cout << "佰"; 183 | } 184 | else if (d8 > 0 && (d6 > 0 || d5 > 0)) 185 | { 186 | cout << "零"; 187 | } 188 | 189 | if (d6 > 0) 190 | { 191 | switch (d6) 192 | { 193 | case 1: 194 | cout << "壹"; 195 | break; 196 | case 2: 197 | cout << "贰"; 198 | break; 199 | case 3: 200 | cout << "叁"; 201 | break; 202 | case 4: 203 | cout << "肆"; 204 | break; 205 | case 5: 206 | cout << "伍"; 207 | break; 208 | case 6: 209 | cout << "陆"; 210 | break; 211 | case 7: 212 | cout << "柒"; 213 | break; 214 | case 8: 215 | cout << "捌"; 216 | break; 217 | case 9: 218 | cout << "玖"; 219 | break; 220 | } 221 | cout << "拾"; 222 | } 223 | else if (d7 > 0 && d5 > 0) 224 | { 225 | cout << "零"; 226 | } 227 | if (d5 > 0) 228 | { 229 | switch (d5) 230 | { 231 | case 1: 232 | cout << "壹"; 233 | break; 234 | case 2: 235 | cout << "贰"; 236 | break; 237 | case 3: 238 | cout << "叁"; 239 | break; 240 | case 4: 241 | cout << "肆"; 242 | break; 243 | case 5: 244 | cout << "伍"; 245 | break; 246 | case 6: 247 | cout << "陆"; 248 | break; 249 | case 7: 250 | cout << "柒"; 251 | break; 252 | case 8: 253 | cout << "捌"; 254 | break; 255 | case 9: 256 | cout << "玖"; 257 | break; 258 | } 259 | } 260 | cout << "万"; 261 | } 262 | 263 | //个位 264 | if (d1 != 0 || d2 != 0 || d3 != 0 || d4 != 0) 265 | { 266 | tag3 = 1; 267 | if (d4 > 0) 268 | { 269 | switch (d4) 270 | { 271 | case 1: 272 | cout << "壹"; 273 | break; 274 | case 2: 275 | cout << "贰"; 276 | break; 277 | case 3: 278 | cout << "叁"; 279 | break; 280 | case 4: 281 | cout << "肆"; 282 | break; 283 | case 5: 284 | cout << "伍"; 285 | break; 286 | case 6: 287 | cout << "陆"; 288 | break; 289 | case 7: 290 | cout << "柒"; 291 | break; 292 | case 8: 293 | cout << "捌"; 294 | break; 295 | case 9: 296 | cout << "玖"; 297 | break; 298 | } 299 | cout << "仟"; 300 | } 301 | else if ((d3 > 0 || d2 > 0 || d1 > 0) && (tag2 == 1 || tag1 == 1)) 302 | { 303 | cout << "零"; 304 | } 305 | if (d3 > 0) 306 | { 307 | switch (d3) 308 | { 309 | case 1: 310 | cout << "壹"; 311 | break; 312 | case 2: 313 | cout << "贰"; 314 | break; 315 | case 3: 316 | cout << "叁"; 317 | break; 318 | case 4: 319 | cout << "肆"; 320 | break; 321 | case 5: 322 | cout << "伍"; 323 | break; 324 | case 6: 325 | cout << "陆"; 326 | break; 327 | case 7: 328 | cout << "柒"; 329 | break; 330 | case 8: 331 | cout << "捌"; 332 | break; 333 | case 9: 334 | cout << "玖"; 335 | break; 336 | } 337 | cout << "佰"; 338 | } 339 | else if (d4 > 0 && (d2 > 0 || d1 > 0)) 340 | { 341 | cout << "零"; 342 | } 343 | 344 | if (d2 > 0) 345 | { 346 | switch (d2) 347 | { 348 | case 1: 349 | cout << "壹"; 350 | break; 351 | case 2: 352 | cout << "贰"; 353 | break; 354 | case 3: 355 | cout << "叁"; 356 | break; 357 | case 4: 358 | cout << "肆"; 359 | break; 360 | case 5: 361 | cout << "伍"; 362 | break; 363 | case 6: 364 | cout << "陆"; 365 | break; 366 | case 7: 367 | cout << "柒"; 368 | break; 369 | case 8: 370 | cout << "捌"; 371 | break; 372 | case 9: 373 | cout << "玖"; 374 | break; 375 | } 376 | cout << "拾"; 377 | } 378 | else if (d3 > 0 && d1 > 0) 379 | { 380 | cout << "零"; 381 | } 382 | if (d1 > 0) 383 | { 384 | switch (d1) 385 | { 386 | case 1: 387 | cout << "壹"; 388 | break; 389 | case 2: 390 | cout << "贰"; 391 | break; 392 | case 3: 393 | cout << "叁"; 394 | break; 395 | case 4: 396 | cout << "肆"; 397 | break; 398 | case 5: 399 | cout << "伍"; 400 | break; 401 | case 6: 402 | cout << "陆"; 403 | break; 404 | case 7: 405 | cout << "柒"; 406 | break; 407 | case 8: 408 | cout << "捌"; 409 | break; 410 | case 9: 411 | cout << "玖"; 412 | break; 413 | } 414 | } 415 | 416 | } 417 | if (tag1 == 0 && tag2 == 0 && tag3 == 0 && d_dec1 == 0 && d_dec2 == 0) 418 | { 419 | cout << "零圆"; 420 | } 421 | if (tag1 != 0 || tag2 != 0 || tag3 != 0) 422 | { 423 | cout << "圆"; 424 | tag4 = 1; 425 | } 426 | //小数 427 | if (d_dec1 == 0 && d_dec2 == 0) 428 | { 429 | cout << "整"; 430 | } 431 | else 432 | { 433 | if (d_dec1 != 0) 434 | { 435 | switch (d_dec1) 436 | { 437 | case 1: 438 | cout << "壹"; 439 | break; 440 | case 2: 441 | cout << "贰"; 442 | break; 443 | case 3: 444 | cout << "叁"; 445 | break; 446 | case 4: 447 | cout << "肆"; 448 | break; 449 | case 5: 450 | cout << "伍"; 451 | break; 452 | case 6: 453 | cout << "陆"; 454 | break; 455 | case 7: 456 | cout << "柒"; 457 | break; 458 | case 8: 459 | cout << "捌"; 460 | break; 461 | case 9: 462 | cout << "玖"; 463 | break; 464 | } 465 | cout << "角"; 466 | if (d_dec2 == 0) 467 | { 468 | cout << "整"; 469 | } 470 | } 471 | else if (tag4 == 1 && d_dec2 != 0) 472 | { 473 | cout << "零"; 474 | } 475 | if (d_dec2 != 0) 476 | { 477 | switch (d_dec2) 478 | { 479 | case 1: 480 | cout << "壹"; 481 | break; 482 | case 2: 483 | cout << "贰"; 484 | break; 485 | case 3: 486 | cout << "叁"; 487 | break; 488 | case 4: 489 | cout << "肆"; 490 | break; 491 | case 5: 492 | cout << "伍"; 493 | break; 494 | case 6: 495 | cout << "陆"; 496 | break; 497 | case 7: 498 | cout << "柒"; 499 | break; 500 | case 8: 501 | cout << "捌"; 502 | break; 503 | case 9: 504 | cout << "玖"; 505 | break; 506 | } 507 | cout << "分"; 508 | } 509 | } 510 | //行末换行 511 | cout << endl; 512 | return 0; 513 | } 514 | -------------------------------------------------------------------------------- /W0401/3-b8.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | double a; 7 | int b; 8 | cout << "请输入找零值:" << endl; 9 | cin >> a; 10 | b = static_cast(a * 100.0 + 0.5); 11 | int change_50 = 0, change_20 = 0, change_10 = 0, change_5 = 0, change_1 = 0; 12 | int change_5jiao = 0, change_1jiao = 0, change_5fen = 0, change_2fen = 0, change_1fen = 0; 13 | int change_counts = 0; 14 | if (b >= 5000) 15 | { 16 | change_50 = b / 5000; 17 | b %= 5000; 18 | change_counts += change_50; 19 | } 20 | if (b >= 2000) 21 | { 22 | change_20 = b / 2000; 23 | b %= 2000; 24 | change_counts += change_20; 25 | } 26 | if (b >= 1000) 27 | { 28 | change_10 = b / 1000; 29 | b %= 1000; 30 | change_counts += change_10; 31 | } 32 | if (b >= 500) 33 | { 34 | change_5 = b / 500; 35 | b %= 500; 36 | change_counts += change_5; 37 | } 38 | if (b >= 100) 39 | { 40 | change_1 = b / 100; 41 | b %= 100; 42 | change_counts += change_1; 43 | } 44 | if (b >= 50) 45 | { 46 | change_5jiao = b / 50; 47 | b %= 50; 48 | change_counts += change_5jiao; 49 | } 50 | if (b >= 10) 51 | { 52 | change_1jiao = b / 10; 53 | b %= 10; 54 | change_counts += change_1jiao; 55 | } 56 | if (b >= 5) 57 | { 58 | change_5fen = b / 5; 59 | b %= 5; 60 | change_counts += change_5fen; 61 | } 62 | if (b >= 2) 63 | { 64 | change_2fen = b / 2; 65 | b %= 2; 66 | change_counts += change_2fen; 67 | } 68 | if (b >= 1) 69 | { 70 | change_1fen = b; 71 | change_counts += change_1fen; 72 | } 73 | cout << "共" << change_counts << "张找零,具体如下:" << endl; 74 | if (change_50 > 0) 75 | { 76 | cout << "50元 : " << change_50 << "张" << endl; 77 | } 78 | if (change_20 > 0) 79 | { 80 | cout << "20元 : " << change_20 << "张" << endl; 81 | } 82 | if (change_10 > 0) 83 | { 84 | cout << "10元 : " << change_10 << "张" << endl; 85 | } 86 | if (change_5 > 0) 87 | { 88 | cout << "5元 : " << change_5 << "张" << endl; 89 | } 90 | if (change_1 > 0) 91 | { 92 | cout << "1元 : " << change_1 << "张" << endl; 93 | } 94 | if (change_5jiao > 0) 95 | { 96 | cout << "5角 : " << change_5jiao << "张" << endl; 97 | } 98 | if (change_1jiao > 0) 99 | { 100 | cout << "1角 : " << change_1jiao << "张" << endl; 101 | } 102 | if (change_5fen > 0) 103 | { 104 | cout << "5分 : " << change_5fen << "张" << endl; 105 | } 106 | if (change_2fen > 0) 107 | { 108 | cout << "2分 : " << change_2fen << "张" << endl; 109 | } 110 | if (change_1fen > 0) 111 | { 112 | cout << "1分 : " << change_1fen << "张" << endl; 113 | } 114 | return 0; 115 | } -------------------------------------------------------------------------------- /W0501/3-b10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | for (int i = 1; i <= 9; i++) 10 | { 11 | for (int j = 1; j <= i; j++) 12 | { 13 | int product = i * j; 14 | cout << j << "x" << i << "=" << product; 15 | cout << " "; 16 | if (product < 10) 17 | { 18 | cout << " "; 19 | } 20 | } 21 | cout << endl; 22 | } 23 | //demo有额外换行 24 | cout << endl; 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /W0501/3-b11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include //取系统时间 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | LARGE_INTEGER tick, begin, end; 10 | 11 | QueryPerformanceFrequency(&tick); //获得计数器频率 12 | QueryPerformanceCounter(&begin); //获得初始硬件计数器计数 13 | 14 | /* 此处是你的程序开始 */ 15 | 16 | int counter = 0; 17 | int no11, no12, no13;//第1个数字的三位 18 | int no21, no22, no23;//第2个数字的三位 19 | int no31, no32, no33;//第3个数字的三位 20 | //如果3*i>=1953(即i>=651),则增大i也不可能等于1953,故终止该循环 21 | //650中含零非法,也不考虑 22 | for (int i = 123; i <= 649; i++) 23 | { 24 | //判断数字重复 第1个三位数 25 | no11 = i % 10; 26 | no12 = i / 10 % 10; 27 | no13 = i / 100; 28 | if (no11 * no12 * no13 == 0) 29 | { 30 | continue; 31 | } 32 | if (no11 == no12 || no11 == no13 || no12 == no13) 33 | { 34 | continue; 35 | } 36 | //如果i+j+(j+1)>1953,则增大j也不可能等于1953,故终止该循环 37 | int temp = 1952 - i;//j可取到的最大值 38 | int jmin = 1953 - i - 987; //如果j过小,得到的k就不符合要求 39 | int jstart = 0; 40 | //确定j的起始值 41 | if (jmin > i+1) 42 | { 43 | jstart = jmin; 44 | } 45 | else 46 | { 47 | jstart = i + 1; 48 | } 49 | for (int j = jstart; 2*j <= temp; j++) 50 | { 51 | //判断数字重复 第2个三位数 52 | no21 = j % 10; 53 | no22 = j / 10 % 10; 54 | no23 = j / 100; 55 | if (no21 * no22 * no23 == 0) 56 | { 57 | continue; 58 | } 59 | if (no21 == no22 || no22 == no23 || no23 == no21) 60 | { 61 | continue; 62 | } 63 | //判断和之前数字是否重复 64 | if (no21 == no11 || no21 == no12 || no21 == no13) 65 | { 66 | continue; 67 | } 68 | if (no22 == no11 || no22 == no12 || no22 == no13) 69 | { 70 | continue; 71 | } 72 | if (no23 == no11 || no23 == no12 || no23 == no13) 73 | { 74 | continue; 75 | } 76 | //第三个数 77 | 78 | int k = 1953 - i - j; 79 | //k值非法,跳过循环 80 | if (k < 124) 81 | { 82 | //最小数位不重复的三位数是123 83 | //由于j增大后k减小,故k<124时直接终止j的循环 84 | break; 85 | } 86 | 87 | //判断数字重复 第3个三位数 88 | no31 = k % 10; 89 | no32 = k / 10 % 10; 90 | no33 = k / 100; 91 | if (no31 * no32 * no33 == 0) 92 | { 93 | continue; 94 | } 95 | if (no31 == no32 || no32 == no33 || no33 == no31) 96 | { 97 | continue; 98 | } 99 | //判断和之前数字是否重复 100 | if (no31 == no11 || no31 == no12 || no31 == no13) 101 | { 102 | continue; 103 | } 104 | if (no32 == no11 || no32 == no12 || no32 == no13) 105 | { 106 | continue; 107 | } 108 | if (no33 == no11 || no33 == no12 || no33 == no13) 109 | { 110 | continue; 111 | } 112 | if (no31 == no21 || no31 == no22 || no31 == no23) 113 | { 114 | continue; 115 | } 116 | if (no32 == no21 || no32 == no22 || no32 == no23) 117 | { 118 | continue; 119 | } 120 | if (no33 == no21 || no33 == no22 || no33 == no23) 121 | { 122 | continue; 123 | } 124 | //全部合法时,进行输出 125 | counter++; 126 | cout << "No." << setw(3) << counter << " : " << i << "+" << j << "+" << k << "=1953" << endl; 127 | } 128 | } 129 | 130 | cout << "total=" << counter << endl; 131 | 132 | 133 | /* 此处是你的程序结束 */ 134 | 135 | QueryPerformanceCounter(&end); //获得终止硬件计数器计数 136 | 137 | cout << "计数器频率 : " << tick.QuadPart << "Hz" << endl; 138 | cout << "计数器计数 : " << end.QuadPart - begin.QuadPart << endl; 139 | cout << setiosflags(ios::fixed) << setprecision(6) << double(end.QuadPart - begin.QuadPart) / tick.QuadPart << "秒" << endl; 140 | 141 | return 0; 142 | } 143 | -------------------------------------------------------------------------------- /W0501/3-b12.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | 4 | int main() 5 | { 6 | int x; 7 | int scanf_return; 8 | int c; 9 | while (1) 10 | { 11 | printf("请输入x的值[0-100] : "); 12 | scanf_return = scanf("%d", &x); //读入x的方式必须是scanf且格式符为%d,不准用非scanf 13 | if (scanf_return == 0) 14 | { 15 | while ((c = getchar()) != '\n' && c != -1) 16 | ; 17 | } 18 | if (x >= 0 && x <= 100) 19 | break; 20 | } 21 | 22 | printf("x=%d\n", x); 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /W0501/3-b12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int x; 7 | 8 | while (1) 9 | { 10 | cout << "请输入x的值[0-100] : "; 11 | cin >> x; 12 | if (cin.fail()) 13 | { 14 | cin.clear(); 15 | cin.ignore(2147483647, '\n'); 16 | continue; 17 | } 18 | if (x >= 0 && x <= 100) 19 | break; 20 | } 21 | cout << "x=" << x << endl; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /W0501/3-b13.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | 4 | int main() 5 | { 6 | //处理输入 7 | int year = -1, month = -1, week = -1, scanf_return = 0, c; 8 | while (1) 9 | { 10 | printf("请输入年份(2000-2030)和月份(1-12) : "); 11 | scanf_return = scanf("%d%d", &year, &month); 12 | if (scanf_return != 2 ) 13 | { 14 | while ((c = getchar()) != '\n' && c != -1) 15 | ; 16 | printf("输入非法,请重新输入\n"); 17 | continue; 18 | } 19 | if (year > 2030 || year < 2000 || month < 1 || month>12) 20 | { 21 | printf("输入非法,请重新输入\n"); 22 | continue; 23 | } 24 | break; 25 | } 26 | while (1) 27 | { 28 | printf("请输入%d年%d月1日的星期(0-6表示星期日-星期六) : ", year, month); 29 | scanf_return = scanf("%d", &week); 30 | if (scanf_return != 1 ) 31 | { 32 | while ((c = getchar()) != '\n' && c != -1) 33 | ; 34 | printf("输入非法,请重新输入\n"); 35 | continue; 36 | } 37 | if (week < 0 || week>6) 38 | { 39 | printf("输入非法,请重新输入\n"); 40 | continue; 41 | } 42 | break; 43 | } 44 | //打印月历 45 | printf("\n%d年%d月的月历为:\n", year, month); 46 | printf("星期日 星期一 星期二 星期三 星期四 星期五 星期六\n"); 47 | int day_limit = 28, leap_year = 0; 48 | if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) 49 | { 50 | leap_year = 1; 51 | } 52 | 53 | switch (month) 54 | { 55 | case 1: 56 | day_limit = 31; 57 | break; 58 | case 2: 59 | day_limit = 28 + leap_year; 60 | break; 61 | case 3: 62 | day_limit = 31; 63 | break; 64 | case 4: 65 | day_limit = 30; 66 | break; 67 | case 5: 68 | day_limit = 31; 69 | break; 70 | case 6: 71 | day_limit = 30; 72 | break; 73 | case 7: 74 | day_limit = 31; 75 | break; 76 | case 8: 77 | day_limit = 31; 78 | break; 79 | case 9: 80 | day_limit = 30; 81 | break; 82 | case 10: 83 | day_limit = 31; 84 | break; 85 | case 11: 86 | day_limit = 30; 87 | break; 88 | case 12: 89 | day_limit = 31; 90 | break; 91 | 92 | } 93 | for (int i = 1 - week, j = 0; i <= day_limit; i++, j++, j %= 7) 94 | { 95 | printf(" "); 96 | if (i <= 0) 97 | { 98 | printf(" "); 99 | } 100 | else if (i < 10) 101 | { 102 | printf(" "); 103 | printf("%d", i); 104 | } 105 | else 106 | { 107 | printf("%d", i); 108 | } 109 | 110 | printf(" "); 111 | //按照Demo输出规则,在星期六输出结束后换行,而非在星期天输出之前换行 112 | if (j == 6) 113 | { 114 | printf("\n"); 115 | } 116 | } 117 | //行末换行 118 | printf("\n"); 119 | return 0; 120 | } -------------------------------------------------------------------------------- /W0501/3-b13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | //处理输入 8 | int year = -1, month = -1, week = -1; 9 | while (1) 10 | { 11 | cout << "请输入年份(2000-2030)和月份(1-12) : "; 12 | cin >> year >> month; 13 | 14 | if (cin.fail()) 15 | { 16 | cin.clear(); 17 | cin.ignore(2147483647, '\n'); 18 | cout << "输入非法,请重新输入" << endl; 19 | continue; 20 | } 21 | if (year > 2030 || year < 2000 || month < 1 || month>12) 22 | { 23 | cout << "输入非法,请重新输入" << endl; 24 | continue; 25 | } 26 | break; 27 | } 28 | while (1) 29 | { 30 | cout << "请输入" << year << "年" << month << "月1日的星期(0-6表示星期日-星期六) : " ; 31 | cin >> week; 32 | if (cin.fail()) 33 | { 34 | cin.clear(); 35 | cin.ignore(2147483647, '\n'); 36 | cout << "输入非法,请重新输入" << endl; 37 | continue; 38 | } 39 | if (week < 0 || week>6) 40 | { 41 | cout << "输入非法,请重新输入" << endl; 42 | continue; 43 | } 44 | break; 45 | } 46 | //打印月历 47 | cout << endl; 48 | cout << year << "年" << month << "月的月历为:" << endl; 49 | cout << "星期日 星期一 星期二 星期三 星期四 星期五 星期六"< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int sum_of_factors = 0; 9 | for (int i = 2; i < 1001; i++) 10 | { 11 | sum_of_factors = 0; 12 | for (int j = 1; j < i; j++) 13 | { 14 | if ( i % j == 0 ) 15 | { 16 | sum_of_factors += j; 17 | } 18 | } 19 | if (sum_of_factors == i) 20 | { 21 | cout << i; 22 | cout << ",its factors are"; 23 | for (int j = 1; j < i; j++) 24 | { 25 | if (i % j == 0) 26 | { 27 | if (j == 1) 28 | { 29 | cout << " " << j; 30 | } 31 | else 32 | { 33 | cout << "," << j; 34 | } 35 | } 36 | } 37 | cout << endl; 38 | } 39 | } 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /W0601-Ch3/3-b10.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | 4 | 5 | int main() 6 | { 7 | for (int i = 1; i <= 9; i++) 8 | { 9 | for (int j = 1; j <= i; j++) 10 | { 11 | int product = i * j; 12 | printf("%dx%d=%d", j, i, product); 13 | printf(" "); 14 | if (product < 10) 15 | { 16 | printf(" "); 17 | } 18 | } 19 | printf("\n"); 20 | } 21 | //demo有额外换行 22 | printf("\n"); 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /W0601-Ch3/3-b11.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include //取系统时间 4 | 5 | int main() 6 | { 7 | LARGE_INTEGER tick, begin, end; 8 | 9 | QueryPerformanceFrequency(&tick); //获得计数器频率 10 | QueryPerformanceCounter(&begin); //获得初始硬件计数器计数 11 | 12 | /* 此处是你的程序开始 */ 13 | 14 | int counter = 0; 15 | int no11, no12, no13;//第1个数字的三位 16 | int no21, no22, no23;//第2个数字的三位 17 | int no31, no32, no33;//第3个数字的三位 18 | //如果3*i>=1953(即i>=651),则增大i也不可能等于1953,故终止该循环 19 | //650中含零非法,也不考虑 20 | for (int i = 123; i <= 649; i++) 21 | { 22 | //判断数字重复 第1个三位数 23 | no11 = i % 10; 24 | no12 = i / 10 % 10; 25 | no13 = i / 100; 26 | if (no11 * no12 * no13 == 0) 27 | { 28 | continue; 29 | } 30 | if (no11 == no12 || no11 == no13 || no12 == no13) 31 | { 32 | continue; 33 | } 34 | //如果i+j+(j+1)>1953,则增大j也不可能等于1953,故终止该循环 35 | int temp = 1952 - i;//j可取到的最大值 36 | int jmin = 1953 - i - 987; //如果j过小,得到的k就不符合要求 37 | int jstart = 0; 38 | //确定j的起始值 39 | if (jmin > i + 1) 40 | { 41 | jstart = jmin; 42 | } 43 | else 44 | { 45 | jstart = i + 1; 46 | } 47 | for (int j = jstart; 2 * j <= temp; j++) 48 | { 49 | //判断数字重复 第2个三位数 50 | no21 = j % 10; 51 | no22 = j / 10 % 10; 52 | no23 = j / 100; 53 | if (no21 * no22 * no23 == 0) 54 | { 55 | continue; 56 | } 57 | if (no21 == no22 || no22 == no23 || no23 == no21) 58 | { 59 | continue; 60 | } 61 | //判断和之前数字是否重复 62 | if (no21 == no11 || no21 == no12 || no21 == no13) 63 | { 64 | continue; 65 | } 66 | if (no22 == no11 || no22 == no12 || no22 == no13) 67 | { 68 | continue; 69 | } 70 | if (no23 == no11 || no23 == no12 || no23 == no13) 71 | { 72 | continue; 73 | } 74 | //第三个数 75 | 76 | int k = 1953 - i - j; 77 | //k值非法,跳过循环 78 | if (k < 124) 79 | { 80 | //最小数位不重复的三位数是123 81 | //由于j增大后k减小,故k<124时直接终止j的循环 82 | break; 83 | } 84 | 85 | //判断数字重复 第3个三位数 86 | no31 = k % 10; 87 | no32 = k / 10 % 10; 88 | no33 = k / 100; 89 | if (no31 * no32 * no33 == 0) 90 | { 91 | continue; 92 | } 93 | if (no31 == no32 || no32 == no33 || no33 == no31) 94 | { 95 | continue; 96 | } 97 | //判断和之前数字是否重复 98 | if (no31 == no11 || no31 == no12 || no31 == no13) 99 | { 100 | continue; 101 | } 102 | if (no32 == no11 || no32 == no12 || no32 == no13) 103 | { 104 | continue; 105 | } 106 | if (no33 == no11 || no33 == no12 || no33 == no13) 107 | { 108 | continue; 109 | } 110 | if (no31 == no21 || no31 == no22 || no31 == no23) 111 | { 112 | continue; 113 | } 114 | if (no32 == no21 || no32 == no22 || no32 == no23) 115 | { 116 | continue; 117 | } 118 | if (no33 == no21 || no33 == no22 || no33 == no23) 119 | { 120 | continue; 121 | } 122 | //全部合法时,进行输出 123 | counter++; 124 | printf("No.%3d : %d+%d+%d=1953\n", counter, i, j, k); 125 | 126 | } 127 | } 128 | printf("total=%d\n", counter); 129 | 130 | 131 | /* 此处是你的程序结束 */ 132 | 133 | QueryPerformanceCounter(&end); //获得终止硬件计数器计数 134 | 135 | //QuadPart是longlong类型 136 | printf("计数器频率 : %I64dHz\n", tick.QuadPart); 137 | printf("计数器计数 : %I64d\n", end.QuadPart - begin.QuadPart); 138 | printf("%.6f秒\n", (double)(end.QuadPart - begin.QuadPart) / tick.QuadPart); 139 | return 0; 140 | } 141 | -------------------------------------------------------------------------------- /W0601-Ch3/3-b6.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | 4 | int main() 5 | { 6 | int year, month, day, day_limit = 0, day_before = 0; 7 | int leap_year = 0; 8 | printf("请输入,年,月,日\n"); 9 | int scanfrtn = scanf("%d%d%d", &year, &month, &day); 10 | scanfrtn++; //处理W4级别的4198 11 | //先判断闰年 12 | if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) 13 | { 14 | leap_year = 1; 15 | } 16 | //判断月份 17 | if (month > 0 && month <= 12) 18 | { 19 | switch (month) 20 | { 21 | case 1: 22 | day_limit = 31; 23 | day_before = 0; 24 | break; 25 | case 2: 26 | day_limit = 28 + leap_year; 27 | day_before = 31; 28 | break; 29 | case 3: 30 | day_limit = 31; 31 | day_before = 59 + leap_year; 32 | break; 33 | case 4: 34 | day_limit = 30; 35 | day_before = 90 + leap_year; 36 | break; 37 | case 5: 38 | day_limit = 31; 39 | day_before = 120 + leap_year; 40 | break; 41 | case 6: 42 | day_limit = 30; 43 | day_before = 151 + leap_year; 44 | break; 45 | case 7: 46 | day_limit = 31; 47 | day_before = 181 + leap_year; 48 | break; 49 | case 8: 50 | day_limit = 31; 51 | day_before = 212 + leap_year; 52 | break; 53 | case 9: 54 | day_limit = 30; 55 | day_before = 243 + leap_year; 56 | break; 57 | case 10: 58 | day_limit = 31; 59 | day_before = 273 + leap_year; 60 | break; 61 | case 11: 62 | day_limit = 30; 63 | day_before = 304 + leap_year; 64 | break; 65 | case 12: 66 | day_limit = 31; 67 | day_before = 334 + leap_year; 68 | break; 69 | 70 | } 71 | if (day > day_limit || day <= 0) 72 | { 73 | printf("输入错误-日与月的关系非法"); 74 | } 75 | else 76 | { 77 | printf("%d-%d-%d", year, month, day); 78 | printf("是"); 79 | printf("%d年的第%d天", year, day_before + day); 80 | 81 | } 82 | } 83 | else 84 | { 85 | printf("输入错误-月份不正确"); 86 | } 87 | printf("\n"); 88 | 89 | return 0; 90 | } 91 | -------------------------------------------------------------------------------- /W0601-Ch3/3-b8.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | 4 | int main() 5 | { 6 | double a; 7 | int b; 8 | printf("请输入找零值:\n"); 9 | int scanfrtn = scanf("%lf,", &a); 10 | scanfrtn++; //处理W4级别的C4198 11 | b = (int)(a * 100.0 + 0.5); 12 | int change_50 = 0, change_20 = 0, change_10 = 0, change_5 = 0, change_1 = 0; 13 | int change_5jiao = 0, change_1jiao = 0, change_5fen = 0, change_2fen = 0, change_1fen = 0; 14 | int change_counts = 0; 15 | if (b >= 5000) 16 | { 17 | change_50 = b / 5000; 18 | b %= 5000; 19 | change_counts += change_50; 20 | } 21 | if (b >= 2000) 22 | { 23 | change_20 = b / 2000; 24 | b %= 2000; 25 | change_counts += change_20; 26 | } 27 | if (b >= 1000) 28 | { 29 | change_10 = b / 1000; 30 | b %= 1000; 31 | change_counts += change_10; 32 | } 33 | if (b >= 500) 34 | { 35 | change_5 = b / 500; 36 | b %= 500; 37 | change_counts += change_5; 38 | } 39 | if (b >= 100) 40 | { 41 | change_1 = b / 100; 42 | b %= 100; 43 | change_counts += change_1; 44 | } 45 | if (b >= 50) 46 | { 47 | change_5jiao = b / 50; 48 | b %= 50; 49 | change_counts += change_5jiao; 50 | } 51 | if (b >= 10) 52 | { 53 | change_1jiao = b / 10; 54 | b %= 10; 55 | change_counts += change_1jiao; 56 | } 57 | if (b >= 5) 58 | { 59 | change_5fen = b / 5; 60 | b %= 5; 61 | change_counts += change_5fen; 62 | } 63 | if (b >= 2) 64 | { 65 | change_2fen = b / 2; 66 | b %= 2; 67 | change_counts += change_2fen; 68 | } 69 | if (b >= 1) 70 | { 71 | change_1fen = b; 72 | change_counts += change_1fen; 73 | } 74 | printf("共%d张找零,具体如下:\n", change_counts); 75 | if (change_50 > 0) 76 | { 77 | printf("50元 : %d张\n", change_50); 78 | } 79 | if (change_20 > 0) 80 | { 81 | printf("20元 : %d张\n", change_20); 82 | } 83 | if (change_10 > 0) 84 | { 85 | printf("10元 : %d张\n", change_10); 86 | } 87 | if (change_5 > 0) 88 | { 89 | printf("5元 : %d张\n", change_5); 90 | } 91 | if (change_1 > 0) 92 | { 93 | printf("1元 : %d张\n", change_1); 94 | } 95 | if (change_5jiao > 0) 96 | { 97 | printf("5角 : %d张\n", change_5jiao); 98 | } 99 | if (change_1jiao > 0) 100 | { 101 | printf("1角 : %d张\n", change_1jiao); 102 | } 103 | if (change_5fen > 0) 104 | { 105 | printf("5分 : %d张\n", change_5fen); 106 | } 107 | if (change_2fen > 0) 108 | { 109 | printf("2分 : %d张\n", change_2fen); 110 | } 111 | if (change_1fen > 0) 112 | { 113 | printf("1分 : %d张\n", change_1fen); 114 | } 115 | return 0; 116 | } -------------------------------------------------------------------------------- /W0601-Ch3/3-b9.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | 4 | int main() 5 | { 6 | int sum_of_factors = 0; 7 | for (int i = 2; i < 1001; i++) 8 | { 9 | sum_of_factors = 0; 10 | for (int j = 1; j < i; j++) 11 | { 12 | if (i % j == 0) 13 | { 14 | sum_of_factors += j; 15 | } 16 | } 17 | if (sum_of_factors == i) 18 | { 19 | printf("%d,its factors are", i); 20 | 21 | for (int j = 1; j < i; j++) 22 | { 23 | if (i % j == 0) 24 | { 25 | if (j == 1) 26 | { 27 | printf(" %d", j); 28 | 29 | } 30 | else 31 | { 32 | printf(",%d", j); 33 | } 34 | } 35 | } 36 | printf("\n"); 37 | } 38 | } 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /W0601-Ch4/4-b1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | /*************************************************************************** 6 | 函数名称: 7 | 功 能:输出大写的0~9 8 | 输入参数: 9 | 返 回 值: 10 | 说 明:除本函数外,不允许任何函数中输出“零”-“玖”!!!!!! 11 | ***************************************************************************/ 12 | void daxie(int num, int flag_of_zero) 13 | { 14 | /* 不允许对本函数做任何修改 */ 15 | switch (num) { 16 | case 0: 17 | if (flag_of_zero) //此标记什么意思请自行思考 18 | cout << "零"; 19 | break; 20 | case 1: 21 | cout << "壹"; 22 | break; 23 | case 2: 24 | cout << "贰"; 25 | break; 26 | case 3: 27 | cout << "叁"; 28 | break; 29 | case 4: 30 | cout << "肆"; 31 | break; 32 | case 5: 33 | cout << "伍"; 34 | break; 35 | case 6: 36 | cout << "陆"; 37 | break; 38 | case 7: 39 | cout << "柒"; 40 | break; 41 | case 8: 42 | cout << "捌"; 43 | break; 44 | case 9: 45 | cout << "玖"; 46 | break; 47 | default: 48 | cout << "error"; 49 | break; 50 | } 51 | } 52 | 53 | /* 可根据需要自定义其它函数(也可以不定义) */ 54 | int main() 55 | { 56 | double a; 57 | double b; 58 | //小数数位 59 | int d_dec1, d_dec2; 60 | //整数数位 61 | int d1, d2, d3, d4; 62 | int d5, d6, d7, d8; 63 | int d9, d10; 64 | //标记 65 | int tag1 = 0, tag2 = 0, tag3 = 0; 66 | int tag4 = 0; 67 | cout << "请输入[0-100亿)之间的数字: " << endl; 68 | cin >> a; 69 | b = fmod(a, 1); 70 | d_dec2 = static_cast(b * 100.0 + 0.5) % 10; 71 | d_dec1 = static_cast(round(1e1 * fmod(b * 1e+1, 1e+1)) / 1e1); 72 | d1 = static_cast((fmod(a, 1e+1) - fmod(a, 1e+0)) / 1e+0); 73 | d2 = static_cast((fmod(a, 1e+2) - fmod(a, 1e+1)) / 1e+1); 74 | d3 = static_cast((fmod(a, 1e+3) - fmod(a, 1e+2)) / 1e+2); 75 | d4 = static_cast((fmod(a, 1e+4) - fmod(a, 1e+3)) / 1e+3); 76 | d5 = static_cast((fmod(a, 1e+5) - fmod(a, 1e+4)) / 1e+4); 77 | d6 = static_cast((fmod(a, 1e+6) - fmod(a, 1e+5)) / 1e+5); 78 | d7 = static_cast((fmod(a, 1e+7) - fmod(a, 1e+6)) / 1e+6); 79 | d8 = static_cast((fmod(a, 1e+8) - fmod(a, 1e+7)) / 1e+7); 80 | d9 = static_cast((fmod(a, 1e+9) - fmod(a, 1e+8)) / 1e+8); 81 | d10 = static_cast((fmod(a, 1e+10) - fmod(a, 1e+9)) / 1e+9); 82 | 83 | cout << "大写结果是:" << endl; 84 | //亿位 85 | if (d10 != 0 || d9 != 0) 86 | { 87 | tag1 = 1; 88 | if (d10 > 0) 89 | { 90 | daxie(d10, 0); 91 | cout << "拾"; 92 | } 93 | if (d9 > 0) 94 | { 95 | daxie(d9, 0); 96 | } 97 | cout << "亿"; 98 | } 99 | //万位 100 | if (d5 != 0 || d6 != 0 || d7 != 0 || d8 != 0) 101 | { 102 | tag2 = 1; 103 | if (d8 > 0) 104 | { 105 | daxie(d8, 0); 106 | cout << "仟"; 107 | } 108 | else if ((d7 > 0 || d6 > 0 || d5 > 0) && tag1 == 1) 109 | { 110 | daxie(0, 1); 111 | } 112 | if (d7 > 0) 113 | { 114 | daxie(d7, 0); 115 | cout << "佰"; 116 | } 117 | else if (d8 > 0 && (d6 > 0 || d5 > 0)) 118 | { 119 | daxie(0, 1); 120 | } 121 | 122 | if (d6 > 0) 123 | { 124 | daxie(d6, 0); 125 | cout << "拾"; 126 | } 127 | else if (d7 > 0 && d5 > 0) 128 | { 129 | daxie(0, 1); 130 | } 131 | if (d5 > 0) 132 | { 133 | daxie(d5, 0); 134 | } 135 | cout << "万"; 136 | } 137 | 138 | //个位 139 | if (d1 != 0 || d2 != 0 || d3 != 0 || d4 != 0) 140 | { 141 | tag3 = 1; 142 | if (d4 > 0) 143 | { 144 | daxie(d4, 0); 145 | cout << "仟"; 146 | } 147 | else if ((d3 > 0 || d2 > 0 || d1 > 0) && (tag2 == 1 || tag1 == 1)) 148 | { 149 | daxie(0, 1); 150 | } 151 | if (d3 > 0) 152 | { 153 | daxie(d3, 0); 154 | cout << "佰"; 155 | } 156 | else if (d4 > 0 && (d2 > 0 || d1 > 0)) 157 | { 158 | daxie(0, 1); 159 | } 160 | 161 | if (d2 > 0) 162 | { 163 | daxie(d2, 0); 164 | cout << "拾"; 165 | } 166 | else if (d3 > 0 && d1 > 0) 167 | { 168 | daxie(0, 1); 169 | } 170 | if (d1 > 0) 171 | { 172 | daxie(d1, 0); 173 | } 174 | 175 | } 176 | if (tag1 == 0 && tag2 == 0 && tag3 == 0 && d_dec1 == 0 && d_dec2 == 0) 177 | { 178 | daxie(0, 1); 179 | cout << "圆"; 180 | } 181 | if (tag1 != 0 || tag2 != 0 || tag3 != 0) 182 | { 183 | cout << "圆"; 184 | tag4 = 1; 185 | } 186 | //小数 187 | if (d_dec1 == 0 && d_dec2 == 0) 188 | { 189 | cout << "整"; 190 | } 191 | else 192 | { 193 | if (d_dec1 != 0) 194 | { 195 | daxie(d_dec1, 0); 196 | cout << "角"; 197 | if (d_dec2 == 0) 198 | { 199 | cout << "整"; 200 | } 201 | } 202 | else if (tag4 == 1 && d_dec2 != 0) 203 | { 204 | daxie(0, 1); 205 | } 206 | if (d_dec2 != 0) 207 | { 208 | daxie(d_dec2, 0); 209 | cout << "分"; 210 | } 211 | } 212 | //行末换行 213 | cout << endl; 214 | return 0; 215 | } 216 | -------------------------------------------------------------------------------- /W0601-Ch4/4-b2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | 4 | int zeller(int y, int m, int d) 5 | { 6 | if (m <= 2) 7 | { 8 | y--; 9 | m += 12; 10 | } 11 | int c = y / 100; 12 | int y2 = y % 100; 13 | int w; 14 | w = y2 + y2 / 4 + c / 4 - 2 * c + 13 * (m + 1) / 5 + d - 1; 15 | w %= 7; 16 | w += 7; 17 | return w % 7; 18 | } 19 | 20 | int main() 21 | { 22 | int y = 0, m = 0, d = 0; 23 | int leap_flag = 0; 24 | int day_limit = 0; 25 | int c; 26 | while (1) 27 | { 28 | printf("请输入年[1900-2100]、月、日:\n"); 29 | while (scanf("%d%d%d", &y, &m, &d)!=3) 30 | { 31 | while ((c = getchar()) != '\n' && c != -1) 32 | ; 33 | printf("输入错误,请重新输入\n"); 34 | 35 | } 36 | 37 | if (y < 1900 || y>2100) 38 | { 39 | printf("年份不正确,请重新输入\n"); 40 | while ((c = getchar()) != '\n' && c != -1) 41 | ; 42 | continue; 43 | } 44 | if ((y % 100 != 0 && y % 4 == 0) || (y % 100 == 0 && y % 400 == 0)) 45 | { 46 | leap_flag = 1; 47 | } 48 | else 49 | { 50 | leap_flag = 0; 51 | } 52 | if (m < 1 || m>12) 53 | { 54 | printf("月份不正确,请重新输入\n"); 55 | while ((c = getchar()) != '\n' && c != -1) 56 | ; 57 | continue; 58 | } 59 | switch (m) 60 | { 61 | case 1: 62 | day_limit = 31; 63 | break; 64 | case 2: 65 | day_limit = 28 + leap_flag; 66 | break; 67 | case 3: 68 | day_limit = 31; 69 | break; 70 | case 4: 71 | day_limit = 30; 72 | break; 73 | case 5: 74 | day_limit = 31; 75 | break; 76 | case 6: 77 | day_limit = 30; 78 | break; 79 | case 7: 80 | day_limit = 31; 81 | break; 82 | case 8: 83 | day_limit = 31; 84 | break; 85 | case 9: 86 | day_limit = 30; 87 | break; 88 | case 10: 89 | day_limit = 31; 90 | break; 91 | case 11: 92 | day_limit = 30; 93 | break; 94 | case 12: 95 | day_limit = 31; 96 | break; 97 | } 98 | if (d > day_limit || d <= 0) 99 | { 100 | printf("日不正确,请重新输入\n"); 101 | while ((c = getchar()) != '\n' && c != -1) 102 | ; 103 | continue; 104 | } 105 | break; 106 | } 107 | int w = zeller(y, m, d); 108 | printf("星期"); 109 | switch (w) 110 | { 111 | case 1: 112 | printf("一"); 113 | break; 114 | case 2: 115 | printf("二"); 116 | break; 117 | case 3: 118 | printf("三"); 119 | break; 120 | case 4: 121 | printf("四"); 122 | break; 123 | case 5: 124 | printf("五"); 125 | break; 126 | case 6: 127 | printf("六"); 128 | break; 129 | case 0: 130 | printf("日"); 131 | break; 132 | } 133 | printf("\n"); 134 | return 0; 135 | } 136 | -------------------------------------------------------------------------------- /W0601-Ch4/4-b2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int zeller(int y, int m, int d) 5 | { 6 | if (m <= 2) 7 | { 8 | y--; 9 | m += 12; 10 | } 11 | int c = y / 100; 12 | int y2 = y % 100; 13 | int w; 14 | w = y2 + y2 / 4 + c / 4 - 2*c + 13 * (m + 1) / 5 + d - 1; 15 | w %= 7; 16 | w += 7; 17 | return w % 7; 18 | } 19 | 20 | int main() 21 | { 22 | int y = 0, m = 0, d = 0; 23 | int leap_flag = 0; 24 | int day_limit = 0; 25 | while (1) 26 | { 27 | cout << "请输入年[1900-2100]、月、日:" << endl; 28 | cin >> y >> m >> d; 29 | if (cin.fail()) 30 | { 31 | cout << "输入错误,请重新输入" << endl; 32 | cin.clear(); 33 | cin.sync(); 34 | cin.ignore(2147483647, '\n'); 35 | continue; 36 | } 37 | if (y < 1900 || y>2100) 38 | { 39 | cout << "年份不正确,请重新输入" << endl; 40 | cin.clear(); 41 | cin.sync(); 42 | cin.ignore(2147483647, '\n'); 43 | continue; 44 | } 45 | if ((y % 100 != 0 && y % 4 == 0) || (y % 100 == 0 && y % 400 == 0)) 46 | { 47 | leap_flag = 1; 48 | } 49 | else 50 | { 51 | leap_flag = 0; 52 | } 53 | if (m < 1 || m>12) 54 | { 55 | cout << "月份不正确,请重新输入" << endl; 56 | cin.clear(); 57 | cin.sync(); 58 | cin.ignore(2147483647, '\n'); 59 | continue; 60 | } 61 | switch (m) 62 | { 63 | case 1: 64 | day_limit = 31; 65 | break; 66 | case 2: 67 | day_limit = 28 + leap_flag; 68 | break; 69 | case 3: 70 | day_limit = 31; 71 | break; 72 | case 4: 73 | day_limit = 30; 74 | break; 75 | case 5: 76 | day_limit = 31; 77 | break; 78 | case 6: 79 | day_limit = 30; 80 | break; 81 | case 7: 82 | day_limit = 31; 83 | break; 84 | case 8: 85 | day_limit = 31; 86 | break; 87 | case 9: 88 | day_limit = 30; 89 | break; 90 | case 10: 91 | day_limit = 31; 92 | break; 93 | case 11: 94 | day_limit = 30; 95 | break; 96 | case 12: 97 | day_limit = 31; 98 | break; 99 | } 100 | if (d > day_limit || d <= 0) 101 | { 102 | cout << "日不正确,请重新输入" << endl; 103 | cin.clear(); 104 | cin.sync(); 105 | cin.ignore(2147483647, '\n'); 106 | continue; 107 | } 108 | break; 109 | } 110 | int w = zeller(y, m, d); 111 | cout << "星期"; 112 | switch (w) 113 | { 114 | case 1: 115 | cout << "一"; 116 | break; 117 | case 2: 118 | cout << "二"; 119 | break; 120 | case 3: 121 | cout << "三"; 122 | break; 123 | case 4: 124 | cout << "四"; 125 | break; 126 | case 5: 127 | cout << "五"; 128 | break; 129 | case 6: 130 | cout << "六"; 131 | break; 132 | case 0: 133 | cout << "日"; 134 | break; 135 | } 136 | cout << endl; 137 | return 0; 138 | } -------------------------------------------------------------------------------- /W0601-Ch4/4-b3.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | 4 | 5 | int zeller(int y, int m, int d) 6 | { 7 | if (m <= 2) 8 | { 9 | y--; 10 | m += 12; 11 | } 12 | int c = y / 100; 13 | int y2 = y % 100; 14 | int w; 15 | w = y2 + y2 / 4 + c / 4 - 2 * c + 13 * (m + 1) / 5 + d - 1; 16 | w %= 7; 17 | w += 7; 18 | return w % 7; 19 | } 20 | 21 | 22 | int main() 23 | { 24 | //处理输入 25 | int year = -1, month = -1, week = -1; 26 | int c; 27 | while (1) 28 | { 29 | printf("请输入年[1900-2100]、月\n"); 30 | 31 | if (scanf("%d%d", &year, &month)!=2) 32 | { 33 | while ((c = getchar()) != '\n' && c != -1) 34 | ; 35 | printf("输入非法,请重新输入\n"); 36 | continue; 37 | } 38 | 39 | if (year > 2100 || year < 1900) 40 | { 41 | printf("年份不正确,请重新输入\n"); 42 | while ((c = getchar()) != '\n' && c != -1) 43 | ; 44 | continue; 45 | } 46 | if (month < 1 || month>12) 47 | { 48 | printf("月份不正确,请重新输入\n"); 49 | while ((c = getchar()) != '\n' && c != -1) 50 | ; 51 | continue; 52 | } 53 | break; 54 | } 55 | week = zeller(year, month, 1); 56 | //打印月历 57 | printf("\n"); 58 | printf("%d年%d月\n", year, month); 59 | printf("======================================================\n"); 60 | printf("星期日 星期一 星期二 星期三 星期四 星期五 星期六\n"); 61 | printf("======================================================\n"); 62 | int day_limit = 28, leap_year = 0; 63 | if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) 64 | { 65 | leap_year = 1; 66 | } 67 | 68 | switch (month) 69 | { 70 | case 1: 71 | day_limit = 31; 72 | break; 73 | case 2: 74 | day_limit = 28 + leap_year; 75 | break; 76 | case 3: 77 | day_limit = 31; 78 | break; 79 | case 4: 80 | day_limit = 30; 81 | break; 82 | case 5: 83 | day_limit = 31; 84 | break; 85 | case 6: 86 | day_limit = 30; 87 | break; 88 | case 7: 89 | day_limit = 31; 90 | break; 91 | case 8: 92 | day_limit = 31; 93 | break; 94 | case 9: 95 | day_limit = 30; 96 | break; 97 | case 10: 98 | day_limit = 31; 99 | break; 100 | case 11: 101 | day_limit = 30; 102 | break; 103 | case 12: 104 | day_limit = 31; 105 | break; 106 | 107 | } 108 | for (int i = 1 - week, j = 0; i <= day_limit; i++, j++, j %= 7) 109 | { 110 | if (i <= 0) 111 | { 112 | printf(" "); 113 | } 114 | else 115 | { 116 | if (i < 10) 117 | { 118 | printf(" %d", i); 119 | } 120 | else 121 | { 122 | printf(" %d", i); 123 | } 124 | } 125 | printf(" "); 126 | //按照Demo输出规则,在星期六输出结束后换行,而非在星期天输出之前换行 127 | if (j == 6) 128 | { 129 | printf("\n"); 130 | } 131 | } 132 | //行末换行 133 | printf("\n"); 134 | printf("\n"); //Demo有额外换行 135 | return 0; 136 | } -------------------------------------------------------------------------------- /W0601-Ch4/4-b3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int zeller(int y, int m, int d) 6 | { 7 | if (m <= 2) 8 | { 9 | y--; 10 | m += 12; 11 | } 12 | int c = y / 100; 13 | int y2 = y % 100; 14 | int w; 15 | w = y2 + y2 / 4 + c / 4 - 2 * c + 13 * (m + 1) / 5 + d - 1; 16 | w %= 7; 17 | w += 7; 18 | return w % 7; 19 | } 20 | 21 | int main() 22 | { 23 | //处理输入 24 | int year = -1, month = -1, week = -1; 25 | while (1) 26 | { 27 | cout << "请输入年[1900-2100]、月" << endl; 28 | cin >> year >> month; 29 | 30 | if (cin.fail()) 31 | { 32 | cin.clear(); 33 | cin.sync(); 34 | cin.ignore(2147483647, '\n'); 35 | cout << "输入非法,请重新输入" << endl; 36 | continue; 37 | } 38 | if (year > 2100 || year < 1900) 39 | { 40 | cin.clear(); 41 | cin.sync(); 42 | cin.ignore(2147483647, '\n'); 43 | cout << "年份不正确,请重新输入" << endl; 44 | continue; 45 | } 46 | if (month < 1 || month>12) 47 | { 48 | cin.clear(); 49 | cin.sync(); 50 | cin.ignore(2147483647, '\n'); 51 | cout << "月份不正确,请重新输入" << endl; 52 | continue; 53 | } 54 | break; 55 | } 56 | week = zeller(year, month, 1); 57 | //打印月历 58 | cout << endl; 59 | cout << year << "年" << month << "月" << endl; 60 | cout << "======================================================" << endl; 61 | cout << "星期日 星期一 星期二 星期三 星期四 星期五 星期六" << endl; 62 | cout << "======================================================" << endl; 63 | int day_limit = 28, leap_year = 0; 64 | if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) 65 | { 66 | leap_year = 1; 67 | } 68 | 69 | switch (month) 70 | { 71 | case 1: 72 | day_limit = 31; 73 | break; 74 | case 2: 75 | day_limit = 28 + leap_year; 76 | break; 77 | case 3: 78 | day_limit = 31; 79 | break; 80 | case 4: 81 | day_limit = 30; 82 | break; 83 | case 5: 84 | day_limit = 31; 85 | break; 86 | case 6: 87 | day_limit = 30; 88 | break; 89 | case 7: 90 | day_limit = 31; 91 | break; 92 | case 8: 93 | day_limit = 31; 94 | break; 95 | case 9: 96 | day_limit = 30; 97 | break; 98 | case 10: 99 | day_limit = 31; 100 | break; 101 | case 11: 102 | day_limit = 30; 103 | break; 104 | case 12: 105 | day_limit = 31; 106 | break; 107 | 108 | } 109 | for (int i = 1 - week, j = 0; i <= day_limit; i++, j++, j %= 7) 110 | { 111 | cout << setw(4); 112 | if (i <= 0) 113 | { 114 | cout << " "; 115 | } 116 | else 117 | { 118 | cout << i; 119 | } 120 | cout << " "; 121 | //按照Demo输出规则,在星期六输出结束后换行,而非在星期天输出之前换行 122 | if (j == 6) 123 | { 124 | cout << endl; 125 | } 126 | } 127 | //行末换行 128 | cout << endl; 129 | cout << endl; //Demo有额外换行 130 | return 0; 131 | } -------------------------------------------------------------------------------- /W0701/4-b10.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | 4 | /* ----具体要求---- 5 | 1、不允许添加其它头文件 6 | 2、不允许定义全局变量、静态局部变量 7 | 3、不允许添加其它函数 8 | 4、整个程序不允许出现任何形式的循环(while、do-while、for、if-goto) 9 | --------------------------------------------------------------------- */ 10 | 11 | /*************************************************************************** 12 | 函数名称: 13 | 功 能:将整数n分解后输出 14 | 输入参数: 15 | 返 回 值: 16 | 说 明:1、函数名、形参、返回类型均不准动 17 | 2、不允许使用64位整数 18 | ***************************************************************************/ 19 | void convert(int n) 20 | { 21 | int x = n % 10; 22 | x = (x < 0) ? -x : x; 23 | printf("%c ", x + '0'); 24 | if (n / 10 != 0) 25 | { 26 | convert(n / 10); 27 | } 28 | else if (n < 0) 29 | { 30 | printf("- "); 31 | } 32 | 33 | } 34 | 35 | /*************************************************************************** 36 | 函数名称: 37 | 功 能: 38 | 输入参数: 39 | 返 回 值: 40 | 说 明:main函数不准动 41 | ***************************************************************************/ 42 | int main() 43 | { 44 | int n; 45 | int scanfret = 0; 46 | printf("请输入一个整数\n"); 47 | scanfret=scanf("%d", &n); 48 | scanfret++; 49 | convert(n); 50 | printf("\n"); 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /W0701/4-b11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | /*************************************************************************** 7 | 函数名称: 8 | 功 能:返回fibonacci数列的第n项的值 9 | 输入参数: 10 | 返 回 值: 11 | 说 明: 12 | ***************************************************************************/ 13 | int fibonacci(int n) 14 | { 15 | return (n <= 2) ? 1 : (fibonacci(n - 1) + fibonacci(n - 2)); 16 | /* 按要求完成本函数 */ 17 | } 18 | 19 | /*************************************************************************** 20 | 函数名称: 21 | 功 能: 22 | 输入参数: 23 | 返 回 值: 24 | 说 明:main函数不允许修改 25 | ***************************************************************************/ 26 | int main() 27 | { 28 | int n, f; 29 | cout << "请输入Fibonacci数列的项数[1-46]" << endl; 30 | cin >> n; 31 | 32 | LARGE_INTEGER tick, begin, end; 33 | QueryPerformanceFrequency(&tick); //获得时钟频率 34 | QueryPerformanceCounter(&begin); //获得初始硬件定时器计数 35 | f = fibonacci(n); 36 | cout << "Fibonacci数列第" << n << "项的值 : " << f << endl; 37 | 38 | QueryPerformanceCounter(&end);//获得终止硬件定时器计数 39 | cout << "计数器频率 : " << tick.QuadPart << "Hz" << endl; 40 | cout << "计数器计数 : " << end.QuadPart - begin.QuadPart << endl; 41 | cout << setiosflags(ios::fixed) << setprecision(6) << double(end.QuadPart - begin.QuadPart) / tick.QuadPart << "秒" << endl; 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /W0701/4-b12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* ----具体要求---- 5 | 1、不允许添加其它头文件 6 | 2、不允许定义全局变量、静态局部变量 7 | 3、不允许添加其它函数 8 | 4、整个程序不允许出现任何形式的循环(while、do-while、for、if-goto) 9 | --------------------------------------------------------------------- */ 10 | 11 | 12 | /*************************************************************************** 13 | 函数名称: 14 | 功 能:求num是不是base的幂 15 | 输入参数: 16 | 返 回 值:0 - 不是 17 | 1 - 是 18 | 说 明:函数名、形参、返回类型均不准动 19 | ***************************************************************************/ 20 | int is_power(int num, int base) 21 | { 22 | if (num == 0) 23 | { 24 | return 0; 25 | } 26 | if (num == 1) 27 | { 28 | return 1; 29 | } 30 | int mod = num % base; 31 | if (mod != 0) 32 | { 33 | return 0; 34 | } 35 | return is_power(num / base, base); 36 | } 37 | 38 | /*************************************************************************** 39 | 函数名称: 40 | 功 能: 41 | 输入参数: 42 | 返 回 值: 43 | 说 明:完成输入、调用递归函数、输出 44 | ***************************************************************************/ 45 | int main() 46 | { 47 | int num, base; 48 | cout << "请输入整数num及基数base" << endl; 49 | cin >> num >> base; 50 | int res = is_power(num, base); 51 | if (res == 1) 52 | { 53 | cout << num << "是" << base << "的幂" << endl; 54 | } 55 | else 56 | { 57 | cout << num << "不是" << base << "的幂" << endl; 58 | } 59 | return 0; 60 | } -------------------------------------------------------------------------------- /W0701/4-b13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | /* ----------------------------------------------------------------------------------- 7 | 允许 :按需增加一个或多个函数,但是所有增加的函数中不允许任何形式的循环 8 | 定义符号常量 9 | 定义const型变量 10 | 不允许 :定义全局变量 11 | ----------------------------------------------------------------------------------- */ 12 | void seq_print_level(int total_levels, int cur_level, int cur_pos) 13 | { 14 | if (cur_pos == cur_level) 15 | { 16 | cout << setw(total_levels - cur_level) << static_cast(cur_pos + 'A'); 17 | if (cur_level != 0) 18 | { 19 | seq_print_level(total_levels, cur_level, cur_pos - 1); 20 | } 21 | } 22 | else if (cur_pos > 0) 23 | { 24 | cout << static_cast(cur_pos + 'A'); 25 | seq_print_level(total_levels, cur_level, cur_pos - 1); 26 | } 27 | else if (cur_pos == 0) 28 | { 29 | cout << 'A'; 30 | if (cur_level != 0) 31 | { 32 | seq_print_level(total_levels, cur_level, cur_pos - 1); 33 | } 34 | } 35 | else if (cur_pos > -cur_level) 36 | { 37 | cout << static_cast(-cur_pos + 'A'); 38 | seq_print_level(total_levels, cur_level, cur_pos - 1); 39 | } 40 | else 41 | { 42 | cout << static_cast(-cur_pos + 'A'); 43 | } 44 | return; 45 | } 46 | 47 | void inv_print_level(int total_levels, int cur_level, int cur_pos) 48 | { 49 | if (cur_pos == cur_level) 50 | { 51 | cout << setw(total_levels - cur_level) << static_cast((total_levels-1-cur_pos) + 'A'); 52 | if (cur_level != 0) 53 | { 54 | inv_print_level(total_levels, cur_level, cur_pos - 1); 55 | } 56 | } 57 | else if (cur_pos > 0) 58 | { 59 | cout << static_cast((total_levels - 1 -cur_pos) + 'A'); 60 | inv_print_level(total_levels, cur_level, cur_pos - 1); 61 | } 62 | else if (cur_pos == 0) 63 | { 64 | cout << static_cast(total_levels-1 + 'A'); 65 | if (cur_level != 0) 66 | { 67 | inv_print_level(total_levels, cur_level, cur_pos - 1); 68 | } 69 | } 70 | else if (cur_pos > -cur_level) 71 | { 72 | cout << static_cast((total_levels - 1 +cur_pos) + 'A'); 73 | inv_print_level(total_levels, cur_level, cur_pos - 1); 74 | } 75 | else 76 | { 77 | cout << static_cast((total_levels - 1 +cur_pos) + 'A'); 78 | } 79 | return; 80 | } 81 | /*************************************************************************** 82 | 函数名称: 83 | 功 能:正向或反向打印三角塔 84 | 输入参数:order - 0 :正三角形塔 85 | order - 1 :倒三角形塔 86 | 返 回 值: 87 | 说 明: 88 | ***************************************************************************/ 89 | void print_tower(char start_ch, char end_ch, int order) 90 | { 91 | /* 按需实现,函数中不允许任何形式的循环,不允许定义静态局部变量 */ 92 | cout << setfill(' '); 93 | if (order == 0) 94 | { 95 | seq_print_level(end_ch - 'A' + 1, start_ch - 'A', start_ch - 'A'); 96 | cout << endl; 97 | if (start_ch != end_ch) 98 | { 99 | print_tower(start_ch + 1, end_ch, order); 100 | } 101 | } 102 | else if (order == 1) 103 | { 104 | inv_print_level(end_ch - 'A' + 1, end_ch - start_ch, end_ch - start_ch); 105 | cout << endl; 106 | if (start_ch != end_ch) 107 | { 108 | print_tower(start_ch + 1, end_ch, order); 109 | } 110 | } 111 | } 112 | 113 | /*************************************************************************** 114 | 函数名称: 115 | 功 能: 116 | 输入参数: 117 | 返 回 值: 118 | 说 明:main函数不准修改 119 | ***************************************************************************/ 120 | int main() 121 | { 122 | char end_ch; 123 | 124 | /* 键盘输入结束字符(仅大写有效) */ 125 | while (1) { 126 | cout << "请输入结束字符(A~Z)" << endl; 127 | end_ch = getchar(); //读缓冲区第一个字符 128 | while (getchar() != '\n') //清空缓冲区剩余字符 129 | ; 130 | if (end_ch >= 'A' && end_ch <= 'Z') 131 | break; 132 | } 133 | 134 | /* 正三角字母塔(中间为A) */ 135 | cout << setfill('=') << setw((end_ch - 'A') * 2 + 1) << '=' << endl;/* 按字母塔最大长度输出= */ 136 | cout << "正三角字母塔" << endl; 137 | cout << setfill('=') << setw((end_ch - 'A') * 2 + 1) << '=' << endl;/* 按字母塔最大长度输出= */ 138 | print_tower('A', end_ch, 0); 139 | cout << endl; 140 | 141 | /* 倒三角字母塔(两边为A) */ 142 | cout << setfill('=') << setw((end_ch - 'A') * 2 + 1) << '=' << endl; /* 按字母塔最大长度输出= */ 143 | cout << "倒三角字母塔" << endl; 144 | cout << setfill('=') << setw((end_ch - 'A') * 2 + 1) << '=' << endl; /* 按字母塔最大长度输出= */ 145 | print_tower('A', end_ch, 1); 146 | cout << endl; 147 | 148 | return 0; 149 | } 150 | -------------------------------------------------------------------------------- /W0701/4-b5.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | 4 | /* ----具体要求---- 5 | 1、不允许添加其它头文件 6 | 2、不允许定义全局变量、静态局部变量 7 | 3、不允许添加其它函数 8 | 4、整个程序不允许出现任何形式的循环(while、do-while、for、if-goto) 9 | --------------------------------------------------------------------- */ 10 | 11 | /*************************************************************************** 12 | 函数名称: 13 | 功 能:返回n的阶乘 14 | 输入参数: 15 | 返 回 值: 16 | 说 明:函数名、形参、返回类型均不准动 17 | ***************************************************************************/ 18 | int fac(int n) 19 | { 20 | return (n == 0 || n == 1) ? 1 : fac(n - 1) * n; 21 | } 22 | 23 | /*************************************************************************** 24 | 函数名称: 25 | 功 能: 26 | 输入参数: 27 | 返 回 值: 28 | 说 明:完成输入、调用递归函数、输出 29 | ***************************************************************************/ 30 | int main() 31 | { 32 | printf("计算a!+b!+c!,请输入a,b,c的值(非负整数)\n"); 33 | int a,b,c; 34 | int scanfret = scanf("%d%d%d", &a, &b, &c); 35 | scanfret++; 36 | printf("%d!+%d!+%d=%d\n",a,b,c,fac(a)+fac(b)+fac(c)); 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /W0701/4-b6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | /* ----具体要求---- 6 | 1、不允许添加其它头文件 7 | 2、不允许定义全局变量、静态局部变量 8 | 3、不允许添加其它函数 9 | 4、整个程序不允许出现任何形式的循环(while、do-while、for、if-goto) 10 | --------------------------------------------------------------------- */ 11 | 12 | /*************************************************************************** 13 | 函数名称: 14 | 功 能:返回n阶legendre多项式的值 15 | 输入参数: 16 | 返 回 值: 17 | 说 明:函数名、形参、返回类型均不准动 18 | ***************************************************************************/ 19 | double legendre(double x, int n) 20 | { 21 | if(n==0) 22 | { 23 | return 1.0; 24 | } 25 | else if (n==1) 26 | { 27 | return x; 28 | } 29 | else 30 | { 31 | double tmp = ((2.0 * n - 1.0) * x * legendre(x, n - 1) - (n - 1.0) * legendre(x, n - 2)); 32 | tmp /= static_cast(n); 33 | return tmp; 34 | } 35 | } 36 | 37 | /*************************************************************************** 38 | 函数名称: 39 | 功 能: 40 | 输入参数: 41 | 返 回 值: 42 | 说 明:完成输入、调用递归函数、输出 43 | ***************************************************************************/ 44 | int main() 45 | { 46 | cout<<"计算legendre,请输入x和n的值"<>x>>n; 50 | cout<<"legendre["< 2 | #include 3 | using namespace std; 4 | 5 | /* ----具体要求---- 6 | 1、不允许添加其它头文件 7 | 2、不允许定义全局变量、静态局部变量 8 | 3、允许添加用于输入层数、起始/目标柱的函数,函数中允许使用循环处理错误输入 9 | 4、如果输入用函数进行,则main中不允许出现任何形式的循环(while、do-while、for、if-goto) 10 | 如果输入在main中进行,则main中允许出现循环 11 | --------------------------------------------------------------------- */ 12 | 13 | /*************************************************************************** 14 | 函数名称: 15 | 功 能:打印n层汉诺塔的移动顺序 16 | 输入参数:int n:层数 17 | char src:起始柱 18 | char tmp:中间柱 19 | char dst:目标柱 20 | 返 回 值: 21 | 说 明:1、函数名、形参、返回类型均不准动 22 | 2、本函数不允许出现任何形式的循环 23 | ***************************************************************************/ 24 | void hanoi(int n, char src, char tmp, char dst) 25 | { 26 | if (n > 1) 27 | { 28 | hanoi(n - 1, src, dst, tmp); 29 | } 30 | //move (src->dst); 31 | cout << setw(2) << n << "# " << src << "-->" << dst << endl; 32 | if (n > 1) 33 | { 34 | hanoi(n - 1, tmp, src, dst); 35 | } 36 | return; 37 | } 38 | 39 | /*************************************************************************** 40 | 函数名称: 41 | 功 能: 42 | 输入参数: 43 | 返 回 值: 44 | 说 明:完成输入(或调用输入函数)、调用递归函数 45 | ***************************************************************************/ 46 | int main() 47 | { 48 | int levels = 0; 49 | char start = 'D', dest = 'D'; 50 | char tmp = 'D'; 51 | int a_used = 0, b_used = 0, c_used = 0; 52 | int cinflag; 53 | while (1) 54 | { 55 | cout << "请输入汉诺塔的层数(1-16)" << endl; 56 | cin >> levels; 57 | cinflag = cin.fail(); 58 | cin.clear(); 59 | cin.ignore(2147483647, '\n'); 60 | if (cinflag || levels <= 0 || levels > 16) 61 | { 62 | continue; 63 | } 64 | break; 65 | } 66 | while (1) 67 | { 68 | cout << "请输入起始柱(A-C)" << endl; 69 | cin >> start; 70 | cinflag = cin.fail(); 71 | cin.clear(); 72 | cin.ignore(2147483647, '\n'); 73 | if (start >= 'a' && start <= 'c') 74 | { 75 | start = start - 'a' + 'A'; 76 | } 77 | if (cinflag || start < 'A' || start>'C') 78 | { 79 | continue; 80 | } 81 | break; 82 | } 83 | while (1) 84 | { 85 | cout << "请输入目标柱(A-C)" << endl; 86 | cin >> dest; 87 | cinflag = cin.fail(); 88 | cin.clear(); 89 | cin.ignore(2147483647, '\n'); 90 | if (dest >= 'a' && dest <= 'c') 91 | { 92 | dest = dest - 'a' + 'A'; 93 | } 94 | if (cinflag || dest < 'A' || dest>'C') 95 | { 96 | continue; 97 | } 98 | if (dest == start) 99 | { 100 | cout << "目标柱(" << dest << ")不能与起始柱(" << start << ")相同" << endl; 101 | continue; 102 | } 103 | break; 104 | } 105 | if (static_cast(start) + static_cast(dest) == static_cast('A') + static_cast('C')) 106 | { 107 | tmp = 'B'; 108 | } 109 | else if (static_cast(start) + static_cast(dest) == static_cast('B') + static_cast('C')) 110 | { 111 | tmp = 'A'; 112 | } 113 | else if (static_cast(start) + static_cast(dest) == static_cast('A') + static_cast('B')) 114 | { 115 | tmp = 'C'; 116 | } 117 | cout << "移动步骤为:" << endl; 118 | hanoi(levels, start, tmp, dest); 119 | return 0; 120 | } 121 | -------------------------------------------------------------------------------- /W0701/4-b8.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | 4 | /* ----具体要求---- 5 | 1、不允许添加其它头文件 6 | 2、允许定义一个全局变量、不允许定义静态局部变量 7 | 3、允许添加用于输入层数、起始/目标柱的函数,函数中允许使用循环处理错误输入 8 | 4、如果输入用函数进行,则main中不允许出现任何形式的循环(while、do-while、for、if-goto) 9 | 如果输入在main中进行,则main中允许出现循环 10 | --------------------------------------------------------------------- */ 11 | int cnt = 0; 12 | 13 | /*************************************************************************** 14 | 函数名称: 15 | 功 能:打印n层汉诺塔的移动顺序 16 | 输入参数:int n:层数 17 | char src:起始柱 18 | char tmp:中间柱 19 | char dst:目标柱 20 | 返 回 值: 21 | 说 明:1、函数名、形参、返回类型均不准动 22 | 2、本函数不允许出现任何形式的循环 23 | ***************************************************************************/ 24 | void hanoi(int n, char src, char tmp, char dst) 25 | { 26 | if (n > 1) 27 | { 28 | hanoi(n - 1, src, dst, tmp); 29 | } 30 | //move (src->dst); 31 | printf("%5d:%3d# %c-->%c\n", ++cnt, n, src, dst); 32 | if (n > 1) 33 | { 34 | hanoi(n - 1, tmp, src, dst); 35 | } 36 | return; 37 | } 38 | 39 | /*************************************************************************** 40 | 函数名称: 41 | 功 能: 42 | 输入参数: 43 | 返 回 值: 44 | 说 明:完成输入(或调用输入函数)、调用递归函数 45 | ***************************************************************************/ 46 | int main() 47 | { 48 | int levels = 0; 49 | char start = 'D', dest = 'D'; 50 | char tmp; 51 | int a_used = 0, b_used = 0, c_used = 0; 52 | int cinflag; 53 | int scanfret = 0, c; 54 | while (1) { 55 | printf("请输入汉诺塔的层数(1-16)\n"); 56 | scanfret = scanf("%d", &levels); 57 | while (1) 58 | { 59 | c = getchar(); 60 | if (c == 0 || c == '\n') 61 | { 62 | break; 63 | } 64 | } 65 | cinflag = scanfret < 1; 66 | if (cinflag || levels <= 0 || levels > 16) 67 | { 68 | continue; 69 | } 70 | break; 71 | } 72 | while (1) { 73 | printf("请输入起始柱(A-C)\n"); 74 | scanfret = scanf("%c", &start); 75 | while (1) 76 | { 77 | c = getchar(); 78 | if (c == 0 || c == '\n') 79 | { 80 | break; 81 | } 82 | } 83 | cinflag = scanfret < 1; 84 | if (start >= 'a' && start <= 'c') 85 | { 86 | start = start - 'a' + 'A'; 87 | } 88 | if (cinflag || start < 'A' || start>'C') 89 | { 90 | continue; 91 | } 92 | break; 93 | } 94 | while (1) { 95 | printf("请输入目标柱(A-C)\n"); 96 | scanfret = scanf("%c", &dest); 97 | while (1) 98 | { 99 | c = getchar(); 100 | if (c == 0 || c == '\n') 101 | { 102 | break; 103 | } 104 | } 105 | cinflag = scanfret < 1; 106 | if (dest >= 'a' && dest <= 'c') 107 | { 108 | dest = dest - 'a' + 'A'; 109 | } 110 | if (cinflag || dest < 'A' || dest>'C') 111 | { 112 | continue; 113 | } 114 | if (dest == start) 115 | { 116 | printf("目标柱(%c)不能与起始柱(%c)相同\n", dest, start); 117 | continue; 118 | } 119 | break; 120 | } 121 | if ((int)(start)+(int)(dest) == (int)('A') + (int)('C')) 122 | { 123 | tmp = 'B'; 124 | } 125 | else if ((int)(start)+(int)(dest) == (int)('B') + (int)('C')) 126 | { 127 | tmp = 'A'; 128 | } 129 | else if ((int)(start)+(int)(dest) == (int)('A') + (int)('B')) 130 | { 131 | tmp = 'C'; 132 | } 133 | printf("移动步骤为:\n"); 134 | hanoi(levels, start, tmp, dest); 135 | return 0; 136 | } 137 | -------------------------------------------------------------------------------- /W0701/4-b8.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | /* ----具体要求---- 6 | 1、不允许添加其它头文件 7 | 2、不允许定义全局变量、仅允许hanoi中定义一个静态局部变量 8 | 3、允许添加用于输入层数、起始/目标柱的函数,函数中允许使用循环处理错误输入 9 | 4、如果输入用函数进行,则main中不允许出现任何形式的循环(while、do-while、for、if-goto) 10 | 如果输入在main中进行,则main中允许出现循环 11 | --------------------------------------------------------------------- */ 12 | 13 | /*************************************************************************** 14 | 函数名称: 15 | 功 能:打印n层汉诺塔的移动顺序 16 | 输入参数:int n:层数 17 | char src:起始柱 18 | char tmp:中间柱 19 | char dst:目标柱 20 | 返 回 值: 21 | 说 明:1、函数名、形参、返回类型均不准动 22 | 2、本函数不允许出现任何形式的循环 23 | 3、允许定义一个静态局部变量 24 | ***************************************************************************/ 25 | void hanoi(int n, char src, char tmp, char dst) 26 | { 27 | static int cnt = 0; 28 | if (n > 1) 29 | { 30 | hanoi(n - 1, src, dst, tmp); 31 | } 32 | //move (src->dst); 33 | cout << setw(5) << (++cnt) << ":" << setw(3) << n << "# " << src << "-->" << dst << endl; 34 | if (n > 1) 35 | { 36 | hanoi(n - 1, tmp, src, dst); 37 | } 38 | return; 39 | } 40 | 41 | /*************************************************************************** 42 | 函数名称: 43 | 功 能: 44 | 输入参数: 45 | 返 回 值: 46 | 说 明:完成输入(或调用输入函数)、调用递归函数 47 | ***************************************************************************/ 48 | int main() 49 | { 50 | int levels = 0; 51 | char start = 'D', dest = 'D'; 52 | char tmp; 53 | int a_used = 0, b_used = 0, c_used = 0; 54 | int cinflag; 55 | while (1) 56 | { 57 | cout << "请输入汉诺塔的层数(1-16)" << endl; 58 | cin >> levels; 59 | cinflag = cin.fail(); 60 | cin.clear(); 61 | cin.ignore(2147483647, '\n'); 62 | if (cinflag || levels <= 0 || levels > 16) 63 | { 64 | continue; 65 | } 66 | break; 67 | } 68 | while (1) { 69 | cout << "请输入起始柱(A-C)" << endl; 70 | cin >> start; 71 | cinflag = cin.fail(); 72 | cin.clear(); 73 | cin.ignore(2147483647, '\n'); 74 | if (start >= 'a' && start <= 'c') 75 | { 76 | start = start - 'a' + 'A'; 77 | } 78 | if (cinflag || start < 'A' || start>'C') 79 | { 80 | continue; 81 | } 82 | break; 83 | } 84 | while (1) 85 | { 86 | cout << "请输入目标柱(A-C)" << endl; 87 | cin >> dest; 88 | cinflag = cin.fail(); 89 | cin.clear(); 90 | cin.ignore(2147483647, '\n'); 91 | if (dest >= 'a' && dest <= 'c') 92 | { 93 | dest = dest - 'a' + 'A'; 94 | } 95 | if (cinflag || dest < 'A' || dest>'C' ) 96 | { 97 | continue; 98 | } 99 | if (dest == start) 100 | { 101 | cout << "目标柱(" << dest << ")不能与起始柱(" << start << ")相同" << endl; 102 | continue; 103 | } 104 | break; 105 | } 106 | if (static_cast(start) + static_cast(dest) == static_cast('A') + static_cast('C')) 107 | { 108 | tmp = 'B'; 109 | } 110 | else if (static_cast(start) + static_cast(dest) == static_cast('B') + static_cast('C')) 111 | { 112 | tmp = 'A'; 113 | } 114 | else if (static_cast(start) + static_cast(dest) == static_cast('A') + static_cast('B')) 115 | { 116 | tmp = 'C'; 117 | } 118 | cout << "移动步骤为:" << endl; 119 | hanoi(levels, start, tmp, dest); 120 | return 0; 121 | } 122 | -------------------------------------------------------------------------------- /W0701/4-b9.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* ----具体要求---- 5 | 1、不允许添加其它头文件 6 | 2、不允许定义全局变量、静态局部变量 7 | 3、不允许添加其它函数 8 | 4、整个程序不允许出现任何形式的循环(while、do-while、for、if-goto) 9 | --------------------------------------------------------------------- */ 10 | 11 | /*************************************************************************** 12 | 函数名称: 13 | 功 能:将整数n分解后输出 14 | 输入参数: 15 | 返 回 值: 16 | 说 明:1、函数名、形参、返回类型均不准动 17 | 2、不允许使用64位整数 18 | ***************************************************************************/ 19 | void convert(int n) 20 | { 21 | int x = n % 10; 22 | if (n / 10 != 0) 23 | { 24 | convert(n / 10); 25 | } 26 | else if (n < 0) 27 | { 28 | cout << '-' << ' '; 29 | } 30 | x = (x < 0) ? -x : x; 31 | cout << char(x + '0') << ' '; 32 | } 33 | 34 | /*************************************************************************** 35 | 函数名称: 36 | 功 能: 37 | 输入参数: 38 | 返 回 值: 39 | 说 明:main函数不准动 40 | ***************************************************************************/ 41 | int main() 42 | { 43 | int n; 44 | cout << "请输入一个整数" << endl; 45 | cin >> n; 46 | 47 | convert(n); 48 | cout << endl; 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /W0801/5-b1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | const int ARRAY_SIZE = 21; 7 | const int INPUT_SIZE_MAX = 20; 8 | const int END_FLAG = 0; 9 | 10 | int array_size = 0; 11 | int array_content[ARRAY_SIZE]; 12 | 13 | cout << "请输入任意个正整数(升序,最多20个),以-1结束" << endl; 14 | for (int i = 0; i < INPUT_SIZE_MAX; i++) 15 | { 16 | cin >> array_content[i]; 17 | if (array_content[i] > END_FLAG) 18 | { 19 | array_size = i + 1; 20 | } 21 | else 22 | { 23 | break; 24 | } 25 | } 26 | cin.clear(); 27 | cin.ignore(2147483647, '\n'); 28 | if (array_size == 0) 29 | { 30 | cout << "无有效输入" << endl; 31 | return 0; 32 | } 33 | cout << "原数组为:" << endl; 34 | for (int i = 0; i < array_size; i++) 35 | { 36 | cout << array_content[i] << " "; 37 | } 38 | int insert_num = 0; 39 | cout << endl; 40 | cout << "请输入要插入的正整数" << endl; 41 | cin >> insert_num; 42 | //查找插入位置 43 | int l = 0, r = array_size - 1; 44 | while (l <= r) 45 | { 46 | int mid = (l + r) / 2; 47 | if (array_content[mid] > insert_num) 48 | { 49 | r = mid - 1; 50 | } 51 | else if(array_content[mid]= l; i--) 63 | { 64 | array_content[i + 1] = array_content[i]; 65 | } 66 | array_content[l] = insert_num; 67 | //输出 68 | cout << "插入后的数组为:" << endl; 69 | for (int i = 0; i <= array_size; i++) 70 | { 71 | cout << array_content[i] << " "; 72 | } 73 | cout << endl; 74 | return 0; 75 | } -------------------------------------------------------------------------------- /W0801/5-b2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | const int LIGHT_CNT = 100; 7 | 8 | int light[LIGHT_CNT]; 9 | for (int i = 0; i < LIGHT_CNT; i++) 10 | { 11 | light[i] = 0; 12 | } 13 | for (int i = 0; i < LIGHT_CNT; i++) 14 | { 15 | for (int j = i; j < LIGHT_CNT; j += (i + 1)) 16 | { 17 | light[j] = 1 - light[j]; 18 | } 19 | } 20 | int first_print = true; 21 | for (int i = 0; i < LIGHT_CNT; i++) 22 | { 23 | if (light[i] == 1) 24 | { 25 | if (first_print) 26 | { 27 | first_print = false; 28 | } 29 | else 30 | { 31 | cout << " "; 32 | } 33 | cout << (i + 1); 34 | } 35 | } 36 | cout << endl; 37 | return 0; 38 | } -------------------------------------------------------------------------------- /W0801/5-b3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | const int ARRAY_SIZE = 13; 7 | int day_limit_map[ARRAY_SIZE] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; 8 | int day_limit_cusum[ARRAY_SIZE] = { 0 }; 9 | int year, month, day, day_limit = 0, day_before = 0; 10 | cout << "请输入,年,月,日" << endl; 11 | 12 | cin >> year >> month >> day; 13 | //先判断闰年 14 | if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) 15 | { 16 | day_limit_map[2]++; 17 | } 18 | //前缀和 19 | for (int i = 1; i < ARRAY_SIZE; i++) 20 | { 21 | day_limit_cusum[i] = day_limit_cusum[i - 1] + day_limit_map[i]; 22 | } 23 | 24 | //判断月份 25 | if (month > 0 && month <= 12) 26 | { 27 | 28 | day_limit = day_limit_map[month]; 29 | day_before = day_limit_cusum[month - 1]; 30 | if (day > day_limit || day <= 0) 31 | { 32 | cout << "输入错误-日与月的关系非法"; 33 | } 34 | else 35 | { 36 | cout << year << "-" << month << "-" << day << "是"; 37 | cout << year << "年的第" << day_before + day << "天"; 38 | } 39 | } 40 | else 41 | { 42 | cout << "输入错误-月份不正确"; 43 | } 44 | cout << endl; 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /W0801/5-b4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | const int STUDENT_SIZE_MAX = 1000; 7 | const int SCORE_UPPER_BOUND = 101; 8 | const int END_FLAG = -1; 9 | const int CRLF_COND = 10; 10 | 11 | int student_score[STUDENT_SIZE_MAX]; 12 | int score_counter[SCORE_UPPER_BOUND]; 13 | int student_size = 0; 14 | //清空 15 | for (int i = 0; i < SCORE_UPPER_BOUND; i++) 16 | { 17 | score_counter[i] = 0; 18 | } 19 | cout << "请输入成绩(最多1000个),以-1结束" << endl; 20 | for (int i = 0; i < STUDENT_SIZE_MAX; i++) 21 | { 22 | cin >> student_score[i]; 23 | if (student_score[i] <= END_FLAG) 24 | { 25 | break; 26 | } 27 | student_size++; 28 | } 29 | if (student_size == 0) { 30 | cout << "无有效输入" << endl; 31 | return 0; 32 | } 33 | cout << "输入的数组为:" << endl; 34 | for (int i = 0; i < student_size; i++) 35 | { 36 | cout << student_score[i] << " "; 37 | if ((i + 1) % CRLF_COND == 0 || i == student_size - 1) 38 | { 39 | cout << endl; 40 | } 41 | score_counter[student_score[i]]++; 42 | } 43 | cout << "分数与人数的对应关系为:" << endl; 44 | for (int i = SCORE_UPPER_BOUND - 1; i >= 0; i--) 45 | { 46 | if (score_counter[i] != 0) 47 | { 48 | cout << i << " " << score_counter[i] << endl; 49 | } 50 | } 51 | return 0; 52 | } -------------------------------------------------------------------------------- /W0801/5-b5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | //常量 7 | //Demo是1001,作业要求是1000,但不考虑输入错误,所以该项可以为任意>=1000的值 8 | const int ARRAY_SIZE_MAX = 1001; 9 | const int END_FLAG = -1; 10 | 11 | //变量 12 | int score_size = 0; 13 | int score_list[ARRAY_SIZE_MAX]; 14 | int read_int = 0; 15 | 16 | //输入处理 17 | cout << "请输入成绩(最多1000个),以-1结束" << endl; 18 | while (1) 19 | { 20 | read_int = END_FLAG; 21 | cin >> read_int; 22 | if (read_int > END_FLAG) 23 | { 24 | score_list[score_size++] = read_int; 25 | if (score_size >= ARRAY_SIZE_MAX) 26 | { 27 | break; 28 | } 29 | continue; 30 | } 31 | if (!score_size) 32 | { 33 | cout << "无有效输入" << endl; 34 | return 0; 35 | } 36 | break; 37 | } 38 | cout << "输入的数组为:" << endl; 39 | for (int i = 0; i < score_size; i++) 40 | { 41 | cout << score_list[i] << " "; 42 | if (i + 1 == score_size || i % 10 == 9) 43 | { 44 | cout << endl; 45 | } 46 | } 47 | cout << "分数与名次的对应关系为:" << endl; 48 | 49 | //堆排序 50 | int heap_adjust_node = score_size; 51 | int heap_size = score_size; 52 | while (heap_adjust_node--) 53 | { 54 | int current_node = heap_adjust_node; 55 | int child_node = current_node * 2 + 1; 56 | int father_value = score_list[heap_adjust_node]; 57 | while (child_node < score_size) 58 | { 59 | //选择更小节点 60 | if (child_node < score_size - 1) 61 | { 62 | if (score_list[child_node + 1] < score_list[child_node]) 63 | { 64 | child_node++; 65 | } 66 | } 67 | //比较结点 68 | if (father_value > score_list[child_node]) 69 | { 70 | score_list[current_node] = score_list[child_node]; 71 | current_node = child_node; 72 | child_node = child_node * 2; 73 | child_node++; 74 | } 75 | else 76 | { 77 | break; 78 | } 79 | } 80 | score_list[current_node] = father_value; 81 | } 82 | while (heap_size > 1) 83 | { 84 | //交换 85 | int temp = score_list[0]; 86 | score_list[0] = score_list[--heap_size]; 87 | score_list[heap_size] = temp; 88 | 89 | 90 | int current_node = 0; 91 | int child_node = current_node * 2 + 1; 92 | int father_value = score_list[0]; 93 | while (child_node < heap_size) 94 | { 95 | //选择更小节点 96 | if (child_node < heap_size - 1) 97 | { 98 | if (score_list[child_node + 1] < score_list[child_node]) 99 | { 100 | child_node++; 101 | } 102 | } 103 | //比较结点 104 | if (father_value > score_list[child_node]) 105 | { 106 | score_list[current_node] = score_list[child_node]; 107 | current_node = child_node; 108 | child_node = child_node * 2; 109 | child_node++; 110 | } 111 | else 112 | { 113 | break; 114 | } 115 | } 116 | score_list[current_node] = father_value; 117 | } 118 | //输出 119 | int last_output = -1, parallel_counter = 1, rank = 0; 120 | for (int i = 0; i < score_size; i++) 121 | { 122 | if (score_list[i] == last_output) 123 | { 124 | parallel_counter++; 125 | } 126 | else 127 | { 128 | last_output = score_list[i]; 129 | rank += parallel_counter; 130 | parallel_counter = 1; 131 | } 132 | cout << score_list[i] << ' ' << rank << endl; 133 | } 134 | 135 | return 0; 136 | } 137 | -------------------------------------------------------------------------------- /W0801/5-b6-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | const int STACK_SIZE = 10; 6 | const int COL_COUNT = 3; 7 | 8 | int stack_top_a = 0; 9 | int stack_top_b = 0; 10 | int stack_top_c = 0; 11 | int move_counter = 0; 12 | 13 | int stack_a[STACK_SIZE]; 14 | int stack_b[STACK_SIZE]; 15 | int stack_c[STACK_SIZE]; 16 | 17 | void result_print(int is_first, int lvl, char src,char dst) 18 | { 19 | if (is_first) 20 | { 21 | cout << "��ʼ:" << " "; 22 | } 23 | else 24 | { 25 | cout << "��" << setw(4) << move_counter << " ��(" << setw(2) << lvl << "): " << src << "-->" << dst; 26 | } 27 | cout << " A:"; 28 | for (int i = 0; i < stack_top_a; i++) 29 | { 30 | cout << setw(2)<< stack_a[i]; 31 | } 32 | for (int i = stack_top_a ; i < 10; i++) 33 | { 34 | cout << " "; 35 | } 36 | cout << " B:"; 37 | for (int i = 0; i < stack_top_b; i++) 38 | { 39 | cout << setw(2)<< stack_b[i] ; 40 | } 41 | for (int i = stack_top_b ; i < 10; i++) 42 | { 43 | cout << " "; 44 | } 45 | cout << " C:"; 46 | for (int i = 0; i < stack_top_c; i++) 47 | { 48 | cout <> levels; 125 | cinflag = cin.fail(); 126 | cin.clear(); 127 | cin.ignore(2147483647, '\n'); 128 | if (cinflag || levels <= 0 || levels > 10) 129 | { 130 | continue; 131 | } 132 | break; 133 | } 134 | while (1) 135 | { 136 | cout << "��������ʼ��(A-C)��" << endl; 137 | cin >> start; 138 | cinflag = cin.fail(); 139 | cin.clear(); 140 | cin.ignore(2147483647, '\n'); 141 | if (start >= 'a' && start <= 'c') 142 | { 143 | start = start - 'a' + 'A'; 144 | } 145 | if (cinflag || start < 'A' || start>'C') 146 | { 147 | continue; 148 | } 149 | break; 150 | } 151 | if (start == 'A') 152 | { 153 | for (int i = levels; i >= 1; i--) 154 | { 155 | stack_a[stack_top_a++] = i; 156 | } 157 | } 158 | if (start == 'B') 159 | { 160 | for (int i = levels; i >= 1; i--) 161 | { 162 | stack_b[stack_top_b++] = i; 163 | } 164 | } 165 | if (start == 'C') 166 | { 167 | for (int i = levels; i >= 1; i--) 168 | { 169 | stack_c[stack_top_c++] = i; 170 | } 171 | } 172 | while (1) 173 | { 174 | cout << "������Ŀ����(A-C)��" << endl; 175 | cin >> dest; 176 | cinflag = cin.fail(); 177 | cin.clear(); 178 | cin.ignore(2147483647, '\n'); 179 | if (dest >= 'a' && dest <= 'c') 180 | { 181 | dest = dest - 'a' + 'A'; 182 | } 183 | if (cinflag || dest < 'A' || dest>'C') 184 | { 185 | continue; 186 | } 187 | if (dest == start) 188 | { 189 | cout << "Ŀ����(" << dest << ")��������ʼ��(" << start << ")��ͬ" << endl; 190 | continue; 191 | } 192 | break; 193 | } 194 | if (static_cast(start) + static_cast(dest) == static_cast('A') + static_cast('C')) 195 | { 196 | tmp = 'B'; 197 | } 198 | else if (static_cast(start) + static_cast(dest) == static_cast('B') + static_cast('C')) 199 | { 200 | tmp = 'A'; 201 | } 202 | else if (static_cast(start) + static_cast(dest) == static_cast('A') + static_cast('B')) 203 | { 204 | tmp = 'C'; 205 | } 206 | //cout << "�ƶ�����Ϊ:" << endl; 207 | result_print(1, 0, 0, 0); 208 | hanoi(levels, start, tmp, dest); 209 | 210 | return 0; 211 | } 212 | -------------------------------------------------------------------------------- /W0801/5-b6-2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | 4 | /* ----具体要求---- 5 | 1、不允许添加其它头文件 6 | 2、允许定义一个全局变量、不允许定义静态局部变量 7 | 3、允许添加用于输入层数、起始/目标柱的函数,函数中允许使用循环处理错误输入 8 | 4、如果输入用函数进行,则main中不允许出现任何形式的循环(while、do-while、for、if-goto) 9 | 如果输入在main中进行,则main中允许出现循环 10 | --------------------------------------------------------------------- */ 11 | #define STACK_SIZE 10 12 | #define COL_COUNT 3 13 | 14 | int move_counter = 0; 15 | int stack_top[COL_COUNT] = { 0,0,0 }; 16 | int stack_body[COL_COUNT][STACK_SIZE]; 17 | 18 | void result_print(int is_first, int lvl, char src, char dst) 19 | { 20 | if (is_first) 21 | { 22 | printf("初始: "); 23 | } 24 | else 25 | { 26 | printf("第%4d 步(%2d): %c-->%c", move_counter, lvl, src, dst); 27 | } 28 | printf(" A:"); 29 | for (int i = 0; i 10) 116 | { 117 | continue; 118 | } 119 | break; 120 | } 121 | while (1) 122 | { 123 | printf("请输入起始柱(A-C):\n"); 124 | scanfret = scanf("%c", &start); 125 | while (1) 126 | { 127 | c = getchar(); 128 | if (c == 0 || c == '\n') 129 | { 130 | break; 131 | } 132 | } 133 | cinflag = scanfret < 1; 134 | if (start >= 'a' && start <= 'c') 135 | { 136 | start = start - 'a' + 'A'; 137 | } 138 | if (cinflag || start < 'A' || start>'C') 139 | { 140 | continue; 141 | } 142 | break; 143 | } 144 | for (int i = levels; i >= 1; i--) 145 | { 146 | stack_body[start - 'A'][stack_top[start - 'A']++] = i; 147 | } 148 | while (1) 149 | { 150 | printf("请输入目标柱(A-C):\n"); 151 | scanfret = scanf("%c", &dest); 152 | while (1) 153 | { 154 | c = getchar(); 155 | if (c == 0 || c == '\n') 156 | { 157 | break; 158 | } 159 | } 160 | cinflag = scanfret < 1; 161 | if (dest >= 'a' && dest <= 'c') 162 | { 163 | dest = dest - 'a' + 'A'; 164 | } 165 | if (cinflag || dest < 'A' || dest>'C') 166 | { 167 | continue; 168 | } 169 | if (dest == start) 170 | { 171 | printf("目标柱(%c)不能与起始柱(%c)相同\n", dest, start); 172 | continue; 173 | } 174 | break; 175 | } 176 | if ((int)(start)+(int)(dest) == (int)('A') + (int)('C')) 177 | { 178 | tmp = 'B'; 179 | } 180 | else if ((int)(start)+(int)(dest) == (int)('B') + (int)('C')) 181 | { 182 | tmp = 'A'; 183 | } 184 | else if ((int)(start)+(int)(dest) == (int)('A') + (int)('B')) 185 | { 186 | tmp = 'C'; 187 | } 188 | //printf("移动步骤为:\n"); 189 | result_print(1, 0, 0, 0); 190 | hanoi(levels, start, tmp, dest); 191 | return 0; 192 | } 193 | -------------------------------------------------------------------------------- /W0901/4-b19.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int max(int a, int b) 8 | { 9 | return (a > b) ? a : b; 10 | } 11 | int max(int a, int b, int c) 12 | { 13 | return max(a, max(b, c)); 14 | } 15 | int max(int a, int b, int c, int d) 16 | { 17 | return max(max(a, b), max(c, d)); 18 | } 19 | 20 | int main() 21 | { 22 | int a, b, c, d, n = 15; 23 | int f = 0; 24 | while (1) 25 | { 26 | f = 0; 27 | cout << "请输入个数num及num个正整数:" << endl; 28 | cin >> n; 29 | if (cin.fail()) 30 | { 31 | cout << "个数输入错误" << endl; 32 | return 0; 33 | } 34 | if (n > 4 || n < 2) 35 | { 36 | cout << "个数输入错误" << endl; 37 | return 0; 38 | } 39 | for (int i = 1; i <= n; i++) 40 | { 41 | if (i == 1) 42 | { 43 | a = 0; 44 | cin >> a; 45 | if (cin.fail()||a<=0) 46 | { 47 | cin.clear(); 48 | cin.ignore(65536, '\n'); 49 | f = 1; 50 | break; 51 | } 52 | } 53 | if (i == 2) 54 | { 55 | b = 0; 56 | cin >> b; 57 | if (cin.fail() || b <= 0) 58 | { 59 | cin.clear(); 60 | cin.ignore(65536, '\n'); 61 | f = 1; 62 | break; 63 | } 64 | } 65 | if (i == 3) 66 | { 67 | c = 0; 68 | cin >> c; 69 | if (cin.fail() || c <= 0) 70 | { 71 | cin.clear(); 72 | cin.ignore(65536, '\n'); 73 | f = 1; 74 | break; 75 | } 76 | } 77 | if (i == 4) 78 | { 79 | d = 0; 80 | cin >>d; 81 | if (cin.fail() || d <= 0) 82 | { 83 | cin.clear(); 84 | cin.ignore(65536, '\n'); 85 | f = 1; 86 | break; 87 | } 88 | } 89 | } 90 | if (f == 0) 91 | { 92 | break; 93 | } 94 | } 95 | int ans = 0; 96 | switch (n) { 97 | case 2: 98 | ans = max(a, b); 99 | break; 100 | case 3: 101 | ans = max(a, b, c); 102 | break; 103 | case 4: 104 | ans = max(a, b, c, d); 105 | break; 106 | default: 107 | break; 108 | } 109 | cout << "max=" << ans << endl; 110 | return 0; 111 | } -------------------------------------------------------------------------------- /W0901/4-b20.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int min(int a, int b= 2147483647, int c=2147483647, int d= 2147483647) 8 | { 9 | int f = (a > b) ? b : a; 10 | int g = (c > d) ? d : c; 11 | return (f > g) ? g : f; 12 | } 13 | int main() 14 | { 15 | int a, b, c, d, n = 15; 16 | int f = 0; 17 | while (1) 18 | { 19 | f = 0; 20 | cout << "请输入个数num及num个正整数:" << endl; 21 | cin >> n; 22 | if (cin.fail()) 23 | { 24 | cout << "个数输入错误" << endl; 25 | return 0; 26 | } 27 | if (n > 4 || n < 2) 28 | { 29 | cout << "个数输入错误" << endl; 30 | return 0; 31 | } 32 | for (int i = 1; i <= n; i++) 33 | { 34 | if (i == 1) 35 | { 36 | a = 0; 37 | cin >> a; 38 | if (cin.fail() || a <= 0) 39 | { 40 | cin.clear(); 41 | cin.ignore(65536, '\n'); 42 | f = 1; 43 | break; 44 | } 45 | } 46 | if (i == 2) 47 | { 48 | b = 0; 49 | cin >> b; 50 | if (cin.fail() || b <= 0) 51 | { 52 | cin.clear(); 53 | cin.ignore(65536, '\n'); 54 | f = 1; 55 | break; 56 | } 57 | } 58 | if (i == 3) 59 | { 60 | c = 0; 61 | cin >> c; 62 | if (cin.fail() || c <= 0) 63 | { 64 | cin.clear(); 65 | cin.ignore(65536, '\n'); 66 | f = 1; 67 | break; 68 | } 69 | } 70 | if (i == 4) 71 | { 72 | d = 0; 73 | cin >> d; 74 | if (cin.fail() || d <= 0) 75 | { 76 | cin.clear(); 77 | cin.ignore(65536, '\n'); 78 | f = 1; 79 | break; 80 | } 81 | } 82 | } 83 | if (f == 0) 84 | { 85 | break; 86 | } 87 | } 88 | int ans = 0; 89 | switch (n) { 90 | case 2: 91 | ans = min(a, b); 92 | break; 93 | case 3: 94 | ans = min(a, b, c); 95 | break; 96 | case 4: 97 | ans = min(a, b, c, d); 98 | break; 99 | default: 100 | break; 101 | } 102 | cout << "min=" << ans << endl; 103 | return 0; 104 | } -------------------------------------------------------------------------------- /W0901/4-b21-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /*************************************************************************** 5 | 函数名称: 6 | 功 能: 7 | 输入参数: 8 | 返 回 值: 9 | 说 明:给出fun函数的定义及实现 10 | ***************************************************************************/ 11 | template void fun(A x, B y) 12 | { 13 | cout << "参数1所占空间"; 14 | if (sizeof(A) > sizeof(B)) 15 | { 16 | cout << " > "; 17 | } 18 | else if (sizeof(A) < sizeof(B)) 19 | { 20 | cout << " < "; 21 | } 22 | else 23 | { 24 | cout << " == "; 25 | } 26 | cout << "参数2所占空间" << endl; 27 | return; 28 | } 29 | 30 | /*************************************************************************** 31 | 函数名称: 32 | 功 能: 33 | 输入参数: 34 | 返 回 值: 35 | 说 明:main函数不准修改 36 | ***************************************************************************/ 37 | int main() 38 | { 39 | short s1 = 0, s2 = 0; 40 | int i1 = 0; 41 | long l1 = 0, l2 = 0; 42 | float f1 = 0, f2 = 0; 43 | double d1 = 0, d2 = 0; 44 | 45 | fun(s1, s2); //期望输出 参数1所占空间 == 参数2所占空间 46 | fun(i1, l2); //期望输出 参数1所占空间 == 参数2所占空间 47 | fun(f1, d2); //期望输出 参数1所占空间 < 参数2所占空间 48 | fun(d1, f2); //期望输出 参数1所占空间 > 参数2所占空间 49 | fun(d2, l1); //期望输出 参数1所占空间 > 参数2所占空间 50 | 51 | return 0; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /W0901/4-b21-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /*************************************************************************** 5 | 函数名称: 6 | 功 能: 7 | 输入参数: 8 | 返 回 值: 9 | 说 明:给出fun函数的定义及实现 10 | ***************************************************************************/ 11 | template T fun(T n) 12 | { 13 | T sum = 0; 14 | for (T i = 1; i <= n; i++) 15 | { 16 | sum += i; 17 | } 18 | return sum; 19 | } 20 | 21 | /*************************************************************************** 22 | 函数名称: 23 | 功 能: 24 | 输入参数: 25 | 返 回 值: 26 | 说 明:main函数不准修改 27 | ***************************************************************************/ 28 | int main() 29 | { 30 | short s1 = 255, s2 = s1 + 1; 31 | unsigned short us1 = 255, us2 = us1 + 1; 32 | int i1 = 65535, i2 = i1 + 1; 33 | unsigned int ui1 = 65535, ui2 = ui1 + 1; 34 | 35 | cout << fun(s1) << endl; //期望输出 32640 36 | cout << fun(s2) << endl; //期望输出 -32640 37 | cout << fun(us1) << endl; //期望输出 32640 38 | cout << fun(us2) << endl; //期望输出 32896 39 | cout << fun(i1) << endl; //期望输出 2147450880 40 | cout << fun(i2) << endl; //期望输出 -2147450880 41 | cout << fun(ui1) << endl; //期望输出 2147450880 42 | cout << fun(ui2) << endl; //期望输出 2147516416 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /W0901/5-b10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | int zeller(int y, int m, int d) 7 | { 8 | if (m <= 2) 9 | { 10 | y--; 11 | m += 12; 12 | } 13 | int c = y / 100; 14 | int y2 = y % 100; 15 | int w; 16 | w = y2 + y2 / 4 + c / 4 - 2 * c + 13 * (m + 1) / 5 + d - 1; 17 | w %= 7; 18 | w += 7; 19 | return w % 7; 20 | } 21 | 22 | int main() 23 | { 24 | int day_limit_map[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; 25 | 26 | int y; 27 | while (1) { 28 | cout << "请输入年份[1900-2100]" << endl; 29 | cin >> y; 30 | if (cin.fail()) 31 | { 32 | //cout << "输入错误,请重新输入" << endl; 33 | cin.clear(); 34 | cin.sync(); 35 | cin.ignore(2147483647, '\n'); 36 | continue; 37 | } 38 | if (y < 1900 || y>2100) 39 | { 40 | //cout << "年份不正确,请重新输入" << endl; 41 | cin.clear(); 42 | cin.sync(); 43 | cin.ignore(2147483647, '\n'); 44 | continue; 45 | } 46 | break; 47 | } 48 | if ((y % 4 == 0 && y % 100 != 0) || (y % 100 == 0 && y % 400 == 0)) 49 | { 50 | day_limit_map[2]++; 51 | } 52 | cout << y << "年的日历:" << endl << endl; 53 | int days[13]; 54 | for (int i = 1; i <= 12; i++) 55 | { 56 | days[i] = zeller(y, i, 1); 57 | } 58 | 59 | for (int i = 0; i < 4; i++) 60 | { 61 | cout << resetiosflags(ios::left); 62 | cout << setiosflags(ios::right); 63 | cout << setw(13) << (i * 3 + 1) << "月"; 64 | cout << setw(30) << (i * 3 + 2) << "月"; 65 | cout << setw(30) << (i * 3 + 3) << "月" << endl; 66 | 67 | for (int j = 0; j < 3; j++) 68 | { 69 | cout << "Sun Mon Tue Wed Thu Fri Sat"; 70 | if (j != 2) 71 | { 72 | cout << " "; 73 | } 74 | } 75 | cout << endl; 76 | 77 | int month_base = i * 3 + 1; 78 | int month_start[4]; 79 | int has_output = false; 80 | month_start[1] = -days[month_base] + 1; 81 | month_start[2] = -days[month_base + 1] + 1; 82 | month_start[3] = -days[month_base + 2] + 1; 83 | int terminate_output_1 = 0; 84 | int terminate_output_2 = 0; 85 | int terminate_output_3 = 0; 86 | //cout << month_start[1] << "," << month_start[2] << "," << month_start[3] << "###" << endl; 87 | while (1) 88 | { 89 | if (terminate_output_1 & terminate_output_2 & terminate_output_3) 90 | { 91 | cout << endl; 92 | break; 93 | } 94 | has_output = 0; 95 | for (int j = 1; j <= 3; j++) 96 | { 97 | int month = i * 3 + j; 98 | int month2 = month % 3 + (month % 3 == 0) * 3; 99 | for (int k = 1; k <= 7; k++) 100 | { 101 | if (month_start[month2] <= 0 || month_start[month2] > day_limit_map[month]) 102 | { 103 | month_start[month2]++; 104 | if (month_start[month2] > day_limit_map[month]) 105 | { 106 | if (j == 1) 107 | { 108 | terminate_output_1 = 1; 109 | 110 | } 111 | if (j == 2) 112 | { 113 | terminate_output_2 = 1; 114 | 115 | } 116 | if (j == 3) 117 | { 118 | terminate_output_3 = 1; 119 | 120 | } 121 | } 122 | cout << " "; 123 | } 124 | else 125 | { 126 | cout << resetiosflags(ios::right); 127 | cout << setiosflags(ios::left); 128 | cout << setw(4) << month_start[month2]++; 129 | has_output = true; 130 | if (month_start[month2] > day_limit_map[month]) 131 | { 132 | if (j == 1) 133 | { 134 | terminate_output_1 = 1; 135 | 136 | } 137 | if (j == 2) 138 | { 139 | terminate_output_2 = 1; 140 | 141 | } 142 | if (j == 3) 143 | { 144 | terminate_output_3 = 1; 145 | 146 | } 147 | } 148 | } 149 | } 150 | if (j != 3) 151 | { 152 | cout << " "; 153 | } 154 | 155 | } 156 | cout << endl; 157 | if (!has_output) 158 | { 159 | break; 160 | } 161 | } 162 | 163 | } 164 | cout << endl; 165 | return 0; 166 | } -------------------------------------------------------------------------------- /W0901/5-b7-main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "5-b7.h" 5 | 6 | #define OFFSET_EQUIV_SIGNS_X 12 7 | #define OFFSET_EQUIV_SIGNS_Y 9 8 | 9 | #define OFFSET_SIGN_A_Y 11 10 | #define OFFSET_SIGN_B_Y 21 11 | #define OFFSET_SIGN_C_Y 31 12 | #define OFFSET_SIGN_X 13 13 | #define OFFSET_STEP_DETAIL_X 17 14 | #define OFFSET_COLUMN_TOP_X 2 15 | #define OFFSET_STOP_X 26 16 | #define INT_MAX_SELFDEF 2147483647 //避免使用climits 17 | #define MAX_LEVELS 10 18 | #define STACK_SIZE 10 19 | #define COL_COUNT 3 20 | 21 | using namespace std; 22 | 23 | 24 | static int stack_top_a = 0; 25 | static int stack_top_b = 0; 26 | static int stack_top_c = 0; 27 | static int move_counter = 0; 28 | 29 | int stack_a[STACK_SIZE]; 30 | int stack_b[STACK_SIZE]; 31 | int stack_c[STACK_SIZE]; 32 | 33 | static int delay_opt = -1; 34 | static int disp_internal_stack = -1; 35 | 36 | void transposed_cct_gotoxy(int x, int y) 37 | { 38 | cct_gotoxy(y, x); 39 | } 40 | 41 | void vertical_print_initial() 42 | { 43 | //打印等号和标签 44 | transposed_cct_gotoxy(OFFSET_EQUIV_SIGNS_X, OFFSET_EQUIV_SIGNS_Y); 45 | cout << "========================="; 46 | transposed_cct_gotoxy(OFFSET_SIGN_X, OFFSET_SIGN_A_Y); 47 | cout << "A"; 48 | transposed_cct_gotoxy(OFFSET_SIGN_X, OFFSET_SIGN_B_Y); 49 | cout << "B"; 50 | transposed_cct_gotoxy(OFFSET_SIGN_X, OFFSET_SIGN_C_Y); 51 | cout << "C"; 52 | //A柱 53 | for (int i = 0, j = OFFSET_EQUIV_SIGNS_X - 1; i < stack_top_a; i++, j--) 54 | { 55 | transposed_cct_gotoxy(j, OFFSET_SIGN_A_Y - 1); 56 | cout << setw(2) << stack_a[i]; 57 | } 58 | 59 | //B柱 60 | for (int i = 0, j = OFFSET_EQUIV_SIGNS_X - 1; i < stack_top_b; i++, j--) 61 | { 62 | transposed_cct_gotoxy(j, OFFSET_SIGN_B_Y - 1); 63 | cout << setw(2) << stack_b[i]; 64 | } 65 | //C柱 66 | for (int i = 0, j = OFFSET_EQUIV_SIGNS_X - 1; i < stack_top_c; i++, j--) 67 | { 68 | transposed_cct_gotoxy(j, OFFSET_SIGN_C_Y - 1); 69 | cout << setw(2) << stack_c[i]; 70 | } 71 | } 72 | void vertical_print_alter(char src, char dst) 73 | { 74 | //从原始柱上消除栈顶 75 | if (src == 'A') 76 | { 77 | transposed_cct_gotoxy(OFFSET_EQUIV_SIGNS_X - stack_top_a - 1, OFFSET_SIGN_A_Y - 1); 78 | cout << " "; 79 | } 80 | if (src == 'B') 81 | { 82 | transposed_cct_gotoxy(OFFSET_EQUIV_SIGNS_X - stack_top_b - 1, OFFSET_SIGN_B_Y - 1); 83 | cout << " "; 84 | } 85 | if (src == 'C') 86 | { 87 | transposed_cct_gotoxy(OFFSET_EQUIV_SIGNS_X - stack_top_c - 1, OFFSET_SIGN_C_Y - 1); 88 | cout << " "; 89 | } 90 | //添加现有栈顶 91 | if (dst == 'A') 92 | { 93 | transposed_cct_gotoxy(OFFSET_EQUIV_SIGNS_X - stack_top_a, OFFSET_SIGN_A_Y - 1); 94 | cout << setw(2) << stack_a[stack_top_a - 1]; 95 | } 96 | if (dst == 'B') 97 | { 98 | transposed_cct_gotoxy(OFFSET_EQUIV_SIGNS_X - stack_top_b, OFFSET_SIGN_B_Y - 1); 99 | cout << setw(2) << stack_b[stack_top_b - 1]; 100 | } 101 | if (dst == 'C') 102 | { 103 | transposed_cct_gotoxy(OFFSET_EQUIV_SIGNS_X - stack_top_c, OFFSET_SIGN_C_Y - 1); 104 | cout << setw(2) << stack_c[stack_top_c - 1]; 105 | } 106 | } 107 | 108 | void iterative_sleep(int deg) 109 | { 110 | for (int i = 0; i < deg; i++) 111 | { 112 | for (int j = 0; j <= 20000000; j++) 113 | { 114 | int c = 0; 115 | c++; 116 | } 117 | } 118 | for (int j = 0; j <= 6000000; j++) 119 | { 120 | int c = 0; 121 | } 122 | } 123 | 124 | void pause_enter() 125 | { 126 | if (delay_opt != 0) 127 | return; 128 | while (1) 129 | { 130 | int s = _getch(); 131 | if (s == 13) 132 | { 133 | break; 134 | } 135 | } 136 | } 137 | 138 | void result_print(int is_first, int lvl, char src, char dst) 139 | { 140 | transposed_cct_gotoxy(OFFSET_STEP_DETAIL_X, 0); 141 | if (is_first) 142 | { 143 | cout << "初始:" << " "; 144 | } 145 | else 146 | { 147 | cout << "第" << setw(4) << move_counter << "步(" << lvl << "#: " << src << "-->" << dst << ") "; 148 | } 149 | if (disp_internal_stack == 0) 150 | { 151 | cout << endl; 152 | return; 153 | } 154 | cout << " A:"; 155 | for (int i = 0; i < stack_top_a; i++) 156 | { 157 | cout << setw(2) << stack_a[i]; 158 | } 159 | for (int i = stack_top_a; i < 10; i++) 160 | { 161 | cout << " "; 162 | } 163 | cout << " B:"; 164 | for (int i = 0; i < stack_top_b; i++) 165 | { 166 | cout << setw(2) << stack_b[i]; 167 | } 168 | for (int i = stack_top_b; i < 10; i++) 169 | { 170 | cout << " "; 171 | } 172 | cout << " C:"; 173 | for (int i = 0; i < stack_top_c; i++) 174 | { 175 | cout << setw(2) << stack_c[i]; 176 | } 177 | for (int i = stack_top_c; i < 10; i++) 178 | { 179 | cout << " "; 180 | } 181 | cout << endl; 182 | } 183 | /*************************************************************************** 184 | 函数名称: 185 | 功 能:打印n层汉诺塔的移动顺序 186 | 输入参数:int n:层数 187 | char src:起始柱 188 | char tmp:中间柱 189 | char dst:目标柱 190 | 返 回 值: 191 | 说 明:1、函数名、形参、返回类型均不准动 192 | 2、本函数不允许出现任何形式的循环 193 | ***************************************************************************/ 194 | void hanoi(int n, char src, char tmp, char dst) 195 | { 196 | if (n != 1) 197 | { 198 | hanoi(n - 1, src, dst, tmp); 199 | } 200 | 201 | move_counter++; 202 | int moved = 0; 203 | if (src == 'A') 204 | { 205 | moved = stack_a[--stack_top_a]; 206 | } 207 | if (src == 'B') 208 | { 209 | moved = stack_b[--stack_top_b]; 210 | } 211 | if (src == 'C') 212 | { 213 | moved = stack_c[--stack_top_c]; 214 | } 215 | if (dst == 'A') 216 | { 217 | stack_a[stack_top_a++] = moved; 218 | } 219 | if (dst == 'B') 220 | { 221 | stack_b[stack_top_b++] = moved; 222 | } 223 | if (dst == 'C') 224 | { 225 | stack_c[stack_top_c++] = moved; 226 | } 227 | 228 | if (delay_opt <= 5 && delay_opt >= 1) 229 | { 230 | iterative_sleep((5 - delay_opt) * (5 - delay_opt)); 231 | } 232 | 233 | result_print(0, n, src, dst); 234 | pause_enter(); 235 | vertical_print_alter(src, dst); 236 | pause_enter(); 237 | 238 | if (n != 1) 239 | { 240 | hanoi(n - 1, tmp, src, dst); 241 | } 242 | 243 | } 244 | 245 | /*************************************************************************** 246 | 函数名称: 247 | 功 能: 248 | 输入参数: 249 | 返 回 值: 250 | 说 明:完成输入(或调用输入函数)、调用递归函数 251 | ***************************************************************************/ 252 | int main() 253 | { 254 | int levels = 0; 255 | char start = 'D', dest = 'D'; 256 | char tmp = 'D'; 257 | int cinflag; 258 | while (1) 259 | { 260 | cout << "请输入汉诺塔的层数(1-10)" << endl; 261 | cin >> levels; 262 | cinflag = cin.fail(); 263 | cin.clear(); 264 | cin.ignore(INT_MAX_SELFDEF, '\n'); 265 | if (cinflag || levels <= 0 || levels > 10) 266 | { 267 | continue; 268 | } 269 | break; 270 | } 271 | while (1) 272 | { 273 | cout << "请输入起始柱(A-C)" << endl; 274 | cin >> start; 275 | cinflag = cin.fail(); 276 | cin.clear(); 277 | cin.ignore(INT_MAX_SELFDEF, '\n'); 278 | if (start >= 'a' && start <= 'c') 279 | { 280 | start = start - 'a' + 'A'; 281 | } 282 | if (cinflag || start < 'A' || start>'C') 283 | { 284 | continue; 285 | } 286 | break; 287 | } 288 | if (start == 'A') 289 | { 290 | for (int i = levels; i >= 1; i--) 291 | { 292 | stack_a[stack_top_a++] = i; 293 | } 294 | } 295 | if (start == 'B') 296 | { 297 | for (int i = levels; i >= 1; i--) 298 | { 299 | stack_b[stack_top_b++] = i; 300 | } 301 | } 302 | if (start == 'C') 303 | { 304 | for (int i = levels; i >= 1; i--) 305 | { 306 | stack_c[stack_top_c++] = i; 307 | } 308 | } 309 | while (1) 310 | { 311 | cout << "请输入目标柱(A-C)" << endl; 312 | cin >> dest; 313 | cinflag = cin.fail(); 314 | cin.clear(); 315 | cin.ignore(INT_MAX_SELFDEF, '\n'); 316 | if (dest >= 'a' && dest <= 'c') 317 | { 318 | dest = dest - 'a' + 'A'; 319 | } 320 | if (cinflag || dest < 'A' || dest>'C') 321 | { 322 | continue; 323 | } 324 | if (dest == start) 325 | { 326 | cout << "目标柱(" << dest << ")不能与起始柱(" << start << ")相同" << endl; 327 | continue; 328 | } 329 | break; 330 | } 331 | if (static_cast(start) + static_cast(dest) == static_cast('A') + static_cast('C')) 332 | { 333 | tmp = 'B'; 334 | } 335 | else if (static_cast(start) + static_cast(dest) == static_cast('B') + static_cast('C')) 336 | { 337 | tmp = 'A'; 338 | } 339 | else if (static_cast(start) + static_cast(dest) == static_cast('A') + static_cast('B')) 340 | { 341 | tmp = 'C'; 342 | } 343 | 344 | while (1) 345 | { 346 | delay_opt = 9999; 347 | cout << "请输入移动速度(0-5: 0-按回车单步演示 1-延时最长 5-延时最短)" << endl; 348 | cin >> delay_opt; 349 | cinflag = cin.fail(); 350 | if (cinflag) 351 | { 352 | cin.clear(); 353 | cin.ignore(INT_MAX_SELFDEF, '\n'); 354 | } 355 | if (cinflag || delay_opt < 0 || delay_opt > 5) 356 | { 357 | continue; 358 | } 359 | break; 360 | } 361 | while (1) 362 | { 363 | disp_internal_stack = 9999; 364 | cout << "请输入是否显示内部数组值(0-不显示 1-显示)" << endl; 365 | cin >> disp_internal_stack; 366 | cinflag = cin.fail(); 367 | if (cinflag) 368 | { 369 | cin.clear(); 370 | cin.ignore(INT_MAX_SELFDEF, '\n'); 371 | } 372 | if (cinflag || disp_internal_stack < 0 || disp_internal_stack > 1) 373 | { 374 | continue; 375 | } 376 | break; 377 | } 378 | //处理输出 379 | cct_cls(); 380 | transposed_cct_gotoxy(0, 0); 381 | if (disp_internal_stack) 382 | { 383 | cout << "从 " << start << " 移动到 " << dest << ",共 " << levels << " 层,延时设置为 " << delay_opt << ",显示内部数组值" << endl; 384 | } 385 | else 386 | { 387 | cout << "从 " << start << " 移动到 " << dest << ",共 " << levels << " 层,延时设置为 " << delay_opt << ",不显示内部数组值" << endl; 388 | } 389 | if (disp_internal_stack == 1) 390 | { 391 | result_print(1, 0, 0, 0); 392 | pause_enter(); 393 | } 394 | vertical_print_initial(); 395 | pause_enter(); 396 | hanoi(levels, start, tmp, dest); 397 | 398 | //终止光标定位 399 | transposed_cct_gotoxy(OFFSET_STOP_X, 0); 400 | return 0; 401 | } 402 | -------------------------------------------------------------------------------- /W0901/5-b7.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | void cct_cls(); 4 | void cct_gotoxy(const int X, const int Y); 5 | -------------------------------------------------------------------------------- /W0901/5-b8.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | double vector_product(double x1, double y1, double x2, double y2) 6 | { 7 | return x1 * y2 - x2 * y1; 8 | } 9 | 10 | int main() 11 | { 12 | const double EPS = 1e-9; 13 | const int ARRAY_SIZE = 30; 14 | int n; 15 | double x[ARRAY_SIZE], y[ARRAY_SIZE]; 16 | double prod[ARRAY_SIZE]; 17 | x[1] = 0; 18 | y[1] = 0; 19 | while (1) 20 | { 21 | n = -1; 22 | cout << "请输入多边形的顶点数量(4-7)" << endl; 23 | cin >> n; 24 | if (cin.fail()) 25 | { 26 | cin.clear(); 27 | cin.ignore(65536, '\n'); 28 | continue; 29 | } 30 | if (n > 7 || n < 4) 31 | { 32 | continue; 33 | } 34 | break; 35 | } 36 | double test = 2498230480; 37 | 38 | cout << "请按顺时针/逆时针方向输入" << n << "个顶点的x,y坐标:" << endl; 39 | for (int i = 1; i <= n; i++) 40 | { 41 | while (1) 42 | { 43 | cout << "请输入第" << i << "个顶点的坐标:" << endl; 44 | cin >> x[i] >> y[i]; 45 | if (cin.fail()) 46 | { 47 | cin.clear(); 48 | cin.ignore(65536, '\n'); 49 | continue; 50 | } 51 | break; 52 | } 53 | } 54 | int valid_flag = true; 55 | //判断共线 56 | /* 57 | for (int i = 1; i <= n; i++) 58 | { 59 | for (int j = 1; j <= n; j++) 60 | { 61 | for (int k = 1; k <= n; k++) 62 | { 63 | if (i == k || j == k || i == j) 64 | { 65 | continue; 66 | } 67 | double slope1, slope2; 68 | slope1 = (y[k] - y[i]) / (x[k] - x[i]); 69 | slope2 = (y[j] - y[i]) / (x[j] - x[i]); 70 | if (fabs(slope2 - slope1) <= EPS) 71 | { 72 | valid_flag = false; 73 | } 74 | } 75 | } 76 | }*/ 77 | 78 | //凹凸性判断 79 | int seq_order = true, rev_order = true; 80 | for (int i = 1; i < n-1; i++) 81 | { 82 | prod[i] = vector_product(x[i+1]-x[i], y[i+1]-y[i], x[i + 2] - x[i], y[i + 2] - y[i]); 83 | } 84 | if (n - 1 > 0) 85 | { 86 | prod[n - 1] = vector_product(x[n] - x[n - 1], y[n] - y[n - 1], x[1] - x[n - 1], y[1] - y[n - 1]); 87 | 88 | } 89 | prod[n] = vector_product(x[1] - x[n], y[1] - y[n], x[2] - x[n], y[2] - y[n]); 90 | 91 | for (int i = 1; i <= n; i++) 92 | { 93 | if (prod[i] < 0.0) 94 | { 95 | seq_order = false; 96 | } 97 | if (prod[i] > 0.0) 98 | { 99 | rev_order = false; 100 | } 101 | if (fabs(prod[i]) <= EPS) 102 | { 103 | valid_flag = false; 104 | } 105 | } 106 | if (seq_order == false && rev_order == false) 107 | { 108 | valid_flag = false; 109 | } 110 | 111 | if (valid_flag) 112 | { 113 | double ans = 0; 114 | for (int i = 1; i < n; i++) 115 | { 116 | ans += vector_product(x[i], y[i], x[i + 1], y[i + 1]); 117 | } 118 | ans += vector_product(x[n], y[n], x[1], y[1]); 119 | ans = fabs(ans) * 0.5; 120 | cout << "凸" << n << "边形的面积=" << ans << endl; 121 | } 122 | else 123 | { 124 | cout << "不是凸" << n << "边形" << endl; 125 | } 126 | return 0; 127 | } 128 | -------------------------------------------------------------------------------- /W0901/5-b9.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | const int MATRIX_SIZE = 9; 7 | const int HOUSE_SIZE = 3; 8 | int matrix[MATRIX_SIZE][MATRIX_SIZE]; 9 | int counter[MATRIX_SIZE + 1]; 10 | for (int i = 0; i <= MATRIX_SIZE; i++) 11 | { 12 | counter[i] = 0; 13 | } 14 | cout << "请输入9*9的矩阵,值为1-9之间" << endl; 15 | for (int i = 0; i < MATRIX_SIZE; i++) 16 | { 17 | for (int j = 0; j < MATRIX_SIZE; j++) 18 | { 19 | while (1) 20 | { 21 | matrix[i][j] = 1000; 22 | cin >> matrix[i][j]; 23 | if (cin.fail()) 24 | { 25 | cout << "请重新输入第" << i + 1 << "行" << j + 1 << "列(行列均从1开始计数)的值" << endl; 26 | cin.clear(); 27 | cin.ignore(65536, '\n'); 28 | continue; 29 | } 30 | if (matrix[i][j] <= 0 || matrix[i][j] > MATRIX_SIZE) 31 | { 32 | cout << "请重新输入第" << i + 1 << "行" << j + 1 << "列(行列均从1开始计数)的值" << endl; 33 | //cin.clear(); 34 | //cin.ignore(65536, '\n'); 35 | continue; 36 | } 37 | break; 38 | } 39 | 40 | } 41 | } 42 | int valid_flag = true; 43 | 44 | //按行统计 45 | for (int i = 0; i < MATRIX_SIZE; i++) 46 | { 47 | for (int j = 0; j < MATRIX_SIZE; j++) 48 | { 49 | counter[matrix [i][j]] ++; 50 | } 51 | int standard = counter[1]; 52 | for (int j = 1; j <= MATRIX_SIZE; j++) 53 | { 54 | if (counter[j] != standard) 55 | { 56 | valid_flag = false; 57 | } 58 | } 59 | } 60 | 61 | //按列统计 62 | for (int i = 0; i < MATRIX_SIZE; i++) 63 | { 64 | for (int j = 0; j < MATRIX_SIZE; j++) 65 | { 66 | counter[matrix[j][i]] ++; 67 | } 68 | int standard = counter[1]; 69 | for (int j = 1; j <= MATRIX_SIZE; j++) 70 | { 71 | if (counter[j] != standard) 72 | { 73 | valid_flag = false; 74 | } 75 | } 76 | } 77 | //宫格 78 | for (int i = 0; i < MATRIX_SIZE / HOUSE_SIZE; i++) 79 | { 80 | for (int j = 0; j < MATRIX_SIZE / HOUSE_SIZE; j++) 81 | { 82 | for (int k = 0; k < HOUSE_SIZE; k++) 83 | { 84 | for (int m = 0; m < HOUSE_SIZE; m++) 85 | { 86 | int row = i * HOUSE_SIZE + k; 87 | int col = j * HOUSE_SIZE + m; 88 | counter[matrix[row][col]]++; 89 | } 90 | } 91 | int standard = counter[1]; 92 | for (int k = 1; k <= MATRIX_SIZE; k++) 93 | { 94 | if (counter[k] != standard) 95 | { 96 | valid_flag = false; 97 | } 98 | } 99 | } 100 | } 101 | //输出结果 102 | if (valid_flag) 103 | { 104 | cout << "是数独的解" << endl; 105 | } 106 | else 107 | { 108 | cout << "不是数独的解" << endl; 109 | } 110 | return 0; 111 | } 112 | -------------------------------------------------------------------------------- /W1001/5-b11-1.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | //可按需增加需要的头文件 5 | #include //群内说添加该头文件 6 | 7 | const char chistr[] = "零壹贰叁肆伍陆柒捌玖"; /* 所有输出大写 "零" ~ "玖" 的地方,只允许从这个数组中取值 */ 8 | char result[256]; /* 除result外,不再允许定义任何形式的全局变量 */ 9 | 10 | /* --允许添加需要的函数 --*/ 11 | 12 | void daxie(int num, int flag_of_zero) 13 | { 14 | char tmp[10]; 15 | tmp[0] = chistr[num * 2]; 16 | tmp[1] = chistr[num * 2 + 1]; 17 | tmp[2] = '\0'; 18 | strcat(result, tmp); 19 | } 20 | 21 | /* 可根据需要自定义其它函数(也可以不定义) */ 22 | int convert_main() 23 | { 24 | double a; 25 | double b; 26 | //小数数位 27 | int d_dec1, d_dec2; 28 | //整数数位 29 | int d1, d2, d3, d4; 30 | int d5, d6, d7, d8; 31 | int d9, d10; 32 | //标记 33 | int tag1 = 0, tag2 = 0, tag3 = 0; 34 | int tag4 = 0; 35 | char c; 36 | while (1) 37 | { 38 | printf("请输入[0-100亿)之间的数字: \n"); 39 | int x = scanf("%lf", &a); 40 | if (x == 0) 41 | { 42 | while ((c = getchar()) != '\n' && c != -1) 43 | ; 44 | continue; 45 | } 46 | if (a < 0.0 || a>1e10) 47 | { 48 | continue; 49 | } 50 | break; 51 | } 52 | 53 | b = fmod(a, 1); 54 | d_dec2 = (int)(b * 100.0 + 0.5) % 10; 55 | d_dec1 = (int)(round(1e1 * fmod(b * 1e+1, 1e+1)) / 1e1); 56 | d1 = (int)((fmod(a, 1e+1) - fmod(a, 1e+0)) / 1e+0); 57 | d2 = (int)((fmod(a, 1e+2) - fmod(a, 1e+1)) / 1e+1); 58 | d3 = (int)((fmod(a, 1e+3) - fmod(a, 1e+2)) / 1e+2); 59 | d4 = (int)((fmod(a, 1e+4) - fmod(a, 1e+3)) / 1e+3); 60 | d5 = (int)((fmod(a, 1e+5) - fmod(a, 1e+4)) / 1e+4); 61 | d6 = (int)((fmod(a, 1e+6) - fmod(a, 1e+5)) / 1e+5); 62 | d7 = (int)((fmod(a, 1e+7) - fmod(a, 1e+6)) / 1e+6); 63 | d8 = (int)((fmod(a, 1e+8) - fmod(a, 1e+7)) / 1e+7); 64 | d9 = (int)((fmod(a, 1e+9) - fmod(a, 1e+8)) / 1e+8); 65 | d10 = (int)((fmod(a, 1e+10) - fmod(a, 1e+9)) / 1e+9); 66 | 67 | printf("大写结果是:\n"); 68 | //亿位 69 | if (d10 != 0 || d9 != 0) 70 | { 71 | tag1 = 1; 72 | if (d10 > 0) 73 | { 74 | daxie(d10, 0); 75 | strcat(result, "拾"); 76 | } 77 | if (d9 > 0) 78 | { 79 | daxie(d9, 0); 80 | } 81 | strcat(result, "亿"); 82 | } 83 | //万位 84 | if (d5 != 0 || d6 != 0 || d7 != 0 || d8 != 0) 85 | { 86 | tag2 = 1; 87 | if (d8 > 0) 88 | { 89 | daxie(d8, 0); 90 | strcat(result, "仟"); 91 | } 92 | else if ((d7 > 0 || d6 > 0 || d5 > 0) && tag1 == 1) 93 | { 94 | daxie(0, 1); 95 | } 96 | if (d7 > 0) 97 | { 98 | daxie(d7, 0); 99 | strcat(result, "佰"); 100 | } 101 | else if (d8 > 0 && (d6 > 0 || d5 > 0)) 102 | { 103 | daxie(0, 1); 104 | } 105 | 106 | if (d6 > 0) 107 | { 108 | daxie(d6, 0); 109 | strcat(result, "拾"); 110 | } 111 | else if (d7 > 0 && d5 > 0) 112 | { 113 | daxie(0, 1); 114 | } 115 | if (d5 > 0) 116 | { 117 | daxie(d5, 0); 118 | } 119 | strcat(result, "万"); 120 | } 121 | 122 | //个位 123 | if (d1 != 0 || d2 != 0 || d3 != 0 || d4 != 0) 124 | { 125 | tag3 = 1; 126 | if (d4 > 0) 127 | { 128 | daxie(d4, 0); 129 | strcat(result, "仟"); 130 | } 131 | else if ((d3 > 0 || d2 > 0 || d1 > 0) && (tag2 == 1 || tag1 == 1)) 132 | { 133 | daxie(0, 1); 134 | } 135 | if (d3 > 0) 136 | { 137 | daxie(d3, 0); 138 | strcat(result, "佰"); 139 | } 140 | else if (d4 > 0 && (d2 > 0 || d1 > 0)) 141 | { 142 | daxie(0, 1); 143 | } 144 | 145 | if (d2 > 0) 146 | { 147 | daxie(d2, 0); 148 | strcat(result, "拾"); 149 | } 150 | else if (d3 > 0 && d1 > 0) 151 | { 152 | daxie(0, 1); 153 | } 154 | if (d1 > 0) 155 | { 156 | daxie(d1, 0); 157 | } 158 | 159 | } 160 | if (tag1 == 0 && tag2 == 0 && tag3 == 0 && d_dec1 == 0 && d_dec2 == 0) 161 | { 162 | daxie(0, 1); 163 | strcat(result, "圆"); 164 | } 165 | if (tag1 != 0 || tag2 != 0 || tag3 != 0) 166 | { 167 | strcat(result, "圆"); 168 | tag4 = 1; 169 | } 170 | //小数 171 | if (d_dec1 == 0 && d_dec2 == 0) 172 | { 173 | strcat(result, "整"); 174 | } 175 | else 176 | { 177 | if (d_dec1 != 0) 178 | { 179 | daxie(d_dec1, 0); 180 | strcat(result, "角"); 181 | if (d_dec2 == 0) 182 | { 183 | strcat(result, "整"); 184 | } 185 | } 186 | else if (tag4 == 1 && d_dec2 != 0) 187 | { 188 | daxie(0, 1); 189 | } 190 | if (d_dec2 != 0) 191 | { 192 | daxie(d_dec2, 0); 193 | strcat(result, "分"); 194 | } 195 | } 196 | //行末换行 197 | //strcat(result, "\n"); 198 | return 0; 199 | } 200 | 201 | 202 | /*************************************************************************** 203 | 函数名称: 204 | 功 能: 205 | 输入参数: 206 | 返 回 值: 207 | 说 明: 208 | ***************************************************************************/ 209 | int main() 210 | { 211 | /* --允许添加需要的内容 --*/ 212 | convert_main(); 213 | 214 | printf("%s\n", result); /* 转换得到的大写结果,只允许用本语句输出,之前不允许任何形式的部分输出 */ 215 | return 0; 216 | } 217 | -------------------------------------------------------------------------------- /W1001/5-b11-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | //可按需增加需要的头文件 5 | using namespace std; 6 | 7 | const char chistr[] = "零壹贰叁肆伍陆柒捌玖"; /* 所有输出大写 "零" ~ "玖" 的地方,只允许从这个数组中取值 */ 8 | string result; /* 除result外,不再允许定义任何形式的全局变量 */ 9 | 10 | /* --允许添加需要的函数 --*/ 11 | 12 | 13 | void daxie(int num, int flag_of_zero) 14 | { 15 | char tmp[10]; 16 | tmp[0] = chistr[num * 2]; 17 | tmp[1] = chistr[num * 2 + 1]; 18 | tmp[2] = '\0'; 19 | result = result + tmp; 20 | } 21 | 22 | /* 可根据需要自定义其它函数(也可以不定义) */ 23 | int convert_main() 24 | { 25 | double a; 26 | double b; 27 | //小数数位 28 | int d_dec1, d_dec2; 29 | //整数数位 30 | int d1, d2, d3, d4; 31 | int d5, d6, d7, d8; 32 | int d9, d10; 33 | //标记 34 | int tag1 = 0, tag2 = 0, tag3 = 0; 35 | int tag4 = 0; 36 | while (1) 37 | { 38 | cout << "请输入[0-100亿)之间的数字: " << endl; 39 | cin >> a; 40 | if (cin.fail()) 41 | { 42 | cin.clear(); 43 | cin.ignore(65536, '\n'); 44 | continue; 45 | } 46 | if (a < 0.0 || a>1e10) 47 | { 48 | continue; 49 | } 50 | break; 51 | } 52 | 53 | b = fmod(a, 1); 54 | d_dec2 = (int)(b * 100.0 + 0.5) % 10; 55 | d_dec1 = (int)(round(1e1 * fmod(b * 1e+1, 1e+1)) / 1e1); 56 | d1 = (int)((fmod(a, 1e+1) - fmod(a, 1e+0)) / 1e+0); 57 | d2 = (int)((fmod(a, 1e+2) - fmod(a, 1e+1)) / 1e+1); 58 | d3 = (int)((fmod(a, 1e+3) - fmod(a, 1e+2)) / 1e+2); 59 | d4 = (int)((fmod(a, 1e+4) - fmod(a, 1e+3)) / 1e+3); 60 | d5 = (int)((fmod(a, 1e+5) - fmod(a, 1e+4)) / 1e+4); 61 | d6 = (int)((fmod(a, 1e+6) - fmod(a, 1e+5)) / 1e+5); 62 | d7 = (int)((fmod(a, 1e+7) - fmod(a, 1e+6)) / 1e+6); 63 | d8 = (int)((fmod(a, 1e+8) - fmod(a, 1e+7)) / 1e+7); 64 | d9 = (int)((fmod(a, 1e+9) - fmod(a, 1e+8)) / 1e+8); 65 | d10 = (int)((fmod(a, 1e+10) - fmod(a, 1e+9)) / 1e+9); 66 | 67 | cout << "大写结果是:" << endl; 68 | //亿位 69 | if (d10 != 0 || d9 != 0) 70 | { 71 | tag1 = 1; 72 | if (d10 > 0) 73 | { 74 | daxie(d10, 0); 75 | result = result+ "拾"; 76 | } 77 | if (d9 > 0) 78 | { 79 | daxie(d9, 0); 80 | } 81 | result = result+ "亿"; 82 | } 83 | //万位 84 | if (d5 != 0 || d6 != 0 || d7 != 0 || d8 != 0) 85 | { 86 | tag2 = 1; 87 | if (d8 > 0) 88 | { 89 | daxie(d8, 0); 90 | result = result+ "仟"; 91 | } 92 | else if ((d7 > 0 || d6 > 0 || d5 > 0) && tag1 == 1) 93 | { 94 | daxie(0, 1); 95 | } 96 | if (d7 > 0) 97 | { 98 | daxie(d7, 0); 99 | result = result+ "佰"; 100 | } 101 | else if (d8 > 0 && (d6 > 0 || d5 > 0)) 102 | { 103 | daxie(0, 1); 104 | } 105 | 106 | if (d6 > 0) 107 | { 108 | daxie(d6, 0); 109 | result = result+ "拾"; 110 | } 111 | else if (d7 > 0 && d5 > 0) 112 | { 113 | daxie(0, 1); 114 | } 115 | if (d5 > 0) 116 | { 117 | daxie(d5, 0); 118 | } 119 | result = result+ "万"; 120 | } 121 | 122 | //个位 123 | if (d1 != 0 || d2 != 0 || d3 != 0 || d4 != 0) 124 | { 125 | tag3 = 1; 126 | if (d4 > 0) 127 | { 128 | daxie(d4, 0); 129 | result = result+ "仟"; 130 | } 131 | else if ((d3 > 0 || d2 > 0 || d1 > 0) && (tag2 == 1 || tag1 == 1)) 132 | { 133 | daxie(0, 1); 134 | } 135 | if (d3 > 0) 136 | { 137 | daxie(d3, 0); 138 | result = result+ "佰"; 139 | } 140 | else if (d4 > 0 && (d2 > 0 || d1 > 0)) 141 | { 142 | daxie(0, 1); 143 | } 144 | 145 | if (d2 > 0) 146 | { 147 | daxie(d2, 0); 148 | result = result+ "拾"; 149 | } 150 | else if (d3 > 0 && d1 > 0) 151 | { 152 | daxie(0, 1); 153 | } 154 | if (d1 > 0) 155 | { 156 | daxie(d1, 0); 157 | } 158 | 159 | } 160 | if (tag1 == 0 && tag2 == 0 && tag3 == 0 && d_dec1 == 0 && d_dec2 == 0) 161 | { 162 | daxie(0, 1); 163 | result = result+ "圆"; 164 | } 165 | if (tag1 != 0 || tag2 != 0 || tag3 != 0) 166 | { 167 | result = result+ "圆"; 168 | tag4 = 1; 169 | } 170 | //小数 171 | if (d_dec1 == 0 && d_dec2 == 0) 172 | { 173 | result = result+ "整"; 174 | } 175 | else 176 | { 177 | if (d_dec1 != 0) 178 | { 179 | daxie(d_dec1, 0); 180 | result = result+ "角"; 181 | if (d_dec2 == 0) 182 | { 183 | result = result+ "整"; 184 | } 185 | } 186 | else if (tag4 == 1 && d_dec2 != 0) 187 | { 188 | daxie(0, 1); 189 | } 190 | if (d_dec2 != 0) 191 | { 192 | daxie(d_dec2, 0); 193 | result = result+ "分"; 194 | } 195 | } 196 | 197 | return 0; 198 | } 199 | 200 | 201 | /*************************************************************************** 202 | 函数名称: 203 | 功 能: 204 | 输入参数: 205 | 返 回 值: 206 | 说 明: 207 | ***************************************************************************/ 208 | int main() 209 | { 210 | /* --允许添加需要的内容 --*/ 211 | 212 | convert_main(); 213 | cout << result << endl; /* 转换得到的大写结果,只允许用本语句输出,之前不允许任何形式的部分输出 */ 214 | return 0; 215 | } 216 | -------------------------------------------------------------------------------- /W1001/5-b12-sub.cpp: -------------------------------------------------------------------------------- 1 | int tj_strlen(const char str[]) 2 | { 3 | int i = 0; 4 | while (str[i] != '\0') 5 | { 6 | i++; 7 | } 8 | return i; 9 | } 10 | int tj_strcat(char s1[], const char s2[]) 11 | { 12 | int i = 0, j=0; 13 | while (s1[i] != '\0') 14 | { 15 | i++; 16 | } 17 | while (1) 18 | { 19 | s1[i++] = s2[j]; 20 | if (s2[j++] == '\0') 21 | { 22 | break; 23 | } 24 | } 25 | return 0; 26 | } 27 | int tj_strncat(char s1[], const char s2[], const int len) 28 | { 29 | int i = 0, j = 0; 30 | while (s1[i] != '\0') 31 | { 32 | i++; 33 | } 34 | while (s2[j]!='\0') 35 | { 36 | s1[i++] = s2[j++]; 37 | if (j == len) 38 | { 39 | break; 40 | } 41 | } 42 | s1[i] = '\0'; 43 | return 0; 44 | } 45 | int tj_strcpy(char s1[], const char s2[]) 46 | { 47 | int i = 0; 48 | while (s2[i] != '\0') 49 | { 50 | s1[i] = s2[i++]; 51 | } 52 | s1[i] = '\0'; 53 | return 0; 54 | } 55 | 56 | int tj_strncpy(char s1[], const char s2[], const int len) 57 | { 58 | int i = 0; 59 | while (s2[i] != '\0') 60 | { 61 | s1[i] = s2[i++]; 62 | if (i >= len) 63 | { 64 | break; 65 | } 66 | } 67 | return 0; 68 | } 69 | int tj_strcmp(const char s1[], const char s2[]) 70 | { 71 | int s1_valid = 1; 72 | int s2_valid = 1; 73 | int s1_ascii, s2_ascii; 74 | int i = 0; 75 | int ans = 0; 76 | while (1) 77 | { 78 | if (s1[i] == '\0') 79 | { 80 | s1_valid = 0; 81 | } 82 | if (s1_valid == 0) 83 | { 84 | s1_ascii = 0; 85 | } 86 | else 87 | { 88 | s1_ascii = static_cast(s1[i]); 89 | } 90 | 91 | if (s2[i] == '\0') 92 | { 93 | s2_valid = 0; 94 | } 95 | if (s2_valid == 0) 96 | { 97 | s2_ascii = 0; 98 | } 99 | else 100 | { 101 | s2_ascii = static_cast(s2[i]); 102 | } 103 | ans = s1_ascii - s2_ascii; 104 | if (ans != 0) 105 | { 106 | return ans; 107 | } 108 | else if (s2_valid == 0 && s1_valid == 0) 109 | { 110 | return 0; 111 | } 112 | i++; 113 | } 114 | return 0; 115 | } 116 | 117 | int tj_strcasecmp(const char s1[], const char s2[]) 118 | { 119 | int s1_valid = 1; 120 | int s2_valid = 1; 121 | int s1_ascii, s2_ascii; 122 | int i = 0; 123 | int ans = 0; 124 | while (1) 125 | { 126 | if (s1[i] == '\0') 127 | { 128 | s1_valid = 0; 129 | } 130 | if (s1_valid == 0) 131 | { 132 | s1_ascii = 0; 133 | } 134 | else 135 | { 136 | s1_ascii = static_cast(s1[i]); 137 | if (s1_ascii >= static_cast('A') && s1_ascii <= static_cast('Z')) 138 | { 139 | s1_ascii = s1_ascii - static_cast('A') + static_cast('a'); 140 | } 141 | } 142 | 143 | if (s2[i] == '\0') 144 | { 145 | s2_valid = 0; 146 | } 147 | if (s2_valid == 0) 148 | { 149 | s2_ascii = 0; 150 | } 151 | else 152 | { 153 | s2_ascii = static_cast(s2[i]); 154 | if (s2_ascii >= static_cast('A') && s2_ascii <= static_cast('Z')) 155 | { 156 | s2_ascii = s2_ascii - static_cast('A') + static_cast('a'); 157 | } 158 | } 159 | ans = s1_ascii - s2_ascii; 160 | if (ans != 0) 161 | { 162 | return ans; 163 | } 164 | else if (s2_valid == 0 && s1_valid == 0) 165 | { 166 | return 0; 167 | } 168 | i++; 169 | } 170 | return 0; 171 | } 172 | int tj_strncmp(const char s1[], const char s2[], const int len) 173 | { 174 | int s1_valid = 1; 175 | int s2_valid = 1; 176 | int s1_ascii, s2_ascii; 177 | int i = 0; 178 | int ans = 0; 179 | while (1) 180 | { 181 | if (s1[i] == '\0') 182 | { 183 | s1_valid = 0; 184 | } 185 | if (s1_valid == 0) 186 | { 187 | s1_ascii = 0; 188 | } 189 | else 190 | { 191 | s1_ascii = static_cast(s1[i]); 192 | } 193 | 194 | if (s2[i] == '\0') 195 | { 196 | s2_valid = 0; 197 | } 198 | if (s2_valid == 0) 199 | { 200 | s2_ascii = 0; 201 | } 202 | else 203 | { 204 | s2_ascii = static_cast(s2[i]); 205 | } 206 | ans = s1_ascii - s2_ascii; 207 | if (ans != 0) 208 | { 209 | return ans; 210 | } 211 | else if (s2_valid == 0 || s1_valid == 0) 212 | { 213 | return 0; 214 | } 215 | i++; 216 | if (i >= len) 217 | { 218 | return 0; 219 | } 220 | } 221 | return 0; 222 | } 223 | 224 | int tj_strcasencmp(const char s1[], const char s2[], const int len) 225 | { 226 | int s1_valid = 1; 227 | int s2_valid = 1; 228 | int s1_ascii, s2_ascii; 229 | int i = 0; 230 | int ans = 0; 231 | while (1) 232 | { 233 | if (s1[i] == '\0') 234 | { 235 | s1_valid = 0; 236 | } 237 | if (s1_valid == 0) 238 | { 239 | s1_ascii = 0; 240 | } 241 | else 242 | { 243 | s1_ascii = static_cast(s1[i]); 244 | if (s1_ascii >= static_cast('A') && s1_ascii <= static_cast('Z')) 245 | { 246 | s1_ascii = s1_ascii - static_cast('A') + static_cast('a'); 247 | } 248 | } 249 | 250 | if (s2[i] == '\0') 251 | { 252 | s2_valid = 0; 253 | } 254 | if (s2_valid == 0) 255 | { 256 | s2_ascii = 0; 257 | } 258 | else 259 | { 260 | s2_ascii = static_cast(s2[i]); 261 | if (s2_ascii >= static_cast('A') && s2_ascii <= static_cast('Z')) 262 | { 263 | s2_ascii = s2_ascii - static_cast('A') + static_cast('a'); 264 | } 265 | } 266 | ans = s1_ascii - s2_ascii; 267 | if (ans != 0) 268 | { 269 | return ans; 270 | } 271 | else if (s2_valid == 0 || s1_valid == 0) 272 | { 273 | return 0; 274 | } 275 | i++; 276 | if (i >= len) 277 | { 278 | return 0; 279 | } 280 | } 281 | return 0; 282 | } 283 | 284 | int tj_strupr(char str[]) 285 | { 286 | int i = 0; 287 | while (str[i] != '\0') 288 | { 289 | if (str[i] >= 'a' && str[i] <= 'z') 290 | { 291 | str[i] = str[i] - 'a' + 'A'; 292 | } 293 | i++; 294 | } 295 | return 0; 296 | } 297 | int tj_strlwr(char str[]) 298 | { 299 | int i = 0; 300 | while (str[i] != '\0') 301 | { 302 | if (str[i] >= 'A' && str[i] <= 'Z') 303 | { 304 | str[i] = str[i] - 'A' + 'a'; 305 | } 306 | i++; 307 | } 308 | return 0; 309 | } 310 | int tj_strchr(const char str[], const char ch) 311 | { 312 | int i = 0; 313 | //Because the maximal output is n, according to the instruction 314 | // '\0' will not be counted 315 | while (str[i] != '\0') 316 | { 317 | if (str[i] == ch) 318 | { 319 | return i + 1; 320 | } 321 | i++; 322 | } 323 | return 0; 324 | } 325 | int tj_strstr(const char str[], const char substr[]) 326 | { 327 | int str_len = tj_strlen(str); 328 | int sub_len = tj_strlen(substr); 329 | for (int i = 0; i str_len) 332 | { 333 | break; 334 | } 335 | int match = true; 336 | for (int j = 0; j < sub_len; j++) 337 | { 338 | if (str[i + j] != substr[j]) 339 | { 340 | match = false; 341 | break; 342 | } 343 | } 344 | if (match) 345 | { 346 | return i + 1; 347 | } 348 | } 349 | return 0; 350 | } 351 | int tj_strrchr(const char str[], const char ch) 352 | { 353 | //Because the maximal output is n, according to the instruction 354 | // '\0' will not be counted 355 | int l = tj_strlen(str); 356 | for (int i = l - 1; i >= 0; i--) 357 | { 358 | if (str[i] == ch) 359 | { 360 | return i + 1; 361 | } 362 | } 363 | return 0; 364 | } 365 | int tj_strrstr(const char str[], const char substr[]) 366 | { 367 | int str_len = tj_strlen(str); 368 | int sub_len = tj_strlen(substr); 369 | for (int i = str_len - sub_len; i >= 0; i--) 370 | { 371 | int match = true; 372 | for (int j = 0; j < sub_len; j++) 373 | { 374 | if (str[i + j] != substr[j]) 375 | { 376 | match = false; 377 | break; 378 | } 379 | } 380 | if (match) 381 | { 382 | return i + 1; 383 | } 384 | } 385 | return 0; 386 | } 387 | 388 | int tj_strrev(char str[]) 389 | { 390 | int len = tj_strlen(str); 391 | if (len <= 1 ) 392 | { 393 | return 0; 394 | } 395 | int ub = (len - 1) / 2; 396 | for (int i = 0; i <= ub; i++) 397 | { 398 | int tmp = str[i]; 399 | str[i] = str[len - 1 - i]; 400 | str[len - 1 - i] = tmp; 401 | } 402 | return 0; 403 | } -------------------------------------------------------------------------------- /W1001/5-b12.h: -------------------------------------------------------------------------------- 1 | /* ----------------------------------------- 2 | 3 | 本文件不需要提交、不允许改动 4 | 5 | ----------------------------------------- */ 6 | 7 | #pragma once 8 | 9 | /* 此处给出了一组字符串处理函数的原型声明 */ 10 | int tj_strlen (const char str[]); 11 | int tj_strcat (char s1[], const char s2[]); 12 | int tj_strncat (char s1[], const char s2[], const int len); 13 | int tj_strcpy (char s1[], const char s2[]); 14 | int tj_strncpy (char s1[], const char s2[], const int len); 15 | int tj_strcmp (const char s1[], const char s2[]); 16 | int tj_strcasecmp (const char s1[], const char s2[]); 17 | int tj_strncmp (const char s1[], const char s2[], const int len); 18 | int tj_strcasencmp(const char s1[], const char s2[], const int len); 19 | int tj_strupr (char str[]); 20 | int tj_strlwr (char str[]); 21 | int tj_strchr (const char str[], const char ch); 22 | int tj_strstr (const char str[], const char substr[]); 23 | int tj_strrchr (const char str[], const char ch); 24 | int tj_strrstr (const char str[], const char substr[]); 25 | int tj_strrev (char str[]); 26 | -------------------------------------------------------------------------------- /W1001/5-b13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | srand(static_cast(time(0)));//!!!!!! 10 | 11 | const int BOARD_HEIGHT = 10; 12 | const int BOARD_WIDTH = 26; 13 | const int MINE_COUNTS = 50; 14 | 15 | char board[BOARD_HEIGHT + 2][BOARD_WIDTH + 2]; 16 | //初始化 17 | for (int i = 0; i < BOARD_HEIGHT + 2; i++) 18 | { 19 | for (int j = 0; j < BOARD_WIDTH + 2; j++) 20 | { 21 | board[i][j] = '0'; 22 | } 23 | } 24 | //生成雷 25 | int mines_generated = MINE_COUNTS; 26 | while (mines_generated) 27 | { 28 | int mX, mY; 29 | mX = rand() % BOARD_HEIGHT + 1; 30 | mY = rand() % BOARD_WIDTH + 1; 31 | if (board[mX][mY] == '*') 32 | { 33 | continue; 34 | } 35 | else 36 | { 37 | mines_generated--; 38 | board[mX][mY] = '*'; 39 | } 40 | } 41 | //计算雷数 42 | for (int i = 1; i <= BOARD_HEIGHT; i++) 43 | { 44 | for (int j = 1; j <= BOARD_WIDTH; j++) 45 | { 46 | int cnt = 0; 47 | for (int k = i - 1; k <= i + 1; k++) 48 | { 49 | for (int m = j - 1; m <= j + 1; m++) 50 | { 51 | if (board[k][m] == '*') 52 | { 53 | cnt++; 54 | } 55 | } 56 | } 57 | if (board[i][j] != '*') 58 | { 59 | board[i][j] = '0' + cnt; 60 | } 61 | cout << board[i][j] << " "; 62 | } 63 | cout << endl; 64 | } 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /W1001/5-b14.c: -------------------------------------------------------------------------------- 1 | /* 已验证 xxx 的扫雷内部数组 */ 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | #define BOARD_HEIGHT 10 6 | #define BOARD_WIDTH 26 7 | #define MINE_COUNTS 50 8 | #define STR_TMP_LEN 5000 9 | 10 | int main() 11 | { 12 | char s[STR_TMP_LEN]; 13 | char board[BOARD_HEIGHT+2][BOARD_WIDTH + 2]; 14 | char tmp; 15 | for (int i = 1; i <= BOARD_HEIGHT; i++) 16 | { 17 | for (int j = 1; j <= BOARD_WIDTH; j++) 18 | { 19 | int x = scanf("%s", &s); 20 | board[i][j] = s[0]; 21 | x++; 22 | } 23 | } 24 | //检验错误1 25 | int mine_cnt = 0; 26 | for (int i = 1; i <= BOARD_HEIGHT; i++) 27 | { 28 | for (int j = 1; j <= BOARD_WIDTH; j++) 29 | { 30 | if (board[i][j] == '*') 31 | { 32 | mine_cnt++; 33 | } 34 | } 35 | } 36 | if (mine_cnt != MINE_COUNTS) 37 | { 38 | printf("错误1\n"); 39 | return 0; 40 | } 41 | //检验错误2 42 | for (int i = 1; i <= BOARD_HEIGHT; i++) 43 | { 44 | for (int j = 1; j <= BOARD_WIDTH; j++) 45 | { 46 | int cnt = 0; 47 | for (int k = i - 1; k <= i + 1; k++) 48 | { 49 | for (int m = j - 1; m <= j + 1; m++) 50 | { 51 | if (board[k][m] == '*') 52 | { 53 | cnt++; 54 | } 55 | } 56 | } 57 | if (cnt+'0' != board[i][j] && board[i][j]!='*') 58 | { 59 | printf("错误2\n"); 60 | return 0; 61 | } 62 | } 63 | } 64 | //正确 65 | printf("正确\n"); 66 | return 0; 67 | } -------------------------------------------------------------------------------- /W1101/w11-s1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAXN 11000 3 | using namespace std; 4 | 5 | int count(int src,int digit) 6 | { 7 | int ans=0; 8 | while(1) 9 | { 10 | if(src<10&&src>=0) 11 | { 12 | ans+=(src==digit); 13 | break; 14 | } 15 | ans+=((src%10)==digit); 16 | src/=10; 17 | } 18 | return ans; 19 | } 20 | 21 | int main() 22 | { 23 | int arr[MAXN]; 24 | int n,digit=0,ans=0; 25 | cout<<"请输入数据的个数[1..10000]"<>n; 27 | cout<<"请输入"<>arr[i]; 31 | } 32 | cout<<"请输入数码[0..9]"<>digit; 34 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | const int STACK_SIZE = 10; 6 | const int COL_COUNT = 3; 7 | 8 | int stack_top_a = 0; 9 | int stack_top_b = 0; 10 | int stack_top_c = 0; 11 | int move_counter = 0; 12 | 13 | int stack_a[STACK_SIZE]; 14 | int stack_b[STACK_SIZE]; 15 | int stack_c[STACK_SIZE]; 16 | 17 | void result_print(int is_first, int lvl, char src,char dst) 18 | { 19 | if (is_first) 20 | { 21 | cout << "锟斤拷始:" << " "; 22 | } 23 | else 24 | { 25 | cout << "锟斤拷" << setw(4) << move_counter << " 锟斤拷(" << setw(2) << lvl << "): " << src << "-->" << dst; 26 | } 27 | cout << " A:"; 28 | for (int i = 0; i < stack_top_a; i++) 29 | { 30 | cout << setw(2)<< stack_a[i]; 31 | } 32 | for (int i = stack_top_a ; i < 10; i++) 33 | { 34 | cout << " "; 35 | } 36 | cout << " B:"; 37 | for (int i = 0; i < stack_top_b; i++) 38 | { 39 | cout << setw(2)<< stack_b[i] ; 40 | } 41 | for (int i = stack_top_b ; i < 10; i++) 42 | { 43 | cout << " "; 44 | } 45 | cout << " C:"; 46 | for (int i = 0; i < stack_top_c; i++) 47 | { 48 | cout <> levels; 125 | cinflag = cin.fail(); 126 | cin.clear(); 127 | cin.ignore(2147483647, '\n'); 128 | if (cinflag || levels <= 0 || levels > 10) 129 | { 130 | continue; 131 | } 132 | break; 133 | } 134 | while (1) 135 | { 136 | cout << "锟斤拷锟斤拷锟斤拷锟斤拷始锟斤拷(A-C)锟斤拷" << endl; 137 | cin >> start; 138 | cinflag = cin.fail(); 139 | cin.clear(); 140 | cin.ignore(2147483647, '\n'); 141 | if (start >= 'a' && start <= 'c') 142 | { 143 | start = start - 'a' + 'A'; 144 | } 145 | if (cinflag || start < 'A' || start>'C') 146 | { 147 | continue; 148 | } 149 | break; 150 | } 151 | if (start == 'A') 152 | { 153 | for (int i = levels; i >= 1; i--) 154 | { 155 | stack_a[stack_top_a++] = i; 156 | } 157 | } 158 | if (start == 'B') 159 | { 160 | for (int i = levels; i >= 1; i--) 161 | { 162 | stack_b[stack_top_b++] = i; 163 | } 164 | } 165 | if (start == 'C') 166 | { 167 | for (int i = levels; i >= 1; i--) 168 | { 169 | stack_c[stack_top_c++] = i; 170 | } 171 | } 172 | while (1) 173 | { 174 | cout << "锟斤拷锟斤拷锟斤拷目锟斤拷锟斤拷(A-C)锟斤拷" << endl; 175 | cin >> dest; 176 | cinflag = cin.fail(); 177 | cin.clear(); 178 | cin.ignore(2147483647, '\n'); 179 | if (dest >= 'a' && dest <= 'c') 180 | { 181 | dest = dest - 'a' + 'A'; 182 | } 183 | if (cinflag || dest < 'A' || dest>'C') 184 | { 185 | continue; 186 | } 187 | if (dest == start) 188 | { 189 | cout << "目锟斤拷锟斤拷(" << dest << ")锟斤拷锟斤拷锟斤拷锟斤拷始锟斤拷(" << start << ")锟斤拷同" << endl; 190 | continue; 191 | } 192 | break; 193 | } 194 | if (static_cast(start) + static_cast(dest) == static_cast('A') + static_cast('C')) 195 | { 196 | tmp = 'B'; 197 | } 198 | else if (static_cast(start) + static_cast(dest) == static_cast('B') + static_cast('C')) 199 | { 200 | tmp = 'A'; 201 | } 202 | else if (static_cast(start) + static_cast(dest) == static_cast('A') + static_cast('B')) 203 | { 204 | tmp = 'C'; 205 | } 206 | //cout << "锟狡讹拷锟斤拷锟斤拷为:" << endl; 207 | result_print(1, 0, 0, 0); 208 | hanoi(levels, start, tmp, dest); 209 | 210 | return 0; 211 | } 212 | -------------------------------------------------------------------------------- /W1103/5-b15.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main() 7 | { 8 | char str[3][128]; 9 | int upper = 0, lower = 0, number = 0, space = 0, other = 0; 10 | 11 | for (int i = 0; i < 3; i++) 12 | { 13 | cout << "请输入第" << i + 1 << "行" << endl; 14 | cin.getline(str[i], 128); 15 | int j = 0; 16 | while (str[i][j]) 17 | { 18 | if (str[i][j] >= 'a' && str[i][j] <= 'z') 19 | { 20 | lower++; 21 | } 22 | else if (str[i][j] >= 'A' && str[i][j] <= 'Z') 23 | { 24 | upper++; 25 | } 26 | else if (str[i][j] >= '0' && str[i][j] <= '9') 27 | { 28 | number++; 29 | } 30 | else if (str[i][j] == ' ') 31 | { 32 | space++; 33 | } 34 | else 35 | { 36 | other++; 37 | } 38 | j++; 39 | } 40 | } 41 | 42 | 43 | cout << "大写 : " << upper << endl; 44 | cout << "小写 : " << lower << endl; 45 | cout << "数字 : " << number << endl; 46 | cout << "空格 : " << space << endl; 47 | cout << "其它 : " << other << endl; 48 | return 0; 49 | } -------------------------------------------------------------------------------- /W1103/5-b16-1.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #define STUDENT_CNT 10 4 | 5 | void info_input(char stuId[][8], char stuName[][9], int stuGrades[]) 6 | { 7 | for (int i = 0; i < STUDENT_CNT; i++) 8 | { 9 | printf("请输入第%d个人的学号、姓名、成绩\n", i + 1); 10 | int r = scanf("%s%s%d", stuId[i], stuName[i], &stuGrades[i]); 11 | } 12 | printf("\n"); 13 | } 14 | int tj_strcmp(const char s1[], const char s2[]) 15 | { 16 | int s1_valid = 1; 17 | int s2_valid = 1; 18 | int s1_ascii, s2_ascii; 19 | int i = 0; 20 | int ans = 0; 21 | while (1) 22 | { 23 | if (s1[i] == '\0') 24 | { 25 | s1_valid = 0; 26 | } 27 | if (s1_valid == 0) 28 | { 29 | s1_ascii = 0; 30 | } 31 | else 32 | { 33 | s1_ascii = (int)(s1[i]); 34 | } 35 | 36 | if (s2[i] == '\0') 37 | { 38 | s2_valid = 0; 39 | } 40 | if (s2_valid == 0) 41 | { 42 | s2_ascii = 0; 43 | } 44 | else 45 | { 46 | s2_ascii = (int)(s2[i]); 47 | } 48 | ans = s1_ascii - s2_ascii; 49 | if (ans != 0) 50 | { 51 | return ans; 52 | } 53 | else if (s2_valid == 0 && s1_valid == 0) 54 | { 55 | return 0; 56 | } 57 | i++; 58 | } 59 | return 0; 60 | } 61 | void info_sort(char stuId[][8], char stuName[][9], int stuGrades[]) 62 | { 63 | 64 | for (int i = 0; i < STUDENT_CNT; i++) 65 | { 66 | int select_id = i; 67 | for (int j = i + 1; j < STUDENT_CNT; j++) 68 | { 69 | if (tj_strcmp(stuId[select_id], stuId[j]) < 0) 70 | { 71 | select_id = j; 72 | } 73 | } 74 | if (select_id != i) 75 | { 76 | //Swap stuId 77 | for (int j = 0; j < 8; j++) 78 | { 79 | char tmp = stuId[i][j]; 80 | stuId[i][j] = stuId[select_id][j]; 81 | stuId[select_id][j] = tmp; 82 | } 83 | //Swap Name 84 | for (int j = 0; j < 9; j++) 85 | { 86 | char tmp = stuName[i][j]; 87 | stuName[i][j] = stuName[select_id][j]; 88 | stuName[select_id][j] = tmp; 89 | } 90 | //Swap Grade 91 | int tmp = stuGrades[i]; 92 | stuGrades[i] = stuGrades[select_id]; 93 | stuGrades[select_id] = tmp; 94 | } 95 | } 96 | } 97 | 98 | void info_print(char stuId[][8], char stuName[][9], int stuGrades[]) 99 | { 100 | printf("不及格名单:\n"); 101 | for (int i = 0; i < STUDENT_CNT; i++) 102 | { 103 | if (stuGrades[i] < 60) 104 | { 105 | printf("%s %s %d\n", stuName[i], stuId[i], stuGrades[i]); 106 | } 107 | 108 | } 109 | } 110 | int main() 111 | { 112 | char stuId[STUDENT_CNT][8]; 113 | char stuName[STUDENT_CNT][9]; 114 | int stuGrades[STUDENT_CNT]; 115 | 116 | info_input(stuId, stuName, stuGrades); 117 | info_sort(stuId, stuName, stuGrades); 118 | info_print(stuId, stuName, stuGrades); 119 | 120 | 121 | return 0; 122 | } -------------------------------------------------------------------------------- /W1103/5-b16-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define STUDENT_CNT 10 4 | using namespace std; 5 | 6 | void info_input(string stuId[], string stuName[], int stuGrades[]) 7 | { 8 | for (int i = 0; i < STUDENT_CNT; i++) 9 | { 10 | cout << "请输入第" << i + 1 << "个人的学号、姓名、成绩" << endl; 11 | cin >> stuId[i] >> stuName[i] >> stuGrades[i]; 12 | } 13 | cout << endl; 14 | } 15 | int tj_strcmp(string s1, string s2) 16 | { 17 | int s1_valid = 1; 18 | int s2_valid = 1; 19 | int s1_ascii, s2_ascii; 20 | int i = 0; 21 | int ans = 0; 22 | while (1) 23 | { 24 | if (s1[i] == '\0') 25 | { 26 | s1_valid = 0; 27 | } 28 | if (s1_valid == 0) 29 | { 30 | s1_ascii = 0; 31 | } 32 | else 33 | { 34 | s1_ascii = (int)(s1[i]); 35 | } 36 | 37 | if (s2[i] == '\0') 38 | { 39 | s2_valid = 0; 40 | } 41 | if (s2_valid == 0) 42 | { 43 | s2_ascii = 0; 44 | } 45 | else 46 | { 47 | s2_ascii = (int)(s2[i]); 48 | } 49 | ans = s1_ascii - s2_ascii; 50 | if (ans != 0) 51 | { 52 | return ans; 53 | } 54 | else if (s2_valid == 0 && s1_valid == 0) 55 | { 56 | return 0; 57 | } 58 | i++; 59 | } 60 | return 0; 61 | } 62 | void info_sort(string stuId[], string stuName[], int stuGrades[]) 63 | { 64 | 65 | for (int i = 0; i < STUDENT_CNT; i++) 66 | { 67 | int select_id = i; 68 | for (int j = i + 1; j < STUDENT_CNT; j++) 69 | { 70 | if (stuId[select_id] < stuId[j]) 71 | { 72 | select_id = j; 73 | } 74 | } 75 | if (select_id != i) 76 | { 77 | //Swap stuId 78 | string tmp1 = stuId[i]; 79 | stuId[i] = stuId[select_id]; 80 | stuId[select_id] = tmp1; 81 | //Swap Name 82 | string tmp2 = stuName[i]; 83 | stuName[i] = stuName[select_id]; 84 | stuName[select_id] = tmp2; 85 | //Swap Grade 86 | int tmp = stuGrades[i]; 87 | stuGrades[i] = stuGrades[select_id]; 88 | stuGrades[select_id] = tmp; 89 | } 90 | } 91 | } 92 | 93 | void info_print(string stuId[], string stuName[], int stuGrades[]) 94 | { 95 | cout << "不及格名单:" << endl; 96 | for (int i = 0; i < STUDENT_CNT; i++) 97 | { 98 | if (stuGrades[i] < 60) 99 | { 100 | cout << stuName[i] << " " << stuId[i] << " " << stuGrades[i] << endl; 101 | } 102 | } 103 | } 104 | int main() 105 | { 106 | string stuId[STUDENT_CNT]; 107 | string stuName[STUDENT_CNT]; 108 | int stuGrades[STUDENT_CNT]; 109 | 110 | info_input(stuId, stuName, stuGrades); 111 | info_sort(stuId, stuName, stuGrades); 112 | info_print(stuId, stuName, stuGrades); 113 | 114 | return 0; 115 | } -------------------------------------------------------------------------------- /W1103/5-b17.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int upper = 0, lower = 0, number = 0, other = 0, len = 0; 9 | srand(static_cast(time(0))); 10 | cout << "请输入密码长度(12-16),大写字母个数(≥2),小写字母个数(≥2),数字个数(≥2),其它符号个数(≥2)" << endl; 11 | while (1) 12 | { 13 | cin >> len >> upper >> lower >> number >> other; 14 | if (cin.fail()) 15 | { 16 | cout << "输入含有非法字符." << endl; 17 | return 0; 18 | } 19 | if (len < 12 || len>16) 20 | { 21 | cout << "密码长度[" << len << "]不正确" << endl; 22 | return 0; 23 | } 24 | if (upper < 2) 25 | { 26 | cout << "大写字母个数[" << upper << "]不正确" << endl; 27 | return 0; 28 | } 29 | if (lower < 2) 30 | { 31 | cout << "小写字母个数[" << lower << "]不正确" << endl; 32 | return 0; 33 | } 34 | if (number < 2) 35 | { 36 | cout << "数字个数[" << number << "]不正确" << endl; 37 | return 0; 38 | } 39 | if (other < 2) 40 | { 41 | cout << "其它符号个数[" << other << "]不正确" << endl; 42 | return 0; 43 | } 44 | if (upper + lower + number + other > len) 45 | { 46 | cout << "所有字符类型之和[" << upper << "+" << lower << "+" << number << "+" << other << "]大于总密码长度[" << len << "]" << endl; 47 | return 0; 48 | } 49 | break; 50 | } 51 | cout << len << " " << upper << " " << lower << " " << number << " " << other << endl; 52 | char password[10][17]; 53 | for (int T = 0; T < 10; T++) 54 | { 55 | int types[4]; 56 | 57 | types[0] = upper; 58 | types[1] = lower; 59 | types[2] = number; 60 | types[3] = other; 61 | int uncategorized = len - (upper + lower + number + other); 62 | while (uncategorized--) 63 | { 64 | types[rand() % 4]++; 65 | } 66 | for (int i = 0; i < len; i++) 67 | { 68 | while (1) 69 | { 70 | int cur_type = rand() % 4; 71 | if (types[cur_type] != 0) 72 | { 73 | types[cur_type]--; 74 | switch (cur_type) 75 | { 76 | case 0: 77 | password[T][i] = rand() % 26 + 'A'; 78 | break; 79 | case 1: 80 | password[T][i] = rand() % 26 + 'a'; 81 | break; 82 | case 2: 83 | password[T][i] = rand() % 10 + '0'; 84 | break; 85 | case 3: 86 | while (1) 87 | { 88 | password[T][i] = rand() % (126 - 33 + 1) + 33; 89 | if (password[T][i] >= 'a' && password[T][i] <= 'z') 90 | { 91 | continue; 92 | } 93 | else if (password[T][i] >= 'A' && password[T][i] <= 'Z') 94 | { 95 | continue; 96 | } 97 | else if (password[T][i] >= '0' && password[T][i] <= '9') 98 | { 99 | continue; 100 | } 101 | break; 102 | } 103 | break; 104 | default: 105 | break; 106 | } 107 | break; 108 | } 109 | continue; 110 | } 111 | } 112 | password[T][len] = '\0'; 113 | cout << password[T] << endl; 114 | } 115 | return 0; 116 | } -------------------------------------------------------------------------------- /W1103/5-b18.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define TEMP_CNT 1000 5 | #define PASSWORD_LINES 10 6 | #define PASSWORD_MAXLEN 17 7 | 8 | int main() 9 | { 10 | int valid = 1; 11 | char tip_line[TEMP_CNT]; 12 | cin.getline(tip_line, TEMP_CNT); 13 | int len, upper, lower, number, other; 14 | 15 | while (1) 16 | { 17 | cin >> len >> upper >> lower >> number >> other; 18 | if (cin.fail()) 19 | { 20 | cout << "错误" << endl; 21 | return 0; 22 | } 23 | if (len < 12 || len>16) 24 | { 25 | cout << "错误" << endl; 26 | return 0; 27 | } 28 | if (upper < 2) 29 | { 30 | cout << "错误" << endl; 31 | return 0; 32 | } 33 | if (lower < 2) 34 | { 35 | cout << "错误" << endl; 36 | return 0; 37 | } 38 | if (number < 2) 39 | { 40 | cout << "错误" << endl; 41 | return 0; 42 | } 43 | if (other < 2) 44 | { 45 | cout << "错误" << endl; 46 | return 0; 47 | } 48 | if (upper + lower + number + other > len) 49 | { 50 | cout << "错误" << endl; 51 | return 0; 52 | } 53 | break; 54 | } 55 | 56 | char password[PASSWORD_MAXLEN]; 57 | cin.getline(password, PASSWORD_MAXLEN); 58 | for (int i = 0; i < PASSWORD_LINES; i++) 59 | { 60 | int upper_real =0 , lower_real=0, number_real=0, other_real=0; 61 | cin >> password; 62 | int j = 0; 63 | while (password[j]) 64 | { 65 | if (password[j] >= 'a' && password[j] <= 'z') 66 | { 67 | lower_real++; 68 | } 69 | else if (password[j] >= 'A' && password[j] <= 'Z') 70 | { 71 | upper_real++; 72 | } 73 | else if (password[j] >= '0' && password[j] <= '9') 74 | { 75 | number_real++; 76 | } 77 | else 78 | { 79 | other_real++; 80 | } 81 | j++; 82 | } 83 | if (j != len || upper_real < upper || lower_real < lower) 84 | { 85 | valid = 0; 86 | break; 87 | } 88 | if (number_real < number || other_real < other) 89 | { 90 | valid = 0; 91 | break; 92 | } 93 | } 94 | if (valid) 95 | { 96 | cout << "正确" << endl; 97 | } 98 | else 99 | { 100 | cout << "错误" << endl; 101 | } 102 | return 0; 103 | } -------------------------------------------------------------------------------- /W1201/6-b1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define N 10 /* 假设最多转换10个数字 */ 5 | 6 | /* 不允许再定义其它函数、全局变量 */ 7 | 8 | int main() 9 | { 10 | /* 如果有不需要的变量,允许删除,但不允许添加或替换为其它类型的变量 */ 11 | char str[256], * p; 12 | int a[N] = { 0 }, * pnum, * pa; 13 | bool is_num; 14 | 15 | /* 上面的定义不准动(删除不需要的变量除外),下面为程序的具体实现,要求不得再定义任何变量、常量、常变量 */ 16 | cout << "请输入间隔含有若干正负数字的字符串" << endl; 17 | gets_s(str); 18 | is_num = false; 19 | pnum = a; 20 | for (p = str; *p != '\0'; p++) 21 | { 22 | if (*p >= '0' && *p <= '9' && is_num) 23 | { 24 | *pnum = *pnum * 10 + (*p - '0'); 25 | } 26 | else if (*p >= '0' && *p <= '9' && !is_num) 27 | { 28 | is_num = true; 29 | *pnum = *p - '0'; 30 | } 31 | else if(is_num) 32 | { 33 | pnum++; 34 | is_num = false; 35 | } 36 | if (pnum - a >= N) 37 | { 38 | break; 39 | } 40 | } 41 | if (is_num) 42 | { 43 | pnum++; 44 | } 45 | cout << "共有" << (pnum - a) << "个整数" << endl; 46 | for (pa = a; pa < pnum; pa++) 47 | { 48 | cout << *pa << " "; 49 | } 50 | cout << endl; 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /W1201/6-b2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool is_pal(char* s) 5 | { 6 | if (*s == '\0'||*s=='\n') 7 | return true; 8 | char* p = s, * i = s; 9 | while (*p&&*p!='\n') 10 | { 11 | p++; 12 | } 13 | p--; 14 | for (; *i != '\0'&&*i!='\n'; p--, i++) 15 | { 16 | if (*i != *p) 17 | return false; 18 | } 19 | return true; 20 | } 21 | 22 | int main() 23 | { 24 | char str[81]; 25 | cout << "请输入一个长度小于80的字符串(回文串)" << endl; 26 | fgets(str, 81, stdin); 27 | if (is_pal(str)) 28 | { 29 | cout << "yes"; 30 | } 31 | else 32 | { 33 | cout << "no"; 34 | } 35 | cout << endl; 36 | return 0; 37 | } -------------------------------------------------------------------------------- /W1201/6-b3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | char str[33]; 7 | unsigned int ans = 0u; 8 | cout << "请输入一个0/1组成的字符串,长度不超过32" << endl; 9 | cin >> str; 10 | for (char* i = str; *i != '\0'; i++) 11 | { 12 | ans = ans * 2u + (*i - '0'); 13 | } 14 | cout << ans << endl; 15 | return 0; 16 | } -------------------------------------------------------------------------------- /W1301/7-b1.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS //使用了VS认为unsafe的函数 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include //用getch,因此不需要支持Linux 7 | #include //Dev/CB的strlen需要 8 | using namespace std; 9 | 10 | struct tj_time { 11 | int tj_year; //表示年份 12 | int tj_month; //表示月(1-12) 13 | int tj_day; //表示日(1-28/29/30/31) 14 | int tj_hour; //表示小时(0-23) 15 | int tj_minute; //表示分(0-59) 16 | int tj_second; //表示秒(0-59) 17 | }; 18 | 19 | /* 可以在此定义其它需要的函数 */ 20 | 21 | 22 | 23 | /*************************************************************************** 24 | 函数名称: 25 | 功 能:给出提示并等待回车键 26 | 输入参数: 27 | 返 回 值: 28 | 说 明: 29 | ***************************************************************************/ 30 | void wait_for_enter(const char* const prompt = NULL) 31 | { 32 | if ((prompt == NULL) || (strlen(prompt) == 0)) //思考一下,||的左右两个条件能否互换 33 | cout << endl << "按回车键继续"; 34 | else 35 | cout << endl << prompt << ",按回车键继续"; 36 | 37 | while (_getch() != '\r') 38 | ; 39 | cout << endl << endl; 40 | } 41 | 42 | /*************************************************************************** 43 | 函数名称: 44 | 功 能:调用系统的转换函数将整型秒值转换为与本题相似的结构体并输出 45 | 输入参数: 46 | 返 回 值: 47 | 说 明: 48 | ***************************************************************************/ 49 | void system_time_output(const time_t input_time) //time_t的本质是64位无符号整数 50 | { 51 | struct tm* tt; //struct tm 为系统定义的结构体 52 | 53 | tt = localtime(&input_time); //localtime为系统函数 54 | 55 | /* tm_*** 为struct tm中的成员,和本题的struct tj_time具体的内容不完全符合,具体含义自己查找相关资料 */ 56 | cout << setfill('0') << setw(4) << tt->tm_year + 1900 << '-' 57 | << setw(2) << tt->tm_mon + 1 << '-' 58 | << setw(2) << tt->tm_mday << ' ' 59 | << setw(2) << tt->tm_hour << ':' 60 | << setw(2) << tt->tm_min << ':' 61 | << setw(2) << tt->tm_sec << endl; 62 | 63 | return; 64 | } 65 | 66 | /*************************************************************************** 67 | 函数名称: 68 | 功 能:自定义转换结果输出函数 69 | 输入参数: 70 | 返 回 值: 71 | 说 明: 72 | ***************************************************************************/ 73 | void tj_time_output(const struct tj_time* const tp) 74 | { 75 | /* 实现自定义结构的输出,输出形式与system_time_output相同 */ 76 | cout << setfill('0') << setw(4) << tp->tj_year << '-' 77 | << setw(2) << tp->tj_month << '-' 78 | << setw(2) << tp->tj_day << ' ' 79 | << setw(2) << tp->tj_hour << ':' 80 | << setw(2) << tp->tj_minute << ':' 81 | << setw(2) << tp->tj_second << endl; 82 | } 83 | 84 | /*************************************************************************** 85 | 函数名称: 86 | 功 能:自定义转换函数 87 | 输入参数: 88 | 返 回 值: 89 | 说 明: 90 | ***************************************************************************/ 91 | struct tj_time* tj_time_convert(int input_time) 92 | { 93 | static struct tj_time result; //定义静态局部变量,不准动 94 | 95 | /* 实现过程开始,在下面添加相应的定义及执行语句即可 */ 96 | 97 | //先按照1970-01-01 00:00:00转换 98 | int days_in_month[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; 99 | int day_cnt = input_time / (24 * 3600); 100 | result.tj_second = input_time % 60; 101 | result.tj_minute = input_time / 60 % 60; 102 | result.tj_hour = input_time / 3600 % 24; 103 | int i = 0, is_leap = 0; 104 | for ( i = 1970;; i++) 105 | { 106 | is_leap = ((!(i % 4) && (i % 100)) || (!(i % 100) && !(i % 400))); 107 | int days_in_year = 365 + is_leap; 108 | if (day_cnt <= days_in_year) 109 | { 110 | result.tj_year = i; 111 | break; 112 | } 113 | day_cnt -= days_in_year; 114 | } 115 | days_in_month[2] += is_leap; 116 | for (int j = 1; j <= 12; j++) 117 | { 118 | if (day_cnt <= days_in_month[j]) 119 | { 120 | result.tj_month = j; 121 | break; 122 | } 123 | day_cnt -= days_in_month[j]; 124 | } 125 | result.tj_day = day_cnt + 1; 126 | 127 | //增加8小时(避免Demo在大数字时直接溢出) 128 | result.tj_hour += 8; 129 | if (result.tj_hour >= 24) 130 | { 131 | result.tj_hour -= 24; 132 | result.tj_day++; 133 | } 134 | if (result.tj_day > days_in_month[result.tj_month]) 135 | { 136 | result.tj_day = 1; 137 | result.tj_month++; 138 | } 139 | if (result.tj_month >= 13) 140 | { 141 | result.tj_month = 1; 142 | result.tj_year++; 143 | } 144 | /* 实现过程结束 */ 145 | 146 | return &result; //注意,返回的是静态局部变量的地址,本语句不准动 147 | } 148 | 149 | /*************************************************************************** 150 | 函数名称: 151 | 功 能: 152 | 输入参数: 153 | 返 回 值: 154 | 说 明: 155 | ***************************************************************************/ 156 | int main() 157 | { 158 | int read_time; 159 | struct tj_time* tp; 160 | 161 | for (;;) { 162 | cin >> read_time; //因为采用输入重定向,此处不加任何提示 163 | 164 | /* 输入错误或<0则退出循环 */ 165 | if (cin.good() == 0 || read_time < 0) 166 | break; 167 | 168 | cout << "秒数 : " << read_time << endl; 169 | cout << "系统转换的结果 : "; 170 | system_time_output(read_time); 171 | 172 | cout << "自定义转换的结果 : "; 173 | tp = tj_time_convert(read_time); 174 | tj_time_output(tp); 175 | 176 | wait_for_enter(); 177 | } 178 | 179 | if (1) { 180 | struct tj_time* tp; 181 | int t = (int)time(0); //系统函数,取当前系统时间(从1970-01-01 00:00:00开始的秒数) 182 | 183 | cout << "当前系统时间 : " << t << endl; 184 | cout << "系统转换的结果 : "; 185 | system_time_output(t); 186 | 187 | cout << "自定义转换的结果 : "; 188 | tp = tj_time_convert(t); 189 | tj_time_output(tp); 190 | 191 | wait_for_enter(); 192 | } 193 | 194 | return 0; 195 | } 196 | -------------------------------------------------------------------------------- /W1301/7-b2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | struct KFC 7 | { 8 | char id; 9 | const char* name; 10 | double price; 11 | }; 12 | struct SPECIAL 13 | { 14 | const char* set; 15 | const char* name; 16 | double price; 17 | }; 18 | char chrupr(char x) 19 | { 20 | return (x <= 'z' && x >= 'a') ? (x - 'a' + 'A') : x; 21 | } 22 | void print_detail(const KFC* menu_list,const char* order_info,int* recv) 23 | { 24 | 25 | int dish[256]; 26 | 27 | int first_flag = 1; 28 | for (int i = 0; i < 256; i++) 29 | dish[i] = 0; 30 | 31 | for (const char* k = order_info; *k; k++) 32 | { 33 | dish[static_cast(chrupr(*k))]++; 34 | } 35 | for (int i = 0; i < 256; i++) 36 | { 37 | if (dish[i]) 38 | { 39 | for (const KFC* k = menu_list; (*k).id; k++) 40 | { 41 | if (k->id == chrupr(static_cast(i))) 42 | { 43 | if (first_flag) 44 | { 45 | first_flag = 0; 46 | } 47 | else 48 | { 49 | cout << "+"; 50 | } 51 | cout << k->name; 52 | } 53 | } 54 | if (dish[i] - 1) 55 | { 56 | cout << "*" << dish[i]; 57 | } 58 | 59 | } 60 | } 61 | if (recv != NULL) 62 | { 63 | for (int i = 0; i < 256; i++) 64 | { 65 | recv[i] = dish[i]; 66 | 67 | } 68 | } 69 | } 70 | void print_order(const KFC* menu_list, const SPECIAL* spec_list, const char* order) 71 | { 72 | int dish[256]; 73 | int spec_req[256]; 74 | cout << "您的点餐="; 75 | print_detail(menu_list, order, dish); 76 | double price = 0; 77 | //处理优惠 78 | if (1) 79 | { 80 | const SPECIAL* i = spec_list; 81 | do 82 | { 83 | 84 | for (int j = 0; j < 256; j++) 85 | spec_req[j] = 0; 86 | for (const char* j = i->set; *j; j++) 87 | spec_req[static_cast(*j)]++; 88 | while (1) 89 | { 90 | int satisfied = 1; 91 | for (int j = 0; j < 256; j++) 92 | { 93 | if (dish[j] < spec_req[j]) 94 | { 95 | satisfied = 0; 96 | break; 97 | } 98 | } 99 | if (!satisfied) 100 | break; 101 | price += i->price; 102 | for (int j = 0; j < 256; j++) 103 | { 104 | dish[j] -= spec_req[j]; 105 | } 106 | } 107 | 108 | } while ((++i)->name); 109 | } 110 | //处理单点 111 | if (1) 112 | { 113 | const KFC* i = menu_list; 114 | do 115 | { 116 | price += dish[static_cast(i->id)] * i->price; 117 | } while ((++i)->id); 118 | } 119 | cout << endl; 120 | //完成 121 | cout << "共计:" << price << "元" <id << " "; 151 | cout << setw(20) << i->name; 152 | cout << setw(7) << i->price; 153 | if ((i - menu_list) % 2 == 0) 154 | { 155 | cout << "| "; 156 | } 157 | else 158 | { 159 | cout << endl; 160 | } 161 | } while ((++i)->id); 162 | } 163 | cout << endl; 164 | 165 | //打印优惠信息 166 | cout << "【优惠信息】:" << endl; 167 | if (1) 168 | { 169 | const SPECIAL* j = spec_list; 170 | do 171 | { 172 | cout << j->name << "="; 173 | print_detail(menu_list, j->set, NULL); 174 | cout << "=" << (j->price) << endl; 175 | } while ((++j)->set != NULL); 176 | } 177 | //打印输入规则 178 | cout << endl; 179 | const char* example1 = "ANV"; 180 | const char* example2 = "akaak"; 181 | cout << "【输入规则说明】:" << endl; 182 | cout << example1 << "="; 183 | print_detail(menu_list, example1, NULL); 184 | cout << " / "; 185 | cout << example2 << "="; 186 | print_detail(menu_list, example2, NULL); 187 | cout << endl; 188 | cout << "字母不分大小写,不限顺序,单独输入0则退出程序"; 189 | cout << endl; 190 | cout << endl; 191 | 192 | } 193 | int main() 194 | { 195 | const struct KFC list[] = { 196 | {'A', "香辣鸡腿堡", 18}, 197 | {'B', "劲脆鸡腿堡", 18}, 198 | {'C', "新奥尔良烤鸡腿堡", 18.5}, 199 | {'D', "鸡肉火腿帕尼尼", 14.0}, 200 | {'E', "老北京鸡肉卷", 16.5}, 201 | {'F', "川辣嫩牛卷", 19}, 202 | {'G', "吮指原味鸡(1块)", 11.5}, 203 | {'H', "热辣薯片脆皮鸡", 12.5}, 204 | {'I', "新奥尔良烤翅(2块)", 12}, 205 | {'J', "劲爆鸡米花", 10.5}, 206 | {'K', "香辣鸡翅(2块)", 11.0}, 207 | {'L', "热辣香骨鸡(3块)", 11.0}, 208 | {'M', "鲜蔬色拉", 12.5}, 209 | {'N', "薯条(小)", 8}, 210 | {'O', "薯条(中)", 11}, 211 | {'P', "薯条(大)", 13}, 212 | {'Q', "芙蓉蔬荟汤", 8}, 213 | {'R', "原味花筒冰激凌", 6}, 214 | {'S', "醇香土豆泥", 6.5}, 215 | {'T', "香甜粟米棒", 8.0}, 216 | {'U', "葡式蛋挞", 7.5}, 217 | {'V', "百事可乐(小)", 7.0}, 218 | {'W', "百事可乐(中)", 9.5}, 219 | {'X', "百事可乐(大)", 11.5}, 220 | {'Y', "九珍果汁饮料", 12.0}, 221 | {'Z', "纯纯玉米饮", 11.0}, 222 | {'\0', NULL, 0} 223 | }; 224 | 225 | const struct SPECIAL special[] = { 226 | // {"ANV", "香辣鸡腿堡工作日午餐", 22}, //如果有需要,放开此项,注释掉下一行的“BMV”优惠,观察优惠菜单是否发生了变化 227 | {"BMV", "劲脆鸡腿堡超值套餐", 24}, 228 | {"ABCGGIIKKOUWWW", "超值全家桶", 100}, 229 | {"KIIRRJUWW", "缤纷小吃桶", 65}, 230 | {"JJ","劲爆鸡米花(2份小)", 9.5}, 231 | {NULL, NULL, 0} 232 | }; 233 | system("mode con cols=120 lines=35"); 234 | char order[8292]; 235 | while (1) 236 | { 237 | int fail_flag = 0; 238 | system("cls"); 239 | print_menu(list, special); 240 | cout << "请点单:"; 241 | cin.getline(order, 8290); 242 | cin.clear(); 243 | if (!*order) 244 | { 245 | continue; 246 | } 247 | if (*order == '0' && order[1] == 0) 248 | return 0; 249 | for (char* i = order; *i; i++) 250 | { 251 | if (!(chrupr(*i) >= 'A' && chrupr(*i) <= 'Z')) 252 | { 253 | fail_flag = 1; 254 | cout << "输入错误,按任意键继续." << endl; 255 | while (1) 256 | { 257 | int ch = _getch(); 258 | if (ch != 0 && ch != 224) 259 | break; 260 | } 261 | break; 262 | } 263 | if (fail_flag) 264 | break; 265 | } 266 | if (fail_flag) 267 | continue; 268 | print_order(list, special, order); 269 | } 270 | 271 | return 0; 272 | } 273 | -------------------------------------------------------------------------------- /W1401/8-b2.cpp: -------------------------------------------------------------------------------- 1 | /* 已验证 xxx 的hex文件生成*/ 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | ifstream fs; 10 | char s[1001]; 11 | char t[10]; 12 | char h[16]; 13 | unsigned int c, i = 0; 14 | cerr << "文件名以下形式均可以:" << endl; 15 | cerr << " a.txt:不带路径形式" << endl; 16 | cerr << " ..\\data\b.dat:相对路径形式" << endl; 17 | cerr << " C:\\Windows\\System32\\c.dat:绝对相对路径形式" << endl; 18 | cerr << "请输入文件名: "; 19 | cin.getline(s, 1000); 20 | fs.open(s, ios::binary | ios::in); 21 | if (fs.fail()) 22 | { 23 | cout << "文件" << s << "打开失败!" << endl; 24 | return 0; 25 | } 26 | cout << hex; 27 | while (1) 28 | { 29 | fs.read(t, 1); 30 | if (fs.eof()) 31 | break; 32 | if (i % 0x10 == 0x8) 33 | cout << " -"; 34 | if (i % 0x10 == 0) 35 | { 36 | cout << setfill('0') << setw(8) << i << " "; 37 | cout << setfill(' '); 38 | } 39 | c = ((unsigned int)t[0] % 0x100u); 40 | cout << " " << setfill('0') << setw(2) << (unsigned int)c; 41 | cout << setfill(' '); 42 | h[i % 0x10] = t[0]; 43 | i++; 44 | 45 | if (i % 0x10 == 0x0) 46 | { 47 | cout << " "; 48 | for (int j = 0; j < 0x10; j++) 49 | { 50 | cout << (h[j] >= 33 && h[j] <= 126 ? h[j] : '.'); 51 | } 52 | cout << endl; 53 | } 54 | } 55 | if (i % 0x10 != 0) 56 | { 57 | if (i % 0x10 <= 0x8) 58 | { 59 | cout << " "; 60 | } 61 | for (int j = i % 0x10; j < 0x10; j++) 62 | { 63 | cout << " "; 64 | } 65 | cout << " "; 66 | for (int j = 0; static_cast(j) < i % 0x10; j++) 67 | { 68 | cout << (h[j] >= 33 && h[j] <= 126 ? h[j] : '.'); 69 | } 70 | cout << endl; 71 | } 72 | fs.close(); 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /W1401/8-b3.cpp: -------------------------------------------------------------------------------- 1 | /* 已验证 xxx 的hex文件生成*/ 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | char hex2char(char* t) 8 | { 9 | char ret = 0; 10 | ret = (t[0] >= 'a' ? (t[0] - 'a' + 0xa) : (t[0] - '0' + 0x0)); 11 | ret *= 0x10; 12 | ret += (t[1] >= 'a' ? (t[1] - 'a' + 0xa) : (t[1] - '0' + 0x0)); 13 | return ret; 14 | } 15 | int main() 16 | { 17 | ifstream ifs; 18 | ofstream ofs; 19 | char s[1000], p[1000]; 20 | char temp[1000]; 21 | cerr << "文件名以下形式均可以:" << endl; 22 | cerr << " a.txt:不带路径形式" << endl; 23 | cerr << " ..\\data\b.dat:相对路径形式" << endl; 24 | cerr << " C:\\Windows\\System32\\c.dat:绝对相对路径形式" << endl; 25 | cerr << "请输入文件名: "; 26 | cin >> s; 27 | cerr << "请输入转换后的文件名 :"; 28 | cin >> p; 29 | ifs.open(s, ios::in); 30 | ofs.open(p, ios::out | ios::binary); 31 | if (ifs.fail()) 32 | { 33 | cout << "文件" << s << "打开失败!" << endl; 34 | return 0; 35 | } 36 | if (ofs.fail()) 37 | { 38 | ifs.close(); 39 | cout << "文件" << p << "打开失败!" << endl; 40 | return 0; 41 | } 42 | while (1) 43 | { 44 | int endflag = 0; 45 | while (1) 46 | { 47 | //offset 48 | ifs >> temp; 49 | if (ifs.eof()) 50 | { 51 | endflag = 1; 52 | break; 53 | } 54 | ifs.get(temp[2]); 55 | ifs.get(temp[2]); 56 | //hex 57 | for (int i = 0; i < 0x10; i++) 58 | { 59 | if (i == 0x8) 60 | { 61 | ifs.get(temp[0]); 62 | ifs.get(temp[1]); 63 | if (ifs.eof()) 64 | { 65 | endflag = 1; 66 | break; 67 | } 68 | } 69 | 70 | ifs.get(temp[0]); 71 | ifs.get(temp[1]); 72 | ifs.get(temp[2]); 73 | if (ifs.eof()) 74 | { 75 | endflag = 1; 76 | break; 77 | } 78 | if (temp[0] == 32 && temp[1] == 32) 79 | continue; 80 | char x = hex2char(temp); 81 | ofs.write(&x, 1); 82 | } 83 | //characters 84 | char x = 0; 85 | while(x!='\n') 86 | { 87 | ifs.get(x); 88 | if (ifs.eof()) 89 | { 90 | endflag = 1; 91 | break; 92 | } 93 | } 94 | } 95 | if (endflag) 96 | break; 97 | } 98 | ifs.close(); 99 | ofs.close(); 100 | 101 | 102 | return 0; 103 | } 104 | -------------------------------------------------------------------------------- /W1401/w14-s1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | /* ----具体要求---- 6 | 1、不允许添加其它头文件 7 | 2、不允许定义全局变量、静态局部变量(全局只读变量或宏定义不受此限制) 8 | 3、最多允许添加一个函数,且需要满足要求 9 | --------------------------------------------------------------------- 10 | */ 11 | 12 | #define N 10 13 | 14 | struct course { 15 | float value; //某课程的成绩(百分制) 16 | float weight; //某课程的学分(权重) 17 | int gpa; //某课程的绩点(同济规则,[0..60) - 0, [60,70) - 2, [70,80) - 3, [80-90) - 4,[90,100] - 5) 18 | }; 19 | 20 | struct student { 21 | int no; //学号(虽然用int不够合理,此处不考虑) 22 | char name[9]; //假设姓名最长4个汉字 23 | struct course score[3]; //数组放三门课的成绩(未使用宏定义,函数实现时,直接写3即可) 24 | float wgpa_sum; //可增加其它你认为需要增加的结构体成员(限一个),不要则删除本行 25 | }; 26 | 27 | /*************************************************************************** 28 | 函数名称: 29 | 功 能: 30 | 输入参数: 31 | 返 回 值: 32 | 说 明:此处最多允许添加一个函数,且函数的形参、自动变量等都必须是简单变量(即不允许[]形式) 33 | ***************************************************************************/ 34 | 35 | 36 | 37 | /*************************************************************************** 38 | 函数名称: 39 | 功 能:输入10个学生的信息 40 | 输入参数: 41 | 返 回 值: 42 | 说 明:不允许出现宏定义N 43 | ***************************************************************************/ 44 | void input(struct student* stu, int num) 45 | { 46 | /* 除这两个整型变量外,本函数不再允许定义任何形式的变量 47 | 如果不用,允许删除,但不得增加或替换为其它形式的变量 48 | 包括for(int k=0; )形式的新变量定义同样禁止 */ 49 | 50 | int i, j; 51 | /* 函数的实现部分 */ 52 | for(i=0;i>stu[i].no; 56 | cin>>stu[i].name; 57 | stu[i].wgpa_sum=0; 58 | for(j=0;j<3;j++) 59 | { 60 | cin>>stu[i].score[j].value; 61 | cin>>stu[i].score[j].weight; 62 | if(stu[i].score[j].value<60) 63 | { 64 | stu[i].score[j].gpa=0; 65 | } 66 | else if(stu[i].score[j].value<70) 67 | { 68 | stu[i].score[j].gpa=2; 69 | } 70 | else if(stu[i].score[j].value<80) 71 | { 72 | stu[i].score[j].gpa=3; 73 | } 74 | else if(stu[i].score[j].value<90) 75 | { 76 | stu[i].score[j].gpa=4; 77 | } 78 | else if(stu[i].score[j].value<=100) 79 | { 80 | stu[i].score[j].gpa=5; 81 | } 82 | else 83 | { 84 | stu[i].score[j].gpa=0; 85 | } 86 | stu[i].wgpa_sum+=stu[i].score[j].weight*stu[i].score[j].gpa; 87 | 88 | } 89 | stu[i].wgpa_sum/=(stu[i].score[0].weight+stu[i].score[1].weight+stu[i].score[2].weight); 90 | } 91 | 92 | } 93 | 94 | /*************************************************************************** 95 | 函数名称: 96 | 功 能:输出基本信息 97 | 输入参数: 98 | 返 回 值: 99 | 说 明:不允许出现宏定义N 100 | ***************************************************************************/ 101 | void output_base(struct student* stu, int num) 102 | { 103 | /* 除这两个指针变量外,本函数不再允许定义任何形式的变量 104 | 如果不用,允许删除,但不得增加或替换为其它形式的变量 105 | 包括for(int i=0; )形式的新变量定义同样禁止 */ 106 | struct student* ps; 107 | struct course* pc; 108 | 109 | /* 函数的实现部分,不允许任何形式的[]出现 */ 110 | cout<no; 123 | cout<name; 124 | for(pc=ps->score;pc-(ps->score)<3;pc++) 125 | { 126 | cout<gpa; 127 | cout<weight; 128 | } 129 | cout<wgpa_sum<maxval) 150 | { 151 | maxval=stu[i].wgpa_sum; 152 | } 153 | } 154 | 155 | 156 | cout< 2 | #include "cmd_console_tools.h" 3 | #include "hanoi.h" 4 | using namespace std; 5 | /* ---------------------------------------------------------------------------------- 6 | 7 | 本文件功能: 8 | 1、放main函数 9 | 2、初始化屏幕 10 | 3、调用菜单函数(hanoi_menu.cpp中)并返回选项 11 | 4、根据选项调用菜单各项对应的执行函数(hanoi_multiple_solutions.cpp中) 12 | 13 | 本文件要求: 14 | 1、不允许定义全局变量(含外部全局和静态全局,const及#define不在限制范围内) 15 | 2、静态局部变量的数量不限制,但使用准则也是:少用、慎用、能不用尽量不用 16 | 3、按需加入系统头文件、自定义头文件、命名空间等 17 | 18 | ----------------------------------------------------------------------------------- */ 19 | 20 | /*************************************************************************** 21 | 函数名称: 22 | 功 能: 23 | 输入参数: 24 | 返 回 值: 25 | 说 明: 26 | ***************************************************************************/ 27 | int main() 28 | { 29 | /* demo中首先执行此句,将cmd窗口设置为40行x120列(缓冲区宽度120列,行数9000行,即cmd窗口右侧带有垂直滚动杆)*/ 30 | cct_setconsoleborder(120, 40, 120, 9000); 31 | while (1) 32 | { 33 | int sel = hanoi_menu_select(); 34 | if (sel >= 0 && sel <= 9) 35 | { 36 | cout << sel; 37 | } 38 | switch (sel) 39 | { 40 | case 0: 41 | return 0; 42 | case 1: 43 | hanoi_sol_type_1_2_3_4(1); 44 | break; 45 | case 2: 46 | hanoi_sol_type_1_2_3_4(2); 47 | break; 48 | case 3: 49 | hanoi_sol_type_1_2_3_4(3); 50 | break; 51 | case 4: 52 | hanoi_sol_type_1_2_3_4(4); 53 | break; 54 | case 5: 55 | hanoi_sol_type_5(); 56 | break; 57 | case 6: 58 | hanoi_sol_type_6_7_8_9(6); 59 | break; 60 | case 7: 61 | hanoi_sol_type_6_7_8_9(7); 62 | break; 63 | case 8: 64 | hanoi_sol_type_6_7_8_9(8); 65 | break; 66 | case 9: 67 | hanoi_sol_type_6_7_8_9(9); 68 | break; 69 | default: 70 | break; 71 | } 72 | cct_cls(); 73 | } 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /W_Hanoi/hanoi_menu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "hanoi.h" 4 | #include "cmd_console_tools.h" 5 | using namespace std; 6 | 7 | /* ---------------------------------------------------------------------------------- 8 | 9 | 本文件功能: 10 | 1、放被 hanoi_main.cpp 调用的菜单函数,要求显示各菜单项,读入正确的选项后返回 11 | 12 | 本文件要求: 13 | 1、不允许定义外部全局变量(const及#define不在限制范围内) 14 | 2、不允许定义静态全局变量(全局变量的使用准则是:少用、慎用、能不用尽量不用) 15 | 3、静态局部变量的数量不限制,但使用准则也是:少用、慎用、能不用尽量不用 16 | 4、按需加入系统头文件、自定义头文件、命名空间等 17 | 18 | ----------------------------------------------------------------------------------- */ 19 | 20 | 21 | /*************************************************************************** 22 | 函数名称: 23 | 功 能: 24 | 输入参数: 25 | 返 回 值: 26 | 说 明: 27 | ***************************************************************************/ 28 | 29 | int hanoi_menu_select() 30 | { 31 | for (int i = 0; i < HANOI_MENU_BORDER_CHAR_COUNT; i++) 32 | cout << "-"; 33 | cout << endl; 34 | cout << "1.基本解" << endl; 35 | cout << "2.基本解(步数记录)" << endl; 36 | cout << "3.内部数组显示(横向)" << endl; 37 | cout << "4.内部数组显示(纵向+横向)" << endl; 38 | cout << "5.图形解-预备-画三个圆柱" << endl; 39 | cout << "6.图形解-预备-在起始柱上画n个盘子" << endl; 40 | cout << "7.图形解-预备-第一次移动" << endl; 41 | cout << "8.图形解-自动移动版本" << endl; 42 | cout << "9.图形解-游戏版" << endl; 43 | cout << "0.退出" << endl; 44 | for (int i = 0; i < HANOI_MENU_BORDER_CHAR_COUNT; i++) 45 | cout << "-"; 46 | cout << endl; 47 | cout << "[请选择:]"; 48 | int ch = 0; 49 | while (1) 50 | { 51 | ch = _getch(); 52 | if (ch < '0' || ch>'9') 53 | continue; 54 | break; 55 | } 56 | return ch-'0'; 57 | } -------------------------------------------------------------------------------- /W_SynthesisTen/90-b2-base.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "90-b2.h" 4 | #include "cmd_console_tools.h" 5 | using namespace std; 6 | 7 | /*************************************************************************** 8 | 函数名称:game_base_find_recursive 9 | 功 能:命令行版矩阵打印 10 | 输入参数: int mat[][GAME_INPUT_COL_MAX] 游戏矩阵 11 | int r 矩阵行数 12 | int c 矩阵列数 13 | int type 输出方式 14 | 返 回 值: 15 | 说 明: 16 | ***************************************************************************/ 17 | void game_base_print_mat(int mat[][GAME_INPUT_COL_MAX], int r, int c, int type = GAME_BASE_PRINT_DFFAULT) 18 | { 19 | cout << " | "; 20 | for (int i = 0; i < c; i++) 21 | cout << setw(2) << i << " "; 22 | cout << endl; 23 | cout << "--+-"; 24 | for (int i = 0; i < c; i++) 25 | cout << "---"; 26 | cout << endl; 27 | for (int i = 0; i < r; i++) 28 | { 29 | cout << static_cast('A' + i); 30 | cout << " | "; 31 | for (int j = 0; j < c; j++) 32 | { 33 | if (type == GAME_BASE_PRINT_DFFAULT) 34 | { 35 | cout << setw(2) << mat[i][j] % FLAGGED << " "; 36 | } 37 | else if (type == GAME_BASE_PRINT_FIND_RESULT) 38 | { 39 | if (mat[i][j] >= FLAGGED) 40 | cout << setw(2) << "*" << " "; 41 | else 42 | cout << setw(2) << "0" << " "; 43 | } 44 | else if (type == GAME_BASE_PRINT_COLORED_RESULT) 45 | { 46 | if (mat[i][j] >= FLAGGED) 47 | cct_setcolor(0, COLOR_HYELLOW); 48 | else 49 | cct_setcolor(0, COLOR_HWHITE); 50 | cout << setw(2) << mat[i][j] % FLAGGED << " "; 51 | } 52 | } 53 | cct_setcolor(0, COLOR_HWHITE); 54 | cout << endl; 55 | } 56 | } 57 | 58 | /*************************************************************************** 59 | 函数名称:game_base_find_recursive 60 | 功 能:命令行版——递归标记 61 | 输入参数: int mat[][GAME_INPUT_COL_MAX] 游戏矩阵 62 | int irow 选中矩阵行数 63 | int icol 选中矩阵列数 64 | int srow 矩阵行数 65 | int scol 矩阵列数 66 | 返 回 值: 67 | 说 明: 68 | ***************************************************************************/ 69 | void game_base_find_recursive(int mat[][GAME_INPUT_COL_MAX], int srow, int scol, int irow, int icol) 70 | { 71 | //Recursive DFS 72 | int cx = irow, cy = icol; 73 | mat[cx][cy] = (mat[cx][cy] % FLAGGED) + FLAGGED; 74 | if (cx - 1 >= 0 && mat[cx - 1][cy] % FLAGGED == mat[cx][cy] % FLAGGED && mat[cx - 1][cy] < FLAGGED) 75 | game_base_find_recursive(mat, srow, scol, cx - 1, cy); 76 | if (cy - 1 >= 0 && mat[cx][cy - 1] % FLAGGED == mat[cx][cy] % FLAGGED && mat[cx][cy - 1] < FLAGGED) 77 | game_base_find_recursive(mat, srow, scol, cx, cy - 1); 78 | if (cx + 1 < srow && mat[cx + 1][cy] % FLAGGED == mat[cx][cy] % FLAGGED && mat[cx + 1][cy] < FLAGGED) 79 | game_base_find_recursive(mat, srow, scol, cx + 1, cy); 80 | if (cy + 1 >= 0 && mat[cx][cy + 1] % FLAGGED == mat[cx][cy] % FLAGGED && mat[cx][cy + 1] < FLAGGED) 81 | game_base_find_recursive(mat, srow, scol, cx, cy + 1); 82 | } 83 | 84 | /*************************************************************************** 85 | 函数名称:game_base_coord_input 86 | 功 能:命令行版选择格子的输入 87 | 输入参数: int mat[][GAME_INPUT_COL_MAX] 游戏矩阵 88 | int* irow 选中矩阵行数 89 | int* icol 选中矩阵列数 90 | int srow 矩阵行数 91 | int scol 矩阵列数 92 | 返 回 值: 93 | 说 明: 94 | ***************************************************************************/ 95 | void game_base_coord_input(int mat[][GAME_INPUT_COL_MAX], int* irow, int* icol, int srow, int scol) 96 | { 97 | int rety = 0, retx = 0, rflag = 1; 98 | char base_input[1000]; 99 | if (irow == NULL || icol == NULL) 100 | return; 101 | while (1) 102 | { 103 | if (!rflag) 104 | { 105 | cct_gotoxy(0, rety); 106 | for (int i = GAME_INPUT_ROW_CLR; i; i--) 107 | cout << " "; 108 | cct_gotoxy(0, rety); 109 | } 110 | rflag = 0; 111 | cct_getxy(retx, rety); 112 | cout << "请以字母+数字形式[例:c2]输入矩阵坐标:"; 113 | bool flag = cin.fail(); 114 | cin >> base_input; 115 | cin.clear(); 116 | cin.ignore(GAME_INPUT_CIN_IGNORE, '\n'); 117 | 118 | if (flag || game_tool_strlen(base_input) < 2) 119 | { 120 | cout << "输入错误,请重新输入."; 121 | continue; 122 | } 123 | base_input[0] = game_tool_uprchr(base_input[0]); 124 | if (base_input[0] < 'A' || base_input[0] >= 'A' + srow || base_input[1] < '0' || base_input[1] >= '0' + scol) 125 | { 126 | cout << "输入错误,请重新输入."; 127 | continue; 128 | } 129 | cout << "输入为" << base_input[0] << "行" << base_input[1] << "列" << endl; 130 | if (!game_tool_check_adjacent(mat, srow, scol, base_input[0] - 'A', base_input[1] - '0')) 131 | { 132 | cout << "输入的矩阵坐标位置处无连续相同值,请重新输入" << endl; 133 | rflag = 1; 134 | continue; 135 | } 136 | break; 137 | } 138 | *irow = base_input[0] - 'A'; 139 | *icol = base_input[1] - '0'; 140 | } 141 | /*************************************************************************** 142 | 函数名称:game_base_finish_check 143 | 功 能:命令行版游戏结束判定(提示输出) 144 | 输入参数: int mat[][GAME_INPUT_COL_MAX] 游戏矩阵 145 | int r 矩阵行数 146 | int c 矩阵列数 147 | 返 回 值:1-游戏结束 0-游戏继续 148 | 说 明: 149 | ***************************************************************************/ 150 | int game_base_finish_check(int mat[][GAME_INPUT_COL_MAX], int r, int c) 151 | { 152 | int flag = game_tool_finish_check(mat, r, c); 153 | if (flag) 154 | { 155 | cct_setcolor(COLOR_HYELLOW, COLOR_HRED); 156 | cout << "无可合并的项,游戏结束!" << endl; 157 | cct_setcolor(); 158 | return 1; 159 | } 160 | return 0; 161 | } 162 | /*************************************************************************** 163 | 函数名称:game_base_ascension 164 | 功 能:命令行版游戏目标提升(提示输出) 165 | 输入参数: int mat[][GAME_INPUT_COL_MAX] 游戏矩阵 166 | int r 矩阵行数 167 | int c 矩阵列数 168 | int* goal 游戏目标 169 | 返 回 值: 170 | 说 明: 171 | ***************************************************************************/ 172 | void game_base_ascension(int mat[][GAME_INPUT_COL_MAX], int r, int c, int* goal) 173 | { 174 | int maxval = game_tool_getmax(mat, r, c); 175 | if (maxval >= *goal) 176 | { 177 | cct_setcolor(COLOR_HYELLOW, COLOR_HRED); 178 | cout << "已经合成到" << maxval << endl; 179 | cct_setcolor(); 180 | game_tool_wait_continue("按回车键继续向更高目标进发..."); 181 | (*goal)++; 182 | } 183 | 184 | } 185 | /*************************************************************************** 186 | 函数名称:game_base_options_action 187 | 功 能:命令行版完成一次合成 188 | 输入参数: int mat[][GAME_INPUT_COL_MAX] 游戏矩阵 189 | int row 矩阵行数 190 | int col 矩阵列数 191 | int irow 当前选中行 192 | int icol 当前选中列 193 | int& score 游戏分数 194 | int& goal 游戏目标 195 | 返 回 值: 196 | 说 明: 197 | ***************************************************************************/ 198 | void game_base_options_action(int opt,int mat[][GAME_INPUT_COL_MAX], int row, int col, int irow, int icol, int& score, int& goal) 199 | { 200 | int tmp; 201 | cout << endl << "相同值归并后的数组(不同色标识):" << endl; 202 | score += (tmp = game_tool_combine(mat, row, col, irow, icol)); 203 | game_base_print_mat(mat, row, col, GAME_BASE_PRINT_COLORED_RESULT); 204 | cout << endl << "本次得分:" << tmp << " 总得分:" << score << " 合成目标:" << goal << endl; 205 | cout << endl; 206 | game_tool_wait_continue("按回车键进行数组下落除0操作..."); 207 | cout << "除0后的数组(不同色标识):" << endl; 208 | game_tool_drop_tiles(mat, row, col, 0); 209 | game_base_print_mat(mat, row, col, GAME_BASE_PRINT_COLORED_RESULT); 210 | cout << endl; 211 | game_tool_wait_continue("按回车键进行新值填充..."); 212 | cout << "新值填充后的数组(不同色标识):" << endl; 213 | game_tool_fill_tiles(mat, row, col,0); 214 | game_base_print_mat(mat, row, col, GAME_BASE_PRINT_COLORED_RESULT); 215 | if (opt == OPT_COMPLETE) 216 | { 217 | cout << endl << "本次合成结束,按回车键继续新一次的合成..." << endl; 218 | game_tool_wait_continue("", 13, 0, 1, 0); 219 | } 220 | } 221 | /*************************************************************************** 222 | 函数名称:game_base_option_print_mat 223 | 功 能:命令行版打印矩阵 224 | 输入参数:int opt 选项号 225 | int mat[][GAME_INPUT_COL_MAX] 游戏矩阵 226 | int& row 矩阵行数 227 | int& col 矩阵列数 228 | int& irow 当前选中行 229 | int& icol 当前选中列 230 | 返 回 值:0 - opt为 OPT_RECURSIVE 或 OPT_ITERATIVE 其他为1 231 | 说 明: 232 | ***************************************************************************/ 233 | int game_base_option_print_mat(int opt, int mat[][GAME_INPUT_COL_MAX], int& row, int& col, int& irow, int& icol) 234 | { 235 | cout << endl << "当前数组:" << endl; 236 | game_base_print_mat(mat, row, col); 237 | cout << endl; 238 | if (game_base_finish_check(mat, row, col)) 239 | return 0; 240 | game_base_coord_input(mat, &irow, &icol, row, col); 241 | cout << endl << "查找结果数组:" << endl; 242 | if (opt != OPT_RECURSIVE) 243 | game_tool_find_iterative(mat, row, irow, icol); 244 | else if (opt == OPT_RECURSIVE) 245 | game_base_find_recursive(mat, row, col, irow, icol); 246 | game_base_print_mat(mat, row, col, GAME_BASE_PRINT_FIND_RESULT); 247 | cout << endl << endl << "当前数组(不同色标识):" << endl; 248 | game_base_print_mat(mat, row, col, GAME_BASE_PRINT_COLORED_RESULT); 249 | 250 | if (opt == OPT_RECURSIVE || opt == OPT_ITERATIVE) 251 | return 0; 252 | return 1; 253 | } 254 | /*************************************************************************** 255 | 函数名称:game_base_options 256 | 功 能:控制台版主逻辑 257 | 输入参数:int opt 选项号 258 | 返 回 值: 259 | 说 明: 260 | ***************************************************************************/ 261 | void game_base_options(int opt) 262 | { 263 | int row, col, irow, icol, score = 0, rx = 0, ry = 0, cht = 0, mat[GAME_INPUT_ROW_MAX][GAME_INPUT_COL_MAX]; 264 | int goal = 990; 265 | game_tool_input(&row, &col, (opt == OPT_COMPLETE || opt==OPT_FIRST_OP ? &goal : NULL)); 266 | game_tool_initial_gen(mat, row, col); 267 | while (1) 268 | { 269 | game_base_ascension(mat, row, col, &goal); 270 | game_tool_remove_flag(mat, row, col); 271 | 272 | if (!game_base_option_print_mat(opt, mat, row, col, irow, icol)) 273 | break; 274 | 275 | if (opt == OPT_FIRST_OP || opt == OPT_COMPLETE) 276 | { 277 | cout << endl << "请确认是否把相邻的相同值合并到" << static_cast(irow + 'A') << icol << "中(Y/N/Q)"; 278 | cct_getxy(rx, ry); 279 | while (1) 280 | { 281 | cht = game_tool_uprchr(static_cast(game_tool_getch())); 282 | if (cht != 'Y' && cht != 'N' && cht != 'Q') 283 | continue; 284 | break; 285 | } 286 | cout << static_cast(cht) << endl; 287 | if (cht == 'Y') 288 | game_base_options_action(opt, mat, row, col, irow, icol, score, goal); 289 | if (opt != OPT_COMPLETE || cht == 'Q') 290 | break; 291 | if (cht == 'N') 292 | continue; 293 | } 294 | } 295 | cout << endl; 296 | game_tool_input_end(); 297 | } -------------------------------------------------------------------------------- /W_SynthesisTen/90-b2-main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "cmd_console_tools.h" 8 | 9 | #include "90-b2.h" 10 | using namespace std; 11 | /*************************************************************************** 12 | 函数名称:int game_menu_print() 13 | 功 能:打印菜单 14 | 输入参数: 15 | 返 回 值: 16 | 说 明: 17 | ***************************************************************************/ 18 | int game_menu_print() 19 | { 20 | const char* menuOpts[GAME_MENU_OPTS][2] = 21 | { 22 | {"命令行找出可合成项并标识(非递归)","1"}, 23 | {"命令行找出可合成项并标识(递归)","2"}, 24 | {"命令行完成一次合成(分步骤显示)","3"}, 25 | {"命令行完整版(分步骤显示)","4"}, 26 | {"伪图形界面显示初始数组(无分隔线)","5"}, 27 | {"伪图形界面显示初始数组(有分隔线)","6"}, 28 | {"伪图形界面下用箭头键选择当前色块","7"}, 29 | {"伪图形界面完成一次合成(分步骤)","8"}, 30 | {"伪图形界面完整版","9"}, 31 | {"退出","0"}, 32 | }; 33 | cct_cls(); 34 | cout << "-----------------------------------" << endl; 35 | for (int i = 0; i < GAME_MENU_OPTS; i++) 36 | cout << menuOpts[i][1] << "." << menuOpts[i][0] << endl; 37 | cout << "----------------------------------- " << endl; 38 | int ch = 0; 39 | cout << "[请选择0-9]"; 40 | while (1) 41 | { 42 | ch = _getch(); 43 | if (ch >= '0' && ch <= '9') 44 | break; 45 | if (ch == 0xE0 || ch == 0x00) 46 | ch = _getch(); 47 | } 48 | cout << static_cast(ch); 49 | return ch - '0'; 50 | } 51 | 52 | int main() 53 | { 54 | srand(static_cast(time(0))); 55 | int old_wincol, old_winrow, old_bufcol, old_bufrow; 56 | cct_setcolor(); 57 | cct_getconsoleborder(old_wincol, old_winrow, old_bufcol, old_bufrow); 58 | int option = 0; 59 | while (1) 60 | { 61 | cct_setcursor(CURSOR_VISIBLE_NORMAL); 62 | cct_setconsoleborder(80, 25, -1, old_bufrow); 63 | option = game_menu_print(); 64 | if (!option) 65 | break; 66 | switch (option) 67 | { 68 | case 1: 69 | game_base_options(OPT_ITERATIVE); 70 | break; 71 | case 2: 72 | game_base_options(OPT_RECURSIVE); 73 | break; 74 | case 3: 75 | game_base_options(OPT_FIRST_OP); 76 | break; 77 | case 4: 78 | game_base_options(OPT_COMPLETE); 79 | break; 80 | case 5: 81 | game_con_options(OPT_CON_DRAW1); 82 | break; 83 | case 6: 84 | game_con_options(OPT_CON_DRAW2); 85 | break; 86 | case 7: 87 | game_con_options(OPT_CON_FIRST); 88 | break; 89 | case 8: 90 | game_con_options(OPT_CON_SINGLE); 91 | break; 92 | case 9: 93 | game_con_options(OPT_CON_FULL); 94 | break; 95 | } 96 | } 97 | cct_gotoxy(0, 23); 98 | cout << "请按任意键继续. . ."; 99 | game_tool_getch(); 100 | return 0; 101 | } -------------------------------------------------------------------------------- /W_SynthesisTen/90-b2.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define CONSOLE_W 80 //控制台宽度 4 | 5 | #define GAME_INPUT_ROW_MAX 8 //矩阵最大行数 6 | #define GAME_INPUT_ROW_MIN 3 //矩阵最小行数 7 | 8 | #define GAME_INPUT_COL_MAX 10 //矩阵最大列数 9 | #define GAME_INPUT_COL_MIN 3 //矩阵最小列数 10 | 11 | #define GAME_INPUT_GOAL_MIN 5 //最小初始游戏目标 12 | #define GAME_INPUT_GOAL_MAX 20 //最大游戏初始目标 13 | 14 | #define GAME_INPUT_ROW_CLR 80 //行清空字符数 15 | #define GAME_INPUT_CIN_IGNORE 0x7fffffff //cin忽略字符数 16 | 17 | #define GAME_DELAY_BORDER_DRW 20 //边框绘制时间间隔 18 | #define GAME_DELAY 30 //游戏时间间隔 19 | 20 | #define GAME_BASE_PRINT_DFFAULT 0 //控制台方式:默认打印方式 21 | #define GAME_BASE_PRINT_FIND_RESULT 1 //控制台方式:标出查找结果 22 | #define GAME_BASE_PRINT_COLORED_RESULT 2 //控制台方式:高亮查找结果 23 | 24 | #define GAME_CON_CW 3 //图形版:格子宽度 25 | #define GAME_CON_CH 3 //图形版:格子高度 26 | 27 | #define GAME_CON_LEFT 0 //图形版:按下左箭头 28 | #define GAME_CON_RIGHT 1 //图形版:按下右箭头 29 | #define GAME_CON_UP 2 //图形版:按下上箭头 30 | #define GAME_CON_DOWN 3 //图形版:按下下箭头 31 | #define GAME_CON_ENTER 4 //图形版:按下回车 32 | #define GAME_CON_QUIT 5 //图形版:退出 33 | 34 | #define GAME_CON_BLINK_CNT 4 //图像版:格子闪烁次数 35 | #define GAME_CON_BLINK_DELAY 100 //图形版:格子闪烁间隔 36 | #define GAME_CON_DROP_DELAY 20 //图形版:下落间隔 37 | #define GAME_CON_FILL_DELAY 60 //图形版:填充间隔 38 | #define GAME_MENU_OPTS 10 //菜单选项数 39 | #define GAME_CELLWEIGHT 3 //格子分数权重 40 | #define OPT_ITERATIVE 1 //选项1:非递归 41 | #define OPT_RECURSIVE 2 //选项2:递归 42 | #define OPT_FIRST_OP 3 //选项3:第一次操作 43 | #define OPT_COMPLETE 4 //选项4:控制台完整 44 | #define OPT_CON_DRAW1 5 //选项5:绘制边框无间隔 45 | #define OPT_CON_DRAW2 6 //选项6:绘制边框带间隔 46 | #define OPT_CON_FIRST 7 //选项7:图形版选择格子 47 | #define OPT_CON_SINGLE 8 //选项8:图形版单次合成 48 | #define OPT_CON_FULL 9 //选项9:图形版完整版 49 | 50 | #define OPT_SEL_HSELECT 2 //图形版:高亮选中块 51 | #define OPT_SEL_SELECT 1 //图形版:高亮标记块 52 | #define OPT_SEL_DEFAULT 0 //图形版:不高亮 53 | 54 | #define OPT_DRW_HIGHLIGHT 1 //图形版绘制:高亮 55 | #define OPT_DRW_ALL 0 //图形版绘制:所有格子 56 | #define OPT_DRW_FADE 2 //图形版绘制:取消高亮块 57 | 58 | #define FLAGGED 1000 //格子标记 59 | 60 | 61 | void game_base_options(int opt); 62 | 63 | void game_con_options(int type); 64 | 65 | void game_tool_input(int* irow, int* icol, int* igoal); 66 | void game_tool_initial_gen(int mat[][GAME_INPUT_COL_MAX], int r, int c); 67 | int game_tool_strlen(const char* s); 68 | char game_tool_uprchr(char x); 69 | void game_tool_swap(int* x, int* y); 70 | int game_tool_rand_gen(int maxval); 71 | int game_tool_getmax(int mat[][GAME_INPUT_COL_MAX], int r, int c); 72 | int game_tool_getch(); 73 | void game_tool_wait_continue(const char* t, int key = 13, int skip = 0, int clr = 1,int req_endl=1); 74 | void game_tool_input_end(); 75 | void game_tool_find_iterative(int mat[][GAME_INPUT_COL_MAX], int srow, int irow, int icol); 76 | int game_tool_combine(int mat[][GAME_INPUT_COL_MAX], int srow, int scol, int irow, int icol); 77 | void game_tool_remove_flag(int mat[][GAME_INPUT_COL_MAX], int srow, int scol); 78 | int game_tool_check_adjacent(int mat[][GAME_INPUT_COL_MAX], int r, int c, int cr, int cc); 79 | int game_tool_finish_check(int mat[][GAME_INPUT_COL_MAX], int r, int c); 80 | 81 | void game_tool_fill_tiles(int mat[][GAME_INPUT_COL_MAX], int r, int c, int isConsole); 82 | void game_tool_drop_tiles(int mat[][GAME_INPUT_COL_MAX], int r, int c, int isConsole); 83 | 84 | //为尽可能实现重用加入的声明 85 | void game_con_draw_tile(int val, int cx, int cy, int type = OPT_SEL_DEFAULT); -------------------------------------------------------------------------------- /W_SynthesisTen/cmd_console_tools.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aeroraven/Cpp-Course-Assignment/be3b33ffa47ae2ce66c0782797779031249f5e8a/W_SynthesisTen/cmd_console_tools.h -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # 高级语言程序设计课程作业 2 | 3 | 同济大学高级语言程序设计(Shen Jian,沈坚)课程作业 4 | 5 | ## 注意事项 / Note 6 | 7 | 2021版本教师发布的Demo程序存在错误,已发现教师给出存在Bug的Demo程序包括: 8 | 9 | - 秒数转日期 (整数溢出) 10 | - 多边形面积 (实现算法存在较大浮点精度误差) 11 | - 正确的做法请参见:https://oi-wiki.org/geometry/2d/ 12 | 13 | 对于上述程序,如果你发现本仓库代码执行结果和Demo不一致,则是教师自身的问题。 14 | 15 | ## 协议 / License 16 | 17 | AGPLv3 或者 CC-BY-NC-SA 4.0 18 | 19 | 对于交作业用途就随便用就行,不按上方协议约束。避免查重可以替换点变量名之类的; 在LLM的时代,希望大家能把这些时间能省下来,自由地去做更有意义的事情,而不是做被调教好的人形的`clang-format`(代码格式化工具) 20 | 21 | --------------------------------------------------------------------------------