C language programming of 100 cases (6): Reverse Digital

Example 6 digit reversal

Title Description

Given an integer, the number of the digital please obtained by inverting each bit of a new number. The new number should also meet common form of integers that, unless given the original number is zero, otherwise obtained by inverting the highest digit of the new number should not be zero (see sample 2).

Input Format

An integer N

Output Format

An integer that represents the number of new reversal after.

Input Sample # 1

123

Sample Output # 1

321

Input Sample # 2

-380

Output Sample # 2

-83

        (1) programming ideas.

        A non-negative integer number you turn on the digital separated, the steps of:

1) isolating the single-digit number, i.e. number% 10.

2) divided by the number 10, as the new number, that is the new number dropped to single digits.

3) If the number is equal to 0, the separation is completed, is ended. Otherwise, turn first) step, continue to show.

For example, number = 1234, number% 10 = 4, number = 1234/10 = 123, to obtain the number 4;

                        number% 10 = 3, number = 123/10 = 12, to obtain the number 3;

number% 10 = 2, number = 12/10 = 1, to obtain a digital 2;

number% 10 = 1, number = 1/10 = 0, to obtain a digital 1; end.

How to get the numbers 4,3,2,1 integer 4321 it?

4321=4*1000+3*100+2*10+1=4*103+3*102+2*10+1

The right of the polynomial using Horner's method can be solved.

Solutions reset request. 4 * 10 = P . 3 + 10. 3 * 2 + 2 * 10 +. 1

Can evolve into P = (((0 * 10 + 4) * 10 +3) * 10 + 2) * 10 + 1.

In this way, so that the initial value P 0, each given a number a, performs P = P * 10 + a can.

4,3,2,1 integer number 4321 obtained by the procedure as described:

P = 0, give the number 4, P = 0 * 10 + 4 = 4; after figures 3, P = 4 * 10 + 3 = 43;

After the number 2, P = 43 * 10 + 2 = 432; final figures 1, P = 432 * 10 + 1 = 4321.

The combination of the above two operations, while the number of separate digital you on, while its inverse number P up to fight, so the request process may be inverse number P number written a simple loop.

P=0;

While (number!=0)  { p=p*10+number%10;   number=number/10;   }

(2) source.

#include <stdio.h>

int main ()

{

         int n,m,f;

         scanf("%d",&n);

         m=0; f=1;

          if (n<0)

         {

                   n=-n;

                   f=-1;

         }

         while (n!=0)

         {

                   m=m*10+n%10;

                   n/=10;

         }

         m=f*m;

         printf("%d\n",m);

         return 0;

}

Problem 6

6-1 the number of the number of palindromic

Title Description

 "Palindrome" is the correct reading verlan can read through the sentence, which is at all times have a way of rhetoric and word games, such as "one for all, all for one" and so on. In mathematics, a class number also has such a feature, called number (palindrome number) palindrome.

Let n is an arbitrary natural number. The resulting n1 natural number equal to n if n digits of oppositely oriented, then n is a palindrome. For example, if n = 1234321, then n is a palindrome; if n = 1234567, the number n is not a palindrome.

For example, a number between 10 and 100 palindromic 11,22,33,44,55,66,77,88,99 has a total of nine.

Input Format

Two integers a and b (10≤a≤b≤65535).

Output Format

An integer representing the number of all the number of palindromic integer between a and b.

SAMPLE INPUT

10  100

Sample Output

9

        (1) programming ideas.

        If it is a positive integer equal to the number of reverse thereto, then it must be a palindrome. The method of preparation in Example 6 as a function int inverse (int n) seeking the number of reverse integer n.

        (2) source.

#include<stdio.h>

int inverse(int n)

{

    int s=0;

    while (n!=0)

    {

        s=s*10+n%10;

        n/=10;

    }

    return s;

}

int main ()

{

         int a,b,cnt,i;

         while(scanf("%d%d", &a,&b)!=EOF)

         {

                   cnt=0;

                   for (i=a;i<=b;i++)

                          if (i==inverse(i)) cnt++;

                   printf("%d\n", cnt);

         }

         return 0;

}

