Description

Give you a number on base ten,you should output it on base two.(0 < n < 1000)       
                

Input

For each case there is a postive number n on base ten, end of file.       
                

Output

For each case output a number on base two.       
                

Sample Input

1 2 3
                

Sample Output

1 10 11
 
 
很简单  十进制化为二进制
题目中未提到小数 但应该考虑到
#include<iostream>
using namespace std;
void f(double n)
{
    int i,j,k,a[100],p=0;
    double t;
    k=n;
    t=n-k;
    if(k==0)cout<<0;
    while(k){
        if(k%2==0)a[p++]=0;
        else a[p++]=1;
        k=k/2;
    }
    for(i=p-1;i>=0;i--)cout<<a[i];
    if(t){
        cout<<".";
        while(1){
            t*=2;
            if(t>=1){
                cout<<1;
                if(t==1)break;
                else t-=1;
            }
            else cout<<0;
        }
    }
    cout<<endl;
}
int main()
{
    double n;
    while(cin>>n){
        f(n);
    }
    //system("pause");
    return 0;
}

 

相关文章:

  • 2021-10-24
  • 2021-11-15
  • 2021-10-28
  • 2021-07-20
  • 2021-08-04
  • 2021-11-10
  • 2021-09-20
  • 2021-10-15
猜你喜欢
  • 2022-02-25
  • 2021-11-18
  • 2021-08-06
  • 2021-12-28
  • 2021-06-05
  • 2021-12-01
  • 2022-01-05
相关资源
相似解决方案