zoukankan      html  css  js  c++  java
  • 4188. 进制转换

    4188. 进制转换

    Constraints

    Time Limit: 1 secs, Memory Limit: 256 MB

    Description

    输入一个非负的十进制整数,将其转为二进制.

    Input

    输入的第一行是一个整数T,表示总共有T组数据.

    接下来的T行,每行是一组数据,每组数据是一个待转换的十进制整数n(0<=n<2^31).

    Output

    对于每个十进制数,输出其对应的二进制数,每个数占一行. 注意输出的二进制数不要有多余的前导0.

    Sample Input

    3
    4
    1
    20
    

    Sample Output

    100
    1
    10100
    

    Problem Source

    计算机科学与技术专业11级3班期中考试

    觉得问题的关键就是在于如何把10进制的字符串变成二进制整数数组的转化。

    // Problem#: 4188
    // Submission#: 1984616
    // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
    // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
    // All Copyright reserved by Informatic Lab of Sun Yat-sen University
    #include<iostream>
    #include<string>
    #include<cmath>
    using namespace std;


    int main()
    {       
        int T,i;
        cin>>T;
        for(i=0;i<T;i++){
            string a;
            cin>>a;
            while(a.size()<32){
                a='0'+a;
            }
            int b[32]={0};
            int x=31;
            for(;x>=0;x--){
                b[x]+=((a[x]-'0')*(pow(10,31-x)/pow(2,31-x)));
                if(b[x]>=2){
                    b[x-1]+=(b[x]/2);
                    b[x]%=2;
                }
            }
            x=0;
            while(b[x]==0 && x!=31){
                x++;
            }
            for(;x<32;x++){
                cout<<b[x];
            }
            cout<<endl;
        }
        return 0;
    }                                 

  • 相关阅读:
    WinInet中的FTP操作
    CodeIgniter 用户指南 版本 1.7.2
    《Windows Mobile实例开发》电子书提供下载
    程序静默安装的参数总结
    Select a table of certain webpage
    568A
    在IIS 5.1 或IIS6 中配置PHP 的FastCGI模式
    镁天三国育将篇
    镁天三国军事篇
    windows 环境下的 protoc 安装
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/2978371.html
Copyright © 2011-2022 走看看