C Language Combat reverse a three-digit number

Source: Internet
Author: User

Title: Given a positive three-bit integer, such as 123, output 321, but if it is 700, the output must be 7 cannot be 007

What is the idea?

Assuming that the positive three-digit number is m, its percentile is C, very bit is B, BITS is a, then there is the following equation.

M=c*100+b*10+a*1

Now that M is known, as long as you know the value of the three numbers of C, B, a, then we can output it directly in ABC order.

So how do you know the value of these three numbers?

First, C is the best thing to ask.

C=M/100//Divide the result is the number of hundred

The value of B is also better to use similar ideas.

b= (m%100)/10//m to 100 after the remainder is 10-digit and single-digit, and in the divisible 10 is 10 digits.

An equation three unknowns, know two of them, then the third one will know.

A=m-c*100-b*10

Well, it's settled, write the program.

    intm =0; intA =0; intb =0; intc =0; scanf_s ("%d",&m); C= m/ -; b= (m% -) /Ten; A= M-(c* -)-(b*Ten); printf ("%d%d%d", a,b,c);

After running, found that there is no big problem, only for 700 of this situation, the output or 007, how to do? What to do, very urgent, I initially want to use the character output, the program crashed, unsuccessful. Later a thought, since already knew three position the numerical value, then the direct three position the number adds a bit to be OK, anyway the result has not changed, for example input 700, then C equals 7 b equals 0 A equals 0, then after the reverse order this number is not 0*100+b*10+c*1, then the result is not 7,

The last thing you can do is change it.

        int m = 0;int A = 0;int b = 0;int c = 0;scanf_s ("%d", &m), c = m/100;b = (m%)/10;a = M-(c*100)-(b*10);p rintf ( "%d", a*100+b*10+c);        

  

C Language Combat reverse a three-digit number

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.