Source: Internet
Author: User

Tags: enum repeating and result condition Style 1.0 Val Record

These days in the review of computer principles, see binary system suddenly think of binary system to 10 into the formula, and then second thought to 10 binary system to the formula seems to have no impression, that simply write it out.

The results of the slag I found, can not write it! What sequence, logarithm, xx function forget all clean, and need to have to judge the place, so collapsed, before the algebra problem did not write conditions Ah ~

Simply use C # code to get out (although in C # there are methods to directly convert)

Binaryvalue | 10 binary values | Formula |
---|---|---|

0 | 0 | 0 |

1 | 1 | 1 |

10 | 2 | F (2) =10^1 =10 |

11 | 3 | F (3): The smallest integer bit than log?3 1, recording 10^1, and 2^1 =1,f (1) = 1, the final result is 10^1+f (1) =10+1=11 |

100 | 4 | F (4): log?4=2, Integer, direct return 10^2 |

101 | 5 | F (5): The smallest integer bit than Log?5 2, recorded 10^2, and 2^2 =1,f (1) = 1, the final result is 10^2+f (1) =100+1=101 |

110 | 6 | F (6): The smallest integer bit than Log?6 2, recording 10^2, and 6-(2^2) =2,f (2) = 10, the final result is 10^2+f (2) =100+10=110 |

111 | 7 | .................. |

1000 | 8 | .................. |

1001 | 9 | .................. |

1010 | 10 | .................. |

1011 | 11 | .................. |

1100 | 12 | .................. |

1101 | 13 | .................. |

1110 | 14 | .................. |

1111 | 15 | F (15): The smallest integer bit than log?15 3, recording 10^3, and 15-(2^3) =7,f (7) = 111, the final result is 10^3+f (7) =1000+111=1111 |

10000 | 16 | F (16): Smallest integer bit than log?6 4, integer, direct return 10^4 |

The formula is like this. If the binary of the 10 binary number n is asked, the logarithm log of N of the base 2 is first obtained. N, Judge log? n is an integer, if 10^log is returned directly for an integer? N, otherwise find out than log? n the smallest integer t (e.g. 1.001 takes 1, 2.02 takes 2, 3.9 takes 3). Find and record the 10^t, then subtract the n 2^t and repeat the above steps, and the returned value is added to the 10^t of the record.

Above we set this function to f (n);

Paste code, no doubt, with a recursive ~

Private DoubleDecimaltobinarysystem (intDecimalnumber) { DoubleStandard =0; if(decimalnumber==0) { return xx; } if(decimalnumber==1) { return on; } varDoublevalue = (Double) Decimalnumber; varlogarithm = Math.Log (Doublevalue,2 ); varLogformat =math.truncate (logarithm); varBasenum = Math.pow (Ten, Logformat); if(logarithm. Equals (Logformat)) {returnBasenum; } varLeftnumber = Doublevalue-math.pow (2, Logformat); returnBasenum + Decimaltobinarysystem ((int) leftnumber); }

only integers greater than or equal to 0 have been tested, and negative and fractional numbers are not tested.

Just be happy to think about it and welcome it.

C # implements 10-in-Turn 2-binary

Related Article