刷题记录3

# 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;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值