ZOJ 1383 Binary Numbers

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=383

注意:本题格式上,要求两位之间用一个空格隔开,而每行的最后不能有空格。这点要特别注意。

代码如下:

#include<iostream>
#include<fstream>
#include<set>
#include<string>
#include<queue>
#include<set>
#include<iterator>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<numeric>
using namespace std;
int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("D:\\in.txt", "r", stdin);
        freopen("D:\\out.txt", "w", stdout);
    #endif
        int num(0);
        string str;
        int n(0);
        cin >> num;
        for (int j = 0; j < num; j++)
        {
            //cout << ((0 == j) ? "" : "\n");
            cin >> n;
            str = "";
            /*for (int a = n; a != 0; a = a / 2)
            {
                if (0 == a % 2)
                {
                    str = str + '0';
                }
                else
                {
                    str = str + '1';
                }
            }*/
            for (int a = n; a != 0; a = a / 2)
            {
                str += (a % 2 ? '1' : '0');
            }
            int p = 0;//第一次输出
            for (int i = 0; i < str.length(); i++)
            {
                if ('1' == str[i])
                {
                    if (0 == p)
                    {
                        cout << i;
                    }
                    else
                    {
                        cout << " " << i;
                    }
                    p = 1;
                }
            }
            cout << endl;
        }
        return 0;
}

本题用向量的代码如下:

#include<iostream>
#include<fstream>
#include<set>
#include<string>
#include<queue>
#include<set>
#include<iterator>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<numeric>
using namespace std;
int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("D:\\in.txt", "r", stdin);
        freopen("D:\\out.txt", "w", stdout);
    #endif
        int num(0);
        vector<int> coll;
        int n(0);
        cin >> num;
        for (int j = 0; j < num; j++)
        {
            //cout << ((0 == j) ? "" : "\n");
            cin >> n;
            coll.clear();
            int p = 0;//第一次输出
            for (int i = n; n != 0; n = n / 2)
            {
                coll.push_back(i % 2 ? 1 : 0);
            }
            for (int i = 0; i < coll.size(); i++)
            {
                if (1 == coll[i])
                {
                    if (0 == p)
                    {
                        cout << i;
                    }
                    else
                    {
                        cout << " " << i;
                    }
                    p = 1;
                }
            }
            cout << endl;
        }
        return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值