AOJ_0220 Binary Digit A Doctor Loved
問題
解法
普通に2進数に変換すればいい。
#include <cstdio> #include <algorithm> using namespace std; const double EPS = 1e-9; const double bit[] = {128.0, 64.0, 32.0, 16.0, 8.0, 4.0, 2.0, 1.0, 0.5, 0.25, 0.125, 0.0625}; int main(){ double r; while(scanf("%lf", &r) && r >= 0){ char b[12]; for(int i = 0; i < 12; i++){ if(bit[i] <= r + EPS){ b[i] = 1; r -= bit[i]; }else{ b[i] = 0; } } if(!((r<0)?-r:r < EPS)){ printf("NA\n"); }else{ for(int i = 0; i < 8; i++) printf("%d", b[i]); printf("."); for(int i = 0; i < 4; i++) printf("%d", b[8 + i]); puts(""); } } return 0; }