赞
踩
这个问题可以通过维护一个字符出现频率的数组来解决。考虑到 ASCII 码的范围是 0~127,我们可以创建一个长度为 128 的布尔数组(或者使用位操作优化空间),用来标记某个字符是否出现过。遍历输入字符串的每个字符,如果该字符在 ASCII 范围内,并且之前未标记过,就将其对应的标记位置为 true
并增加不同字符的计数。最后,输出不同字符的总数即可。
- #include <iostream>
- #include <string>
- using namespace std;
-
- int countUniqueCharacters(const string& str) {
- // 创建一个布尔数组来标记字符是否出现过,初始都为 false
- bool 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;
- }
为了解决这个问题,我们可以首先将整数转换成字符串,然后逆序这个字符串。C++ 中有多种方法可以完成这个任务,但最直接的方法是使用标准库中的 `std::to_string` 函数将整数转换为字符串,接着使用 `std::reverse` 对字符串进行逆序操作。
下面是具体的实现步骤:
1. 使用 `std::to_string` 将输入的整数转换为字符串。
2. 使用 `std::reverse` 函数逆序字符串。
3. 输出逆序后的字符串。
下面是实现这个逻辑的 C++ 代码示例:
- #include <iostream>
- #include <string>
- #include <algorithm> // 包含 std::reverse
-
- int 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;
- }
- #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;
- }
- #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;
- }
-
- #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;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。