让用户输入一行字符(空格分隔的多个单词),然后输出每一个单词(每行一个单词)。
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int main() {
// 读入一行字符
string s;
getline(cin, s);
// 将字符串按空格分隔为单词
char str[s.length() + 1];
strcpy(str, s.c_str());
char* pch = strtok(str, " "); // 使用 strtok 函数分隔单词
while (pch != nullptr) {
cout << pch << endl; // 输出单词并换行
pch = strtok(nullptr, " "); // 继续分隔下一个单词
}
return 0;
}
代码:(不是太懂)
int findNum(int *arr, int n)
{
int bit[32] = {0};
for(int i = 0; i < n; i++)
{
int b = 1;
for(int j = 31; j >= 0; j--)
{
int bv = arr[i]&b;
if(bv != 0) bit[j]++;
b <<= 1;
}
}
int res = 0;
for(int j = 0; j < 32; j++)
{
res <<= 1;
res += bit[j]%3;
}
return res;
}