At the beginning, I tried to use the multiplication method, for example, math. pow has been multiplied from 0 and added to the sum until it is less than Y. However, this is a ridiculous design and the result is of course problematic.
Later, I discussed with colleagues, some suggested recursion, and some suggested array. In fact, the suggestion for Bright Eyes is binary. For example, 8 = 2 ^ 1001 = 2 ^ 3 + 2 ^ 0 is actually a binary conversion process, 8 =. This reminds me of the school's mathematical knowledge at that time, which is the best solution to this algorithm.
/// <Summary> <br/> // convert decimal number to Y = 2 ^ N1 + 2 ^ N2 + ...... <Br/> /// </Summary> <br/> /// <Param name = "Number"> type: long </param> <br/> // <returns> Note: return the string not the number </returns> <br/> Public static string converttobinarypower (long number) <br/> {<br/> // convert number to binary string <br/> var strbinary = convert. tostring (number, 2); <br/> // define the result <br/> var strresult = Number + "="; <br/> for (INT I = strbinary. length-1, j = 0; I> = 0; I --, J ++) <br/>{< br/> If (strbinary [I] = 49) // 49 here is '1' <br/>{< br/> strresult + = "2 ^" + J; <br/> if (I> 0) // If is the last one, need not add' + '<br/> {<br/> strresult + = "+ "; <br/>}< br/> return strresult; <br/>}
The following is a solution provided by a colleague. I have made some performance changes:
Static string buildstring (long inumber) { Stringbuilder strresult = new stringbuilder (inumber + "= "); For (VAR I = inumber. tostring (). length * 4; I> = 0; I --) { If (inumber-(long) math. Pow (2, I)> = 0) { Inumber = inumber-(long) math. Pow (2, I ); Strresult. append ("2 ^" + I. tostring () + "+ "); } } Strresult. Length --; Return strresult. tostring (); } |
In addition, the custom Algorithm for converting decimal to binary: (but I didn't use this custom conversion above. It is not clear which efficiency is higher than convert. tostring (number, 2) /// <Summary> <br/> // convert decimal number to binary number <br/> /// </Summary> <br/> // <Param name = "Number"> decimal number </param> <br/> // <returns> return the binary number (type: string) </returns> <br/> Public static string convertdecimaltobinary (long number) <br/>{< br/> long [] temp = new long [100]; // as the type is long, the length 100 is enough <br/> // If 0, will not be converted <br/> If (number = 0) Return (number. tostring (); <br/> var I = 0; <br/> while (number! = 0) <br/>{< br/> temp [I ++] = Number % 2; <br/> Number/= 2; <br/>}< br/> var strbinary = ""; <br/> for (var j = 0; j <= I-1; j ++) <br/> strbinary + = (char) (temp [I-j-1] + 48 ); // 48 here means the char '0' <br/> return (strbinary); <br/>}
In addition, the sad story behind my experiences:
People often say that they like one person. They are very happy to be together. They love one person and want to be together if they are unhappy. I don't know how to say it. That's what it means. I implemented the above y = 2 ^ N1 + 2 ^ N2 + ...... The algorithm is actually designed with the following formula:
L + O + V + E = abcxyyx + ABC * 107 + (AB) 2 + AC) * 106 + XXY + X + Y + BC |
I once tried to keep her happy and put L, O, V, E into my cell phone number (we have two mobile phones, so we have four numbers), A = 5, B = 2, c = 0, x = 13, y = 14, respectively:
134 ×××××××× + 138 ××××××× + 137 ×××××× × + 189 ××××××× = 5201314 + 520*107 + (52) 2 + 50) * 106 + 131314 + 13 + 14 + 20 |
The equation is true. In fact, I want to express my heart and my life's love to her. 520x107 is enough to surpass "I love you for 10 thousand years. I only consist of 5, 2, 3, 1, 4, and power, to be equal to the sum of the four phone numbers (L, O, V, E ).
Finally, it is a pity that this is a tragedy, with good intentions, and finally inevitably suffer setbacks.
From: http://www.cnblogs.com/architect/archive/2009/03/04/1402823.html