main.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <iostream>
  2. using namespace std;
  3. int calc(char *num) {
  4. int result = 0;
  5. for (int i = 0; num[i] != '\0'; i++) {
  6. if (num[i] >= '0' && num[i] <= '9') {
  7. result += num[i] - '0';
  8. } else if (num[i] >= 'A' && num[i] <= 'F') {
  9. result += num[i] - 'A' + 10;
  10. }
  11. }
  12. return result;
  13. }
  14. int duodecimal(int i) {
  15. char ch[20] = {'\0'};
  16. int result = 0;
  17. int count = 0, base = 12;
  18. for (; i != 0;) {
  19. int s = i % base;
  20. result += s;
  21. if (s >= 10) {
  22. ch[count] = 'A' + (s - 10);
  23. } else {
  24. ch[count] = '0' + s;
  25. }
  26. count++;
  27. i /= base;
  28. }
  29. // cout << ch << " - " << i << endl;
  30. return result;
  31. }
  32. int main() {
  33. char *charA = new char[12], *charB = new char[12];
  34. memset(charA, 0, 12);
  35. memset(charB, 0, 12);
  36. int numA = 0, numB = 0, numC = 0;
  37. for (int i = 1000; i <= 9999; i++) {
  38. sprintf(charA, "%X", i);
  39. sprintf(charB, "%d", i);
  40. numA = calc(charA);
  41. numB = calc(charB);
  42. // cout << charA << " - " << numA << " - " << charB << " - " << numB << endl;
  43. memset(charA, 0, 12);
  44. memset(charB, 0, 12);
  45. if (numA != numB) {
  46. continue;
  47. }
  48. numC = duodecimal(i);
  49. if (numA != numC) {
  50. continue;
  51. }
  52. cout << i << endl;
  53. }
  54. free(charA);
  55. free(charB);
  56. }