Original question link: http://acm.nyist.net/JudgeOnline/problem.php? PID = 1, 647
Idea: I didn't want to talk about it. I wanted to write it in Java. I didn't find the corresponding function for double conversion .. C ++ should be used. I wanted to be lazy and use string directly, and then find a function that converts string to double. In the end, I still failed to use sstream, and the format would change .. In fact, the simplest method is to directly simulate it using arrays .. I always wanted to be lazy, and the result was circled...
First, a double-Type Binary template is provided. The format is not considered here ..
# Include <sstream> # include <stdlib. h >#include <algorithm> # include <iostream> using namespace STD; string: iterator it; string trans (long integer) {If (integer = 0) return "0"; string temp = ""; while (integer) {temp = char (integer % 2 + 48) + temp; integer/= 2;} return temp ;} string trans (double decimal) {string temp = ". "; int n = 20; while (decimal & N) {decimal * = 2; temp = temp + char (INT (decimal) + 48 ); decimal = Decimal-int (decimal); n --;} return temp;} int main () {int I, j; Double X; while (CIN> X) {long integer = long (x); double decimal = x-integer; double ans; string ans = trans (integer) + trans (decimal ); cout <ans <Endl ;}}
ACCode:
# Include <sstream> # include <stdlib. h >#include <algorithm> # include <iostream> using namespace STD; string: iterator it; string trans (long integer) {If (integer = 0) return "0"; string temp = ""; while (integer) {temp = char (integer % 2 + 48) + temp; integer/= 2;} return temp ;} string trans (double decimal) {string temp = ". "; int n = 20; while (decimal & N) {decimal * = 2; temp = temp + char (INT (decimal) + 48 ); decimal = Decimal-int (decimal); n --;} return temp;} int main () {int I, j; Double X; while (CIN> X) {long integer = long (x); double decimal = x-integer; double ans; string ans = trans (integer) + trans (decimal ); // cout <ans <Endl;/* Format Control Based on the question, and */INT n = ans. length (); While (-- N) {It = ans. end ()-1; if (ANS [N] = '0') ans. erase (it); else if (ANS [N] = '. ') {ans. erase (it); break;} else break;} cout <ans <Endl;/* stringstream SS; SS <ans; SS> ans; SS. clear (); cout <ans <Endl ;*/}}