码迷,mamicode.com
首页 > 其他好文 > 详细

W - Bitset(第二季水)

时间:2016-02-10 19:58:31      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

 

W - Bitset(第二季水)

标签:

原文地址:http://www.cnblogs.com/farewell-farewell/p/5186063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!