Algorithm: Program ABCDE * 4 = edcba

Source: Internet
Author: User

Question:

Please develop a program to figure out the value of ABCDE which meet the condidition ABCDE * 4 = edcba. (A, B, C, D and E won't be required plicative .)

Solution1:

#include using namespace std ;int main(int argc, char* argv[]){for(int i=10000 ; i<100000 ; i++){int a = i/10000 ;int b = (i/1000)%10 ;int c = (i/100)%10 ;int d = (i/10)%10 ;int e = (i/1)%10 ;int right = e*10000+d*1000+c*100+b*10+a ;if(i*4 == right){//Output A,B,C,D,E
break ;}}return 0;}

Although solution 1 cocould solve our issue successfully, it doesn' t support good compatibility. For example, what if we want to figure out abcdefgh... Z which meet condition abcdefgh... Z * 4 = Z... Hgfedcba?

So, we can optimize our algorithm. the basic idea of sotion is still try to figure out right value and compare it with left * 4. but we can find out a better way to get right value.

Suppose that left value is 12345, then right value shoshould be 54321. So, we can see a regularity as below.

First we can find that right value is the reverse of left value. so, we need to figure our each of number of left value from right to left. and then, the right vaule shoshould be consist of these numbers from left to right.

So, we can get following regularity.

We can get the last number of left value 5 by 12345% 10. This vaule 5 is also the remainder of 12345.

Then we can get the second last number 4 by calculating the remainder of value 1234. And we can get 1234 by 12345/10.

For the same way, we can get the rest of number 3, 2 and 1. But there is a new question is how can we combinate these number to 54321? Well, we have a good way to take care of it.

According above of our caculation, we get the number 5, 4, 3, 2 and 1 in sequently. then, we can put 5 and 4 on proper place by 5*10 + 4 = 54. then, we can put 3 on proper place by 54 × 10 + 3 = 543. on the same way, we can put 2 and 1 on proper place by 543*10 + 2 = 5432 and 5432*10 + 1 = 54321.

To summarize our algorithm, we can develop following codes.

 # Include "stdafx. H "<br/> # include <iostream> </P> <p> using namespace STD; </P> <p> int main (INT argc, char * argv []) <br/>{</P> <p> for (INT I = 10000; I <100000; I ++) <br/>{< br/> int right = 0; <br/> int left = I; </P> <p> while (left! = 0) <br/>{< br/> right = right * 10 + Left % 10; <br/> left = left/10; <br/>}</P> <p> if (I <2 = right) <br/> {<br/> cout <I <Endl; <br/>}</P> <p> return 0; <br/>}< br/>

 

For any question, please contact me.

Yexianyi@hotmail.com

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.