image.png

    1. #include <iostream>
    2. #include <fstream>
    3. #include <cassert>
    4. #include <vector>
    5. #include "queue"
    6. #include "json/json.h" // 直接就可以include,无需额外配置
    7. #include "boost/uuid/uuid_generators.hpp"
    8. #include "boost/lexical_cast.hpp"
    9. #include "boost/uuid/uuid_io.hpp"
    10. using namespace std;
    11. string unite[5] = {"", "s", "b", "q", "w"};
    12. string num[10] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
    13. string convert_sw(int a) {
    14. int idx = 0;
    15. int atemp = a; // 设定a的临时存储值,防止每次进入循环都进行末尾零的判断
    16. string str, result;
    17. while (a != 0) {
    18. while (atemp % 10 == 0) {
    19. idx++;
    20. atemp /= 10;
    21. a /= 10;
    22. }
    23. int last = a % 10;
    24. if (last != 0) {
    25. str = num[last] + unite[idx];
    26. } else {
    27. str = "0";
    28. if (!result.empty() && result[0] == '0') {
    29. // case: num:608008 want:6sw08q08 get:6sw08q008
    30. str = "";
    31. }
    32. }
    33. result = str + result;
    34. a /= 10;
    35. idx++;
    36. }
    37. return result;
    38. }
    39. string convert(int a) {
    40. string result, temp;
    41. if (a < 100000) {
    42. result = convert_sw(a);
    43. } else {
    44. temp = convert_sw(a / 10000);
    45. if (a / 10000 % 10 == 0) {
    46. result = temp + "w0" + convert_sw(a - a / 10000 * 10000);
    47. } else {
    48. result = temp + "w" + convert_sw(a - a / 10000 * 10000);
    49. }
    50. }
    51. // 处理 11 不是 s1 而是 1s1 的case
    52. // 处理 110000 不是 s1w 而是 1s1w 的case
    53. if (result.size() > 2 && result[0] == '1' && result[1] == 's') {
    54. return result.substr(1, result.size() - 1);
    55. } else {
    56. return result;
    57. }
    58. }
    59. // 11 s1
    60. // 302 3b02
    61. // 57049 5w7q04s9
    62. // 600080 6sw08s
    63. // 32000523 3q2bw05b2s3
    64. // 12345678 1q2b3s4w5q6b7s8
    65. int main() {
    66. vector<int> nums = {60808080, 608008, 120000, 11, 302, 57049, 600080, 32000523, 12345678};
    67. vector<string> want = {"6q08sw08q08s", "6sw08q08", "s2w", "s1", "3b02", "5w7q04s9", "6sw08s", "3q2bw05b2s3",
    68. "1q2b3s4w5q6b7s8"};
    69. for (int i = 0; i < nums.size(); i++) {
    70. string get = convert(nums[i]);
    71. cout << "num:" << nums[i]
    72. << "\twant:" << want[i]
    73. << "\tget:" << get
    74. << "\tans:" << (get == want[i])
    75. << endl;
    76. }
    77. return 0;
    78. }