AOJ 0220 Binary Digit A Doctor Loved
こんにちは川です。
今回は2のn(7〜-4)乗で割り切れる場合数字を2のn乗で引いていき、
その時、文字として答えを数字を2のn乗で割った値を格納していきます。
その後、数が0より大きい場合は制限桁数に入っていないためNAを出力し、
それ以外では答えを格納した文字列を出力します。
#include <bits/stdc++.h>
using
namespace
std;
int
main() {
double
num;
while
(cin>>num,num>0){
string str;
for
(
double
i=128,a;i>0.03125;i/=2){
str+=
int
(a=num/i)+
'0'
;
if
(a>=1)num-=i;
if
(i==1)str+=
'.'
;
}
if
(num>0)cout<<
"NA"
<<endl;
else
cout<<str<<endl;
}
}