# 10 字符个数统计
描述
编写一个函数,计算字符串中含有的不同字符的个数。字符在 ASCII 码范围内( 0~127 ,包括 0 和 127 ),换行表示结束符,不算在字符里。不在范围内的不作统计。多个相同的字符只计算一次
例如,对于字符串 abaca 而言,有 a、b、c 三种不同的字符,因此输出 3 。
输入描述:
输入一行没有空格的字符串。
输出描述:
输出 输入字符串 中范围在(0~127,包括0和127)字符的种数。
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> record(130, 0);
string str;
cin >> str;
for (char c : str) {
if (c >= 0 && c <= 127) {
record[c]++;
}
}
int count = 0;
for (int i = 0; i < 130; i++) {
if (record[i] != 0) count++;
}
cout << count << endl;
return 0;
}
# 数字颠倒
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >>n;
string str = to_string(n);
reverse(str.begin(), str.end());
cout << str << endl;
return 0;
}
# 翻转字符串
#include <bits/stdc++.h>
using namespace std;
void reverseString(vector<char>& str) {
for (int i = 0, j = str.size() - 1; i < str.size()/2; i++, j--) {
swap(str[i], str[j]);
}
}
int main() {
vector<char> str;
char c;
while (cin >> c) {
str.push_back(c);
}
reverseString(str);
for (char ch : str) {
cout << ch;
}
return 0;
}