W

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;
}
原文地址:https://www.cnblogs.com/farewell-farewell/p/5186063.html