HJ10 字符个数统计
字符个数统计_牛客题霸_牛客网
题目分析
这个问题可以通过维护一个字符出现频率的数组来解决。考虑到 ASCII 码的范围是 0~127,我们可以创建一个长度为 128 的布尔数组(或者使用位操作优化空间),用来标记某个字符是否出现过。遍历输入字符串的每个字符,如果该字符在 ASCII 范围内,并且之前未标记过,就将其对应的标记位置为 true
并增加不同字符的计数。最后,输出不同字符的总数即可。
题解代码
#include <iostream>
#include <string>
using namespace std;int countUniqueCharacters(const string& str) {// 创建一个布尔数组来标记字符是否出现过,初始都为 falsebool appeared[128] = {false};int count = 0; // 用于统计不同字符的数量for (char ch : str) {// 检查字符是否在 ASCII 范围内if (ch >= 0 && ch <= 127) {// 如果该字符之前没有出现过,增加计数并标记为已出现if (!appeared[ch]) {appeared[ch] = true;++count;}}}return count;
}int main() {string input;cin >> input; // 读取输入的字符串cout << countUniqueCharacters(input) << endl; // 输出不同字符的数量return 0;
}
我的代码
#include <iostream>
#include <string>
#include <unordered_set>
int main() {std::string line;std::unordered_set<char> uset;std::getline(std::cin, line);for (char i: line) {uset.insert(i);}std::cout << uset.size() << std::endl;return 0;
}
HJ11 数字颠倒
数字颠倒_牛客题霸_牛客网
题目分析
为了解决这个问题,我们可以首先将整数转换成字符串,然后逆序这个字符串。C++ 中有多种方法可以完成这个任务,但最直接的方法是使用标准库中的 `std::to_string` 函数将整数转换为字符串,接着使用 `std::reverse` 对字符串进行逆序操作。
下面是具体的实现步骤:
1. 使用 `std::to_string` 将输入的整数转换为字符串。
2. 使用 `std::reverse` 函数逆序字符串。
3. 输出逆序后的字符串。
下面是实现这个逻辑的 C++ 代码示例:
#include <iostream>
#include <string>
#include <algorithm> // 包含 std::reverseint main() {unsigned int n;std::cin >> n; // 读取输入的整数// 将整数转换为字符串std::string str = std::to_string(n);// 使用 std::reverse 逆序字符串std::reverse(str.begin(), str.end());// 输出逆序后的字符串std::cout << str << std::endl;return 0;
}
这段代码首先从标准输入读取一个整数,然后将其转换为字符串并逆序,最后输出逆序后的字符串。注意,这个程序假设输入的整数是非负的,符合题目要求。
我的解法
#include <iostream>
int main() {int num;std::cin >> num;if (num == 0) std::cout << num;while (num > 0) {int out = num % 10;std::cout << out;num = num / 10;}return 0;
}
HJ12 字符串反转
字符串反转_牛客题霸_牛客网、
题目分析
- 读取输入:首先,需要读取一行输入,这行输入是一个只包含小写字母的字符串。
- 反转字符串:使用C++标准库中的功能或者自定义的方式来反转字符串。
- 输出结果:打印或输出反转后的字符串。
#include <iostream> #include <algorithm> // 包含 std::reverse using namespace std;int main() {string str;cin >> str; // 读取输入reverse(str.begin(), str.end()); // 反转字符串cout << str << endl; // 输出结果return 0; }
方法2:自定义函数实现反转、
-
#include <iostream> using namespace std;int main() {string str;cin >> str; // 读取输入int n = str.length();// 自定义反转for(int i = 0; i < n / 2; ++i) {swap(str[i], str[n - i - 1]);}cout << str << endl; // 输出结果return 0; }
HJ13 句子逆序
- 句子逆序_牛客题霸_牛客网
题目分析
- 分割单词:将每个输入的句子按空格分割成单词。
- 逆序排放:将分割出来的单词数组逆序。
- 输出结果:将逆序后的单词数组重新组合成一个句子,并输出。
acm代码
#include <iostream>
#include <vector>
#include <sstream>
#include <algorithm>int main() {std::string line;std::getline (std::cin , line);std::istringstream iss(line);std::vector<std::string> words;std::string word;while (iss >> word) {words.push_back(word);}std::reverse(words.begin(), words.end());for (int i = 0; i < words.size(); i ++) {std::cout << words[i];if (i != words.size() - 1) {std::cout << " ";}}std::cout << std::endl;return 0;
}
我的解法
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>int main() {std::string line;std::string temp;std::getline(std::cin, line);std::vector<std::string> vec;for (int i = 0; i < line.size(); i++) {if (line[i] != ' ') {temp.push_back(line[i]);}else if (line[i] == ' ' ){vec.push_back(temp);temp.clear();}if (i == (line.size() - 1)) vec.push_back(temp);}std::reverse(vec.begin(), vec.end());for (std::string i: vec) {std::cout << i << " ";}return 0;
}