Number of input files for algorithm training, and number of input files for algorithm training

Source: Internet
Author: User

Number of input files for algorithm training, and number of input files for algorithm training
If the number of reads from left to right is the same as that from right to left, we call it the number of replies.
For example, if a decimal number of 56 is given and 56 is added to 65 (that is, 56 is read from the right to the left), 121 is a return number.

Another example: for the 10-digit number 87:
Step 1: 87 + 78 = 165 Step 2: 165 + 561 = 726
Step 3: 726 + 627 = 1353 Step 4: 1353 + 3531 = 4884

One step here refers to the addition of an N-base. In the above example, we use at least four steps to get a retrieval Number of 4884.

Write a program, given a N (2 <= N <= 10 or N = 16) hexadecimal number M (where the hexadecimal number is 0-9 and A-F ), the number of replies can be obtained after at least a few steps.
If the number of replies cannot be obtained within 30 steps (including 30 steps), "Impossible!" Is output !" The input format is two rows. If the N and M output formats are less than 30 steps, the output "STEP = xx" (without quotation marks), where xx is the number of steps; otherwise, an "Impossible!" line is output!" (Without quotation marks) Example 9
87 sample output STEP = 6 Analysis:N = 16 and 2 <= N <= 10,Str receives the specified string

It is set to determine whether str is a return string within 30 cycles. If not, the reverse addition of str and str will be given to str1 first,
Then the str is assigned back.

# Include <stdio. h> # include <string. h> # include <stdlib. h> # define M 30 char str [M], str1 [M]; int len; int Ispalindrome () // judge the number of input records {int I; for (I = 0; I <len/2; I ++) if (str [I]! = Str [len-i-1]) return 0; return 1;} int main () {int n, I, j, t; int t1, flag; char temp [2] = {'\ 0'}; scanf ("% d % s", & n, & str); len = strlen (str); j = 0; flag = 0; if (n = 16) {for (t = 0; t <30; t ++) // obtain the number of replies within 30 steps {if (! Ispalindrome () {for (I = 0; I <len; I ++) {t1 = 0; if (str [I]> = '0' & str [I] <= '9') t1 + = (str [I]-'0 '); else t1 + = (str [I]-'A' + 10); if (str [len-i-1] >='0' & str [len-i-1] <= '9 ') t1 + = (str [len-i-1]-'0'); else t1 + = (str [len-i-1]-'A' + 10); t1 + = flag; if (t1> = n) // to carry {if (t1-n <= 9) itoa (t1-n, temp, 10 ); // convert the integer type to else temp type [0] = 'A' + (t1-n-10); flag = 1; str1 [j ++] = temp [0];} else {if (t1 <= 9) it Oa (t1, temp, 10); // The integer type is converted to plain else temp [0] = 'A' + (t1-10); flag = 0; str1 [j ++] = temp [0] ;}} if (flag) {str1 [j] = '1'; str1 [j + 1] = '\ 0 '; flag = 0; // set 0} else str1 [j] = '\ 0'; strcpy (str, str1 ); // assign str1 to str len = strlen (str); j = 0;} else {printf ("STEP = % d \ n", t); break ;}} if (t = 30) printf ("Impossible! \ N ") ;}else {for (t = 0; t <30; t ++) // obtain the number of replies within 30 steps {if (! Ispalindrome () {for (I = 0; I <len; I ++) {t1 = (str [I]-'0 ') + (str [len-i-1]-'0') + flag; // to add flag if (t1> = n) // to carry {itoa (t1-n, temp, 10 ); // The integer type is converted to the portable flag = 1; str1 [j ++] = temp [0];} else {itoa (t1, temp, 10 ); // The integer type is converted to the portable flag = 0; str1 [j ++] = temp [0] ;}} if (flag) {str1 [j] = '1 '; str1 [j + 1] = '\ 0'; flag = 0; // set 0} else str1 [j] =' \ 0'; strcpy (str, str1 ); // assign str1 to str j = 0; len = strlen (str);} else {printf ("STEP = % d \ n", t); break ;}} if (t = 30) printf ("Impossible! \ N ") ;}return 0 ;}

 

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.