This question around: mathematical formulas simulation system conversion
HDU4814 Golden Radio Base
Title Description
Converting a non-negative integer into a decimal E (golden division number) number hexadecimal
Entry
Nonnegative integer no greater than 10 ^ 9, the process to the end of the file
Export
The hexadecimal number is converted to E (which may contain a decimal)
Sample input
1
2
3
6
10
Sample Output
1
10.01
100.01
1010.0001
10100.0101
Topic analysis
For this question, we should pay attention to the point are: First, for positive a decimal, we can strictly be converted into a E (the golden number of divisions) hexadecimal number, rather than involve approximately equal to, for example, 10-base of n, can be converted into n * E ^ 0 key (the E stands with golden hexadecimal number), in addition to two formulas prompted this question is given in problem solving, we get the following two formulas by these two formulas: ① E ^ n-= E ^ n--. 1 + E ^ n--2, ② 2 * E ^ n-= E ^ n-+. 1 + E ^ n--2 , and the idea of this problem is to build an array a [] for storing us the final answer required output, a [i] stored i-th power coefficient of E (the same as binary), the output of this title may contain decimals, so we will array half of decimal places for storage coefficient (E, -i time position, because we know that by two formulas, even if E exponent is negative formula is still valid), we calculated that E ^ 50 ^ 9 has been greater than 10, so we just opened into the array 100, and the coefficient storage 50 before the decimal point, coefficient before storing after the decimal point 50, a [50] initial When n is equal of (E, 0 to 50 times as orientation, just corresponds to our 10-base mentioned above can be converted into an n-E-base of n * E ^ 0), and for our whole a [] array, we it requires constant traversal, once it satisfies any factor greater than 1 bit (it can be formulated ② coefficients to both sides of the split-bit), two consecutive bits or coefficients greater than or equal 1 (formula ① may be the next bit coefficients a plus a small value that the first two coefficients, and the first two coefficients of this small value is subtracted) may be formulated to be optimized, after a until the entire array can not be optimized by the two equations in [], output answer (note the leading and trailing zeros omitted output, while paying attention to whether the output of decimal places)
Code:
. 1 #include <the iostream> 2 #include <stdio.h> . 3 #include < String .h> . 4 the using namespace STD; . 5 . 6 / * . 7 E ^ n representative of the number of golden n th power of E 8 Equation 1: E ^ n-= E ^ (n--1) + E ^ (n--2) . 9 equation 1 promotion: n-* E ^ n-= n-* E ^ (n--1) + n-* E ^ (n--2) 10 formula 2: 2 * E ^ n-= E ^ (n-+. 1) + E ^ (n--2) . 11 * / 12 is int min ( int A, int B) { 13 is return ? A <B A: B; 14 } 15 16 int main () { 17 int a[105]; 18 int n; 19 while(scanf("%d", &n) != EOF){ 20 memset(a, 0, sizeof(a)); 21 a[50] = n; 22 int flag = 1; 23 while(flag){ 24 flag = 0; 25 for(int i = 1; i <= 100; i++){ 26 IF (A [I]> 1 ) { // if the coefficient is larger than the upper and coefficients low 1 Equation 2 coefficient give until itself coefficient of 0 or 1 27 A [I + 1 A [I]] + = / 2 ; 28 a [I- 2 ] = a + [I] / 2 ; 29 a [I]% = 2 ; 30 in Flag = . 1 ; // Once this step is illustrated performing high and low coefficient changes, factor greater than 1 can occur continuously or occurs 1 31 is } 32 IF (a [I- 1 ] && a [I- 2 ]) { // first call promotion equation 1, the minimum value of two successive coefficients greater than or equal to 1 added to the a [i] 33 is int TEMP = min (A [I- . 1 ], A [I- 2 ]); 34 is A [I- . 1 ] - = TEMP; 35 A [I- 2 ] - = TEMP; 36 A [I] + = TEMP; 37 [ // a [-I. 1] -; // the problem here is that if the code written in the TL timeout comment 38 is // a [I-2] -; 39 // a [I] ++ ; 40 in Flag = 1 ; // Once a [i] is increased, the a [i] may be larger than 1, the number of other adjacent or form a continuous 1 41 is } 42 is } 43 is } 44 is int Point = 0 ; 45 for ( int I = 49 ; I> = . 1 ; i-- ) { 46 is IF (A [I] == . 1 ) { 47 Point = . 1 ; 48 BREAK ; 49 } 50 } 51 // preamble 0 0 suffix is not output and the output, while paying attention to whether the decimal point 52 is int Front = 0 ; 53 is int back = 0; 54 for(int i = 100; i >= 50; i--){ 55 if(a[i] == 0 && front == 0){ //省略前导零 56 continue; 57 } 58 if(a[i] == 1) front = 1; 59 printf("%d", a[i]); 60 } 61 for(int i = 1; i <= 49 ; I ++) { // find the location of a back end of the last record 62 is IF (A [I] == 1 ) { 63 is back = I; 64 BREAK ; 65 } 66 } 67 IF (Point) { 68 the printf ( " . " ); 69 for ( int I = 49 ; I> = Back; i--) the printf ( " % D " , A [I]); 70 } 71 is the printf ( " \ n- "); 72 } 73 Return 0 ; 74 }