fork download
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <map>
  5.  
  6.  
  7. void getdigits(char buf[], int n)
  8. {
  9. while (n) {
  10. *buf++ = n % 10;
  11. n /= 10;
  12. }
  13. }
  14.  
  15. int compint(const void *a, const void *b)
  16. {
  17. return *(const char *)a - *(const char *)b;
  18. }
  19.  
  20. int is_vampire(char n[], const char i[], const char j[], size_t l)
  21. {
  22. char k[2 * l];
  23. std::memcpy(k + 0, i, l);
  24. std::memcpy(k + l, j, l);
  25. std::qsort(k, 2 * l, 1, compint);
  26. std::qsort(n, 2 * l, 1, compint);
  27. return !std::memcmp(n, k, sizeof(k));
  28. }
  29.  
  30.  
  31.  
  32. int main()
  33. {
  34. std::map<int, std::pair<int, int> > m;
  35.  
  36. for (int i = 100; i < 1000; i++) {
  37. for (int j = 100; j < 1000; j++) {
  38. int n = i * j;
  39. if (n < 100000 || n >= 1000000)
  40. continue;
  41.  
  42. char ndigits[6];
  43. getdigits(ndigits, n);
  44.  
  45. char idigits[3];
  46. char jdigits[3];
  47. getdigits(idigits, i);
  48. getdigits(jdigits, j);
  49.  
  50. if ((i % 10 || j % 10) && is_vampire(ndigits, idigits, jdigits, 3)) {
  51. m[n] = std::make_pair(i, j);
  52. }
  53. }
  54. }
  55.  
  56.  
  57. std::cout << m.size() << " vampire numbers found:" << std::endl;
  58.  
  59. for (std::map<int, std::pair<int, int> >::iterator it = m.begin(); it != m.end(); it++)
  60. std::cout << it->first << " = " << it->second.first << " * " << it->second.second << std::endl;
  61.  
  62. return 0;
  63. }
Success #stdin #stdout 0.63s 2988KB
stdin
Standard input is empty
stdout
148 vampire numbers found:
102510 = 510 * 201
104260 = 401 * 260
105210 = 501 * 210
105264 = 516 * 204
105750 = 705 * 150
108135 = 801 * 135
110758 = 701 * 158
115672 = 761 * 152
116725 = 725 * 161
117067 = 701 * 167
118440 = 840 * 141
120600 = 600 * 201
123354 = 534 * 231
124483 = 443 * 281
125248 = 824 * 152
125433 = 543 * 231
125460 = 615 * 204
125500 = 500 * 251
126027 = 627 * 201
126846 = 486 * 261
129640 = 926 * 140
129775 = 725 * 179
131242 = 422 * 311
132430 = 410 * 323
133245 = 423 * 315
134725 = 425 * 317
135828 = 588 * 231
135837 = 387 * 351
136525 = 635 * 215
136948 = 938 * 146
140350 = 401 * 350
145314 = 414 * 351
146137 = 461 * 317
146952 = 942 * 156
150300 = 501 * 300
152608 = 608 * 251
152685 = 585 * 261
153436 = 431 * 356
156240 = 651 * 240
156289 = 581 * 269
156915 = 951 * 165
162976 = 926 * 176
163944 = 414 * 396
172822 = 782 * 221
173250 = 750 * 231
174370 = 470 * 371
175329 = 759 * 231
180225 = 801 * 225
180297 = 897 * 201
182250 = 810 * 225
182650 = 650 * 281
186624 = 864 * 216
190260 = 906 * 210
192150 = 915 * 210
193257 = 591 * 327
193945 = 491 * 395
197725 = 719 * 275
201852 = 801 * 252
205785 = 807 * 255
211896 = 981 * 216
213466 = 626 * 341
215860 = 860 * 251
216733 = 671 * 323
217638 = 678 * 321
218488 = 881 * 248
226498 = 842 * 269
226872 = 822 * 276
229648 = 926 * 248
233896 = 692 * 338
241564 = 524 * 461
245182 = 581 * 422
251896 = 851 * 296
253750 = 725 * 350
254740 = 542 * 470
260338 = 806 * 323
262984 = 926 * 284
263074 = 602 * 437
284598 = 582 * 489
284760 = 678 * 420
286416 = 612 * 468
296320 = 926 * 320
304717 = 707 * 431
312475 = 725 * 431
312975 = 975 * 321
315594 = 591 * 534
315900 = 900 * 351
319059 = 909 * 351
319536 = 951 * 336
326452 = 623 * 524
329346 = 963 * 342
329656 = 926 * 356
336550 = 635 * 530
336960 = 936 * 360
338296 = 863 * 392
341653 = 641 * 533
346968 = 948 * 366
361989 = 981 * 369
362992 = 926 * 392
365638 = 686 * 533
368550 = 630 * 585
369189 = 969 * 381
371893 = 971 * 383
378400 = 800 * 473
378418 = 878 * 431
378450 = 870 * 435
384912 = 891 * 432
386415 = 831 * 465
392566 = 662 * 593
404968 = 908 * 446
414895 = 845 * 491
416650 = 650 * 641
416988 = 891 * 468
428980 = 890 * 482
429664 = 926 * 464
447916 = 941 * 476
456840 = 846 * 540
457600 = 704 * 650
458640 = 840 * 546
475380 = 834 * 570
486720 = 780 * 624
489159 = 891 * 549
489955 = 899 * 545
498550 = 845 * 590
516879 = 759 * 681
529672 = 926 * 572
536539 = 953 * 563
538650 = 855 * 630
559188 = 951 * 588
567648 = 864 * 657
568750 = 875 * 650
629680 = 926 * 680
638950 = 983 * 650
673920 = 936 * 720
679500 = 906 * 750
729688 = 926 * 788
736695 = 963 * 765
738468 = 876 * 843
769792 = 992 * 776
789250 = 902 * 875
789525 = 957 * 825
792585 = 927 * 855
794088 = 984 * 807
809919 = 909 * 891
809964 = 906 * 894
815958 = 951 * 858
829696 = 926 * 896
841995 = 945 * 891
939658 = 986 * 953