Blue Bridge Mug-palindrome number (string!!) )

Source: Internet
Author: User

Problem description Observation Number: 12321,123321 all have a common feature, which is the same whether reading from left to right or from right to left. Such numbers are called: palindrome numbers.

The subject asks you to find some 5-bit or 6-bit decimal digits. Meet the following requirements:
The sum of the individual digits of the number equals the integer entered. The input format is a positive integer n (10<n<100), which indicates the number of digits and that are required to be satisfied. The output formats several lines, each containing a 5-bit or 6-bit integer that satisfies the requirement.
Numbers are arranged in order from small to large.
If no conditions are met, output: 1 Sample Input 44 sample output 99899
499994
589985
598895
679976
688886
697796
769967
778877
787787
796697
859958
868868
877778
886688
895598
949949
958859
967769
976679
985589
994499 Sample Input 60 sample output-1


Idea: First the palindrome sequence of the table hit well, calculated exactly 1800, so I array open 2000

Then sweep the front and back, and then the output will meet the conditions.


AC Code:

[CPP]View Plaincopyprint?
    1. #include <cstdio>
    2. #include <cstring>
    3. #include <algorithm>
    4. Using namespace std;
    5. const INT MAXN = 2000;
    6. int PALIN[MAXN], num = 0;
    7. int Is_palin (int n) //Determine if it is a palindrome number
    8. {
    9. if (n<100000)
    10. {
    11. if ((n/10000 = = n%10) && (n/1000%10 = = n/10%10))
    12. return 1;
    13. }
    14. Else
    15. {
    16. if (n/100000 = = n%10) && (n/10000%10 = = n/10%10) && (n/1000%10 = n/100%10))
    17. return 1;
    18. }
    19. return 0;
    20. }
    21. void init () //palindrome number playing table
    22. {
    23. For (int i=10000; i<=999999; i++)
    24. {
    25. if (Is_palin (i)) palin[num++] = i;
    26. }
    27. }
    28. int fun (int n) //Determine if the condition is met
    29. {
    30. int t = 0, m = n;
    31. While (m)
    32. {
    33. T + = m%10;
    34. M/= 10;
    35. }
    36. return t;
    37. }
    38. int main ()
    39. {
    40. Init ();
    41. int n;
    42. While (scanf ("%d", &n)! = EOF)
    43. {
    44. if (N < 5 | | n>54) {printf (" -1\n");  continue;}
    45. For (int i=0; i<num; i++)
    46. {
    47. if (fun (palin[i]) = = N) printf ("%d\n", Palin[i]);
    48. }
    49. }
    50. return 0;
    51. }

Blue Bridge Mug-palindrome number (string!!) )

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.