6-2 palindrome guess

        The title chosen from Hangzhou University of Electronic Science and Technology OJ exam (http://acm.hdu.edu.cn/showproblem.php?pid=1282)

Problem Description

A positive integer, if read from left to right (sequence number referred to as positive) and read from right to left (reverse call number) is the same, such a number is called palindrome. Either take a positive integer, if not a palindrome number, the number is added to the number of his reverse, and if it is not a palindrome number, repeat above steps until palindrome obtained so far. For example: 154 becomes 68 (68 + 86), then becomes 605 (154 + 451), and finally becomes 1111 (605 + 506), and 1111 is a palindrome. So mathematicians have proposed a guess: no matter what began as a positive integer, after a number of positive steps in reverse order finite number of times and added, you will get a palindrome. So far I do not know this conjecture is right or wrong. Now you programmed verification purposes.

Input

Each line of a positive integer.

Special note: input data to ensure intermediate result is less than 2 ^ 31.

Output

Corresponding to each input, two output lines, one line number is converted, one line transformation process.

Sample Input

27228

37649

Sample Output

3

27228--->109500--->115401--->219912

2

37649--->132322--->355553

         (1) programming ideas.

         Similarly a write function int inverse (int n) seeking the number of reverse integer n.

        (2) source.

#include<stdio.h>

int inverse(int n)

{

    int s=0;

    while (n!=0)

    {

        s=s*10+n%10;

        n/=10;

    }

    return s;

}

int main ()

{

    int m,cnt,t;

    while(scanf("%d",&m)!=EOF)

    {

        cnt=0;

        t=m;

        while (t!=inverse(t))

        {

            cnt++;

            t=t+inverse(t);

        }

        printf("%d\n",cnt);

        printf("%d",m);

        while (cnt--)

        {

            m=m+inverse(m);

            printf("--->%d",m);

        }

        printf("\n");

    }

    return 0;

}

6-3 rebellious Xiaoming

         The title chosen from Hangzhou University of Electronic Science and Technology OJ exam (http://acm.hdu.edu.cn/showproblem.php?pid=4554)

Problem Description

Xiao Ming rebellious like the opposite of what to do, even look at the figures, too (except the minus sign), such as: Xiao Ming will 1234 see it as 4321; as the -1234 -4321; as 032 to 230 (032 = 32 ); as -032 to -230 (-032 = -32).

Now, Xiao Ming did some ab and a + b subject (a, b is an integer and without leading 0), if you give the correct answers to these questions, you can guess what the answer Xiao Ming would you do it?

Input

A first input acts a positive integer T (T <= 10000), were made Xiaoming represents T questions.

Next T lines of two integers x, y (-1000000 <= x, y <= 1000000), x represents a + b is the correct answer, y ab represents the correct answer.

Enter the legal guarantee, and without considering a or b is decimal.

Output

T total output lines, each output two integers st, separated by a space between, where s represents the Bob answers obtained a + b, t represents ab Bob answers obtained.

Sample Input

3

20 6

7 7

-100 -140

Sample Output

38 24

7 7

-19 -23

        (1) programming ideas.

         Since the input x and y are the correct answer, i.e. x = a + b, y = ab. Can be obtained by a = (x + y) / 2, b = (xy) / 2.

        A and b are calculated inverse number c, and d, which is the operand Xiaoming, and c + d output to cd.

       (2) source.

#include<stdio.h>

int inverse(int n)

{

    int s=0;

    while (n!=0)

    {

        s=s*10+n%10;

        n/=10;

    }

    return s;

}

int main ()

{

         int x,y,a,b,c,d,t;

         scanf("%d", &t);

         while(t--)

         {

                   scanf("%d %d", &x, &y);

                   a = (x + y) / 2;

                   b = (x - y) / 2;

                   if (a < 0)

                            c = -1*inverse(-a);

                   else

                            c = inverse(a);

                   if (b < 0)

                            d = -1*inverse(-b);

                   else

                            d = inverse(b);

                   printf("%d %d\n", c + d, c - d);

         }

         return 0;

}

Guess you like

Origin www.cnblogs.com/cs-whut/p/11863842.html