Run ID 作者 问题 语言 测评结果 Time Memory 代码长度 提交时间
75381 彭林江 十进制小数转二进制 C++ Accepted 1 MS 272 KB 690 2024-05-26 17:20:51

Tests(10/10):


Code:

#include<bits/stdc++.h> using namespace std; int main(){ double n; cin >> n; // step1:分整数和小数 int z = n; // 整数 double x = n-z; // 小数 // ste2:处理整数 int b[32], cnt = 0; while(z != 0){ int yu = z % 2; b[cnt] = yu; cnt++; z /= 2; } if(cnt==0){ cout << 0; }else{ for(int i = cnt-1; i>=0; i--){ cout << b[i]; } } // step3:处理小数点 if(x != 0){ // 小数部分不为0,就一定会有小数点 cout << "."; }else{ return 0; } // step4:处理小数 for(int i = 0; i < 9; i++){ x = x * 2; int xz = x; cout << xz; x = x-xz; if(x == 0){ break; } } return 0; }