2.2.3 Runaround Numbers

Source: Internet
Author: User

There's nothing to say about forced search. See the code for specific details. The code is as follows:
  
 
  1. /*
  2. ID: awsd1231
  3. PROG: runround
  4. LANG: C++
  5. */
  6. #include<iostream>
  7. #include<cstdio>
  8. #include<string>
  9. #include<cstring>
  10. using namespace std;
  11. unsigned long M;
  12. bool check(unsigned long x) {
  13. char buf[10];
  14. sprintf(buf, "%u", x);
  15. int ans[10][2] = {0};
  16. Span class= "Typ" >int len = strlen ( buf
  17. Span class= "KWD" >for ( int i = 0 I != Len - 1 ; ++ i ) {
  18. Span class= "KWD" >for ( int J = i + 1 ; J != Len ++ j ) {
  19. if (buf[i] == buf[j] || !buf[j]) {
  20. return false;
  21. }
  22. }
  23. }
  24. Span class= "KWD" >for ( int i = 0 I != Len ++ i ) {
  25. ans[i][0] = buf[i] - ‘0‘;
  26. }
  27. int idx = 0;
  28. if (ans[idx][0] >= len) {
  29. idx = ans[idx][0] % len;
  30. }
  31. else idx = ans[idx][0];
  32. Span class= "KWD" >for ( int i = 0 I != Len ++ i ) {
  33. if (ans[idx][1]) return false;
  34. ans[idx][1] = 1;
  35. Span class= "KWD" >if ( idx + ans [ idx ][ 0 ] >= len ) {
  36. idx = (idx + ans[idx][0]) % len;
  37. }
  38. else
  39. idx += ans[idx][0];
  40. }
  41. return true;
  42. }
  43. int main () {
  44. freopen("runround.in", "r", stdin);
  45. freopen("runround.out", "w", stdout);
  46. cin >> M;
  47. Span class= "KWD" >for ( unsigned long i = Span class= "PLN" > M + 1 ; ++ i ) {
  48. if (i % 10 && check(i)) {
  49. cout << i << endl;
  50. break;
  51. }
  52. }
  53. return 0;
  54. }



From for notes (Wiz)

2.2.3 Runaround Numbers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.