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; }
相关文章推荐
- HDU 1390 || ZOJ 1383 Binary Numbers
- ZOJ Problem Set - 1383 Binary Numbers
- zoj 1383 Binary Numbers
- zoj 1383 Binary Numbers
- ZOJ 1383 Binary Numbers
- ZOJ Problem Set - 1383 Binary Numbers
- ZOJ Problem Set - 1383 Binary Numbers
- ZOJ 1383 Binary Numbers
- ZOJ 1383 Binary Numbers
- zoj 1383 Binary Numbers
- zoj 1383 Binary Numbers
- ZOJ Problem Set–1383 Binary Numbers
- ZOJ 1383: Binary Numbers
- ZOJ 1383题解,乘着媛姐讲课做了一道水题,不做不知道,一做还真是水题!
- zoj 1383 Binary Numbers
- ZOJ-1383
- ZOJ 1383 输出二进制为“1”的位
- ZOJ_1383
- zoj 1383
- ZOJ 1383