题目是这样的
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <stdlib.h>
using namespace std;
int toBinary(int m);
int main()
{
int n = 1; //所输入的行数
cin >> n;
vector<string> str;
vector<string>::iterator itor;
for (int i = 0;i < n;i++) {
string input_num;
cin >> input_num;
str.insert(str.end(),input_num);
}
for (itor = str.begin();itor != str.end();++itor) {
string str_num = *itor;
int int_num = atoi(str_num.c_str());
int sec_num = toBinary(int_num);
//int sec_num = bitset<sizeof(int) * 8>(int_num);
ostringstream stream;
stream << sec_num;
string sec_str = stream.str();
//string sec_str = to_string(sec_num);
vector<char> sec_char;
vector<char>::iterator itr;
sec_char.resize(sec_str.size());
sec_char.assign(sec_str.begin(), sec_str.end());
//const char *sec_char = sec_str.c_str();
int count = 0;
for (itr = sec_char.begin();itr != sec_char.end();++itr) {
char res_num = *itr;
if (res_num == '1') {
count++;
}
}
cout << count << endl;
}
return 0;
}
//二进制转换函数(反)
int toBinary(int m)
{
int a;
string b;
string c = "";
while (m != 0) {
a = m % 2;
ostringstream d;
d << a;
b = d.str();
//b = to_string(a);
c = c + b;
m = m >> 1;
}
return atoi(c.c_str());
}
我自己本地各种试,结果都符合预期。但是在ACM系统提交时,得到的反馈是wronganswer,我是不是有特殊情况没考虑到?实在不解。