您的位置:首页 > 其它

ZOJ 1383 Binary Numbers

2014-02-20 11:25 316 查看
题目链接: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: