fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. int GetTop(int N){
  6. int R = 0;
  7. while (N > 0){
  8. R = N % 10;
  9. N /= 10;
  10. }
  11.  
  12. return R;
  13. }
  14. int GetTail(int N){
  15. return N % 10;
  16. }
  17. int GetDigit(int N){
  18. int R = 0;
  19. while (N > 0){
  20. N /= 10;
  21. R++;
  22. }
  23.  
  24. return R;
  25. }
  26. /* * /
  27. std::size_t SearchNDown(std::vector<int> vec, int N,int WO=1){
  28. for (std::size_t i = vec.size() - 1; i > 0; i--){
  29. if (vec[i] == 1) continue;
  30. if (GetTail(N) == GetTop(i)){
  31. //if (GetTail(i) == GetTop(WO) && GetDigit(i) > 1) continue;
  32. return i;
  33. }
  34. }
  35.  
  36. return 0;
  37. }
  38. /* */
  39. std::size_t SearchNUp(std::vector<int> vec, int N){
  40. for (std::size_t i = 0; i < vec.size(); i++){
  41. if (vec[i] == 1) continue;
  42. if (GetTail(N) == GetTop(i)){
  43. return i;
  44. }
  45. }
  46.  
  47. return 0;
  48. }
  49. std::vector<int> Siritori(std::vector<int> vec,std::size_t Start = 1){
  50. std::size_t S = Start;
  51. std::vector<int> ret;
  52. bool F = true;
  53.  
  54. vec[S] = 1;
  55. ret.push_back(S);
  56.  
  57. while (S != 0){
  58. S = SearchNUp(vec, S);
  59. vec[S] = 1;
  60. ret.push_back(S);
  61.  
  62. }
  63. //>-bad hack-<
  64. ret.pop_back();
  65.  
  66. std::rotate(ret.begin(), ret.begin() + 2, ret.end());
  67. S = SearchNUp(vec, ret.back());
  68. if (vec[S] == 0) ret.push_back(S);
  69. vec[S] = 1;
  70.  
  71. //>---------<
  72.  
  73. std::cout << "NotUse:";
  74. for (std::size_t i = 0; i < vec.size(); i++){
  75. if (vec[i] == 0) std::cout << i << ',';
  76. }
  77.  
  78. return ret;
  79.  
  80. }
  81.  
  82. std::vector<int> Mondai(int N){
  83. std::vector<int> vec(N+1);
  84. for (std::size_t j = 0; j < vec.size(); j++){
  85. if (GetTail(j) == 0) vec[j] = 1;
  86. }
  87.  
  88. return vec;
  89. }
  90.  
  91. int main(){
  92.  
  93. auto R = Mondai(1001);
  94. int X = R.size()-1;
  95. X = 9;
  96. if (GetTail(X) == 0) X--;
  97. R = Siritori(R,X);
  98.  
  99. std::cout <<std::endl<<"result:";
  100. for (auto& o : R) std::cout << o << ',';
  101.  
  102. return 0;
  103.  
  104.  
  105. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
NotUse:
result:1,11,12,2,21,13,3,31,14,4,41,15,5,51,16,6,61,17,7,71,18,8,81,19,92,22,23,32,24,42,25,52,26,62,27,72,28,82,29,93,33,34,43,35,53,36,63,37,73,38,83,39,94,44,45,54,46,64,47,74,48,84,49,95,55,56,65,57,75,58,85,59,96,66,67,76,68,86,69,97,77,78,87,79,98,88,89,99,901,101,102,201,103,301,104,401,105,501,106,601,107,701,108,801,109,902,202,203,302,204,402,205,502,206,602,207,702,208,802,209,903,303,304,403,305,503,306,603,307,703,308,803,309,904,404,405,504,406,604,407,704,408,804,409,905,505,506,605,507,705,508,805,509,906,606,607,706,608,806,609,907,707,708,807,709,908,808,809,909,911,111,112,211,113,311,114,411,115,511,116,611,117,711,118,811,119,912,212,213,312,214,412,215,512,216,612,217,712,218,812,219,913,313,314,413,315,513,316,613,317,713,318,813,319,914,414,415,514,416,614,417,714,418,814,419,915,515,516,615,517,715,518,815,519,916,616,617,716,618,816,619,917,717,718,817,719,918,818,819,919,921,121,122,221,123,321,124,421,125,521,126,621,127,721,128,821,129,922,222,223,322,224,422,225,522,226,622,227,722,228,822,229,923,323,324,423,325,523,326,623,327,723,328,823,329,924,424,425,524,426,624,427,724,428,824,429,925,525,526,625,527,725,528,825,529,926,626,627,726,628,826,629,927,727,728,827,729,928,828,829,929,931,131,132,231,133,331,134,431,135,531,136,631,137,731,138,831,139,932,232,233,332,234,432,235,532,236,632,237,732,238,832,239,933,333,334,433,335,533,336,633,337,733,338,833,339,934,434,435,534,436,634,437,734,438,834,439,935,535,536,635,537,735,538,835,539,936,636,637,736,638,836,639,937,737,738,837,739,938,838,839,939,941,141,142,241,143,341,144,441,145,541,146,641,147,741,148,841,149,942,242,243,342,244,442,245,542,246,642,247,742,248,842,249,943,343,344,443,345,543,346,643,347,743,348,843,349,944,444,445,544,446,644,447,744,448,844,449,945,545,546,645,547,745,548,845,549,946,646,647,746,648,846,649,947,747,748,847,749,948,848,849,949,951,151,152,251,153,351,154,451,155,551,156,651,157,751,158,851,159,952,252,253,352,254,452,255,552,256,652,257,752,258,852,259,953,353,354,453,355,553,356,653,357,753,358,853,359,954,454,455,554,456,654,457,754,458,854,459,955,555,556,655,557,755,558,855,559,956,656,657,756,658,856,659,957,757,758,857,759,958,858,859,959,961,161,162,261,163,361,164,461,165,561,166,661,167,761,168,861,169,962,262,263,362,264,462,265,562,266,662,267,762,268,862,269,963,363,364,463,365,563,366,663,367,763,368,863,369,964,464,465,564,466,664,467,764,468,864,469,965,565,566,665,567,765,568,865,569,966,666,667,766,668,866,669,967,767,768,867,769,968,868,869,969,971,171,172,271,173,371,174,471,175,571,176,671,177,771,178,871,179,972,272,273,372,274,472,275,572,276,672,277,772,278,872,279,973,373,374,473,375,573,376,673,377,773,378,873,379,974,474,475,574,476,674,477,774,478,874,479,975,575,576,675,577,775,578,875,579,976,676,677,776,678,876,679,977,777,778,877,779,978,878,879,979,981,181,182,281,183,381,184,481,185,581,186,681,187,781,188,881,189,982,282,283,382,284,482,285,582,286,682,287,782,288,882,289,983,383,384,483,385,583,386,683,387,783,388,883,389,984,484,485,584,486,684,487,784,488,884,489,985,585,586,685,587,785,588,885,589,986,686,687,786,688,886,689,987,787,788,887,789,988,888,889,989,991,191,192,291,193,391,194,491,195,591,196,691,197,791,198,891,199,992,292,293,392,294,492,295,592,296,692,297,792,298,892,299,993,393,394,493,395,593,396,693,397,793,398,893,399,994,494,495,594,496,694,497,794,498,894,499,995,595,596,695,597,795,598,895,599,996,696,697,796,698,896,699,997,797,798,897,799,998,898,899,999,9,91,1001,