• BUPT复试专题—二进制数(2012)


    https://www.nowcoder.com/practice/103dd589fed14457a673c613d8de3841?tpId=67&tqId=29634&tPage=1&ru=/kaoyan/retest/1005&qru=/ta/bupt-kaoyan/question-ranking

    题目描述

    大家都知道,数据在计算机里中存储是以二进制的形式存储的。 有一天,小明学了C语言之后,他想知道一个类型为unsigned int 类型的数字,存储在计算机中的二进制串是什么样子的。 你能帮帮小明吗?并且,小明不想要二进制串中前面的没有意义的0串,即要去掉前导0。

    输入描述:

    第一行,一个数字T(T<=1000),表示下面要求的数字的个数。
    接下来有T行,每行有一个数字n(0<=n<=10^8),表示要求的二进制串。

    输出描述:

    输出共T行。每行输出求得的二进制串。
    示例1

    输入

    5
    23
    535
    2624
    56275
    989835

    输出

    10111
    1000010111
    101001000000
    1101101111010011
    11110001101010001011


    签到题,除二取余法即可,注意0
    #include <bits/stdc++.h>
    using namespace std;
    #define within(x,a,b) ((unsigned)((x)-(a))<=((b)-(a)))
    int readint(int *p)
    {
        int ch;
        while(!within(ch=getchar(),'0','9'))
            if(ch == EOF) return EOF;
        int rslt = 0;
        do
            rslt=rslt*10+(ch-'0');
        while(within(ch=getchar(),'0','9'));
        *p = rslt;
        return 1;
    }
    int main()
    {
        int T;
        while(~scanf("%d",&T))
        {
            while(T--)
            {
                int i,num=0;
                readint(&i);
                if(i==0)
                {
                    cout<<"0"<<endl;
                    continue;
                }
                char donser[100];
                while(i!=1)
                {
                    donser[num++]=i%2+'0';
                    i/=2;
                }
                cout<<"1";
                while(num--)
                    cout<<donser[num];
                cout<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    P2910 [USACO08OPEN]寻宝之路Clear And Present Danger 洛谷
    P2212 [USACO14MAR]浇地Watering the Fields 洛谷
    Python字体颜色设置
    Python小游戏 -- 猜数字
    数据结构 -- 链表&双向链表
    数据结构 -- 队列 & 循环队列 -- 数组实现
    数据结构 -- 栈的数组实现法
    洛谷P1036 选数
    如何让c语言使用结构体近似模拟c++中的类
    对c语言回调函数的理解
  • 原文地址:https://www.cnblogs.com/dzzy/p/8260638.html
Copyright © 2020-2023  润新知