当前位置: 首页 > article >正文

NO.29十六届蓝桥杯备战|string九道练习|reverse|翻转|回文(C++)

P5015 [NOIP 2018 普及组] 标题统计 - 洛谷
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    getline(cin, s);
    int sz = s.size();
    int cnt = 0;
    for (int i = 0; i < sz; i++)
    {
        if (isspace(s[i]))
            continue;
        else
            cnt++;
    }
    cout << cnt << endl;

    return 0;
}

isspace判断是否是空白字符,即空格和换行

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    getline(cin, s);
    int cnt = 0;
    for (auto x : s)
    {
        if (isspace(x))
            continue;
        else
            cnt++;
    }
    cout << cnt << endl;

    return 0;
}

方法二:按照单词读取

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    int cnt = 0;
    while (cin >> s)
    {
        cnt += s.size();        
    }
    cout << cnt << endl;

    return 0;
}

有时候处理⼀个字符串的时候,也不⼀定要⼀次性读取完整个字符串,如果字符串中有空格的话,其实可以当做多个单词,⼀次读取。
cin >> s 会返回⼀个流对象的引⽤,即 cin 本⾝。在C++中,流对象(如 cin )可以被⽤作布尔值来检查流的状态。如果流的状态良好(即没有发⽣错误),流对象的布尔值为 true 。如果发⽣错误(如遇到输⼊结束符或类型不匹配),布尔值为 false 。
在 while (cin >> s) 语句中,循环的条件部分检查 cin 流的状态。如果流成功读取到⼀个值, cin >> s 返回的流对象cin 将被转换为true ,循环将继续。如果读取失败(例如遇到输⼊结束符或⽆法读取到⼀个值), cin >> s 返回的流对象 cin 将被转换为 false ,循环将停⽌。

B2112 石头剪子布 - 洛谷
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    string p1, p2;
    while (n--)
    {
        cin >> p1 >> p2;
        if (p1 == p2)
            cout << "Tie" << endl;
        else if (p1 == "Rock" && p2 == "Scissors")
            cout << "Player1" << endl;
        else if (p1 == "Scissors" && p2 == "Paper")
            cout << "Player1" << endl;
        else if (p1 == "Paper" && p2 == "Rock")
            cout << "Player1" << endl;
        else
            cout << "Player2" << endl;
    }
    
    return 0;
}
B2115 密码翻译 - 洛谷

![[Pasted image 20250307210105.png]]

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    getline(cin, s);
    for (auto &x : s)
    {
        if ((x >= 'b' && x <= 'z') || (x >= 'B' && x <= 'Z'))
            x--;
        else if (x == 'a')
            x = 'z';
        else if (x == 'A')
            x = 'Z';
    }
    cout << s << endl;
    
    return 0;
}
P5734 【深基6.例6】文字处理软件 - 洛谷
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int q = 0;
    int m = 0;
    string s;
    string str;
    int a, b;
    cin >> q;
    cin >> s;
    while (q--)
    {
        cin >> m;
        switch(m)
        {
        case 1:
            cin >> str;
            s += str;
            cout << s << endl;
            break;
        case 2:
            cin >> a >> b;
            s = s.substr(a, b);
            cout << s << endl;
            break;
        case 3:
            cin >> a >> str;
            s.insert(a, str);
            cout << s << endl;
            break;
        case 4:
            cin >> str;
            size_t n = s.find(str);
            if (n == string::npos)
                cout << -1 << endl;
            else
                cout << n << endl;
            break;
        }
    }
    
    return 0;
}
B2120 单词的长度 - 洛谷
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    cin >> s;
    cout << s.size();
    while (cin >> s)
    {
        size_t n = s.size();
        cout << ',' << n;
    }
    cout << endl;
    
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    bool flag = true;
    while (cin >> s)
    {
        if (flag)
        {
            cout << s.size();
            flag = false;
        }
        else
        {
            size_t n = s.size();
            cout << ',' << n;  
        }
    }
    cout << endl;
    
    return 0;
}
B2122 单词翻转 - 洛谷
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    while (cin >> s)
    {
        int left = 0;
        int right = s.size() - 1;
        while (left < right)
        {
            char tmp = s[left];
            s[left] = s[right];
            s[right] = tmp;
            left++;
            right--;
        }
        cout << s << endl;
    }
    
    return 0;
}

其实在C++的STL中,包含⼀个算法叫 reverse ,可以完成字符串的逆序(反转)。需要的头⽂件是 <algorithm>

void reverse (BidirectionalIterator first, BidirectionalIterator last);  
//first: 指向要反转范围的第⼀个元素的迭代器(也可以是地址)  
//last: 指向要反转范围的最后⼀个元素的下⼀个位置的迭代器(也可以是地址)(翻转时不包括此元素)。

reverse 会逆序范围 [first, last) 内的元素

string s = "abcdef";  
reverse(s.begin(), s.end());
#include <iostream>  
#include <algorithm>  
using namespace std;
int main()  
{  
	//反转字符串  
	string s("abcdef");  
	reverse(s.begin(), s.end());  
	cout << s << endl;  
	
	//反转数组  
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };  
	int size = sizeof(arr) / sizeof(arr[0]);  
	
	//对数组中的元素进⾏反转  
	reverse(arr, arr+size);  
	for (auto e : arr)  
	{  
		cout << e << " ";  
	}  
	cout << endl;  
	return 0;  
}

![[Pasted image 20250308104344.png]]

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    while (cin >> s)
    {
        reverse(s.begin(), s.end());
        cout << s << endl;
    }
    
    return 0;
}
B2124 判断字符串是否为回文 - 洛谷
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    cin >> s;
    string s1;
    s1 = s;
    reverse(s.begin(), s.end());
    if (s1 == s)
        cout << "yes" << endl;
    else
        cout << "no" << endl;
    
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    cin >> s;
    int left = 0;
    int right = s.size() - 1;
    while (left < right)
    {
        if (s[left] != s[right])
        {
            cout << "no" << endl;
            break;
        }
        left++;
        right--;
    }
    if (left >= right)
        cout << "yes" << endl;
    
    return 0;
}
P1765 手机 - 洛谷
#include <bits/stdc++.h>
using namespace std;

int c[26] = {1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,4,1,2,3,1,2,3,4};
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    getline(cin, s);
    int cnt = 0;
    for (auto x : s)
    {
        if (x == ' ')
            cnt++;
        else
            cnt += c[x - 'a'];
    }
    cout << cnt << endl;
    
    return 0;
}
P1957 口算练习题 - 洛谷
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int i = 0;
    cin >> i;
    string op;
    string last;
    while(i--)
    {
        string ans;
        cin >> op;
        int n1, n2;
        int r = 0;
        if (op == "a" || op == "b" || op == "c")
        {
            cin >> n1 >> n2;
            ans += to_string(n1);
            if (op == "a")
            {
                r = n1 + n2;
                ans += "+";
                ans += to_string(n2);
                ans += "=";
                ans += to_string(r);
            }
            else if (op == "b")
            {
                r = n1 - n2;
                ans += "-";
                ans += to_string(n2);
                ans += "=";
                ans += to_string(r);
            }
            else
            {
                r = n1 * n2;
                ans += "*";
                ans += to_string(n2);
                ans += "=";
                ans += to_string(r);                
            }
            last = op;
        }
        else
        {
            n1 = stoi(op);
            ans += to_string(n1);
            cin >> n2;
            if (last == "a")
            {
                r = n1 + n2;
                ans += "+";
                ans += to_string(n2);
                ans += "=";
                ans += to_string(r);
            }
            else if (last == "b")
            {
                r = n1 - n2;
                ans += "-";
                ans += to_string(n2);
                ans += "=";
                ans += to_string(r);
            }
            else
            {
                r = n1 * n2;
                ans += "*";
                ans += to_string(n2);
                ans += "=";
                ans += to_string(r);                
            }
        }
        cout << ans << endl;
        cout << ans.size() << endl;
    }
    
    return 0;
}
#include <iostream>  
#include <string>  
using namespace std;  
int main()  
{  
	int n = 0;  
	cin >> n;  
	string op;  
	string num1;  
	string num2;  
	string last;  
	int ret = 0;  
	while (n--)  
	{  
		string ans;  
		cin >> op;  
		if (op == "a" || op == "b" || op == "c")  
		{  
			cin >> num1 >> num2;  
			int n1 = stoi(num1);  
			int n2 = stoi(num2);  
			ans += num1;  
			if (op == "a")  
				ret = n1 + n2, ans += "+";  
			else if (op == "b")  
				ret = n1 - n2, ans += "-";  
			else  
				ret = n1 * n2, ans += "*";  
			last = op;  
		}  
		else  
		{  
			num1 = op;  
			cin >> num2;  
			int n1 = stoi(num1);  
			int n2 = stoi(num2);
			ans += num1;  
			if (last == "a")  
				ret = n1 + n2, ans += "+";  
			else if (last == "b")  
				ret = n1 - n2, ans += "-";  
			else  
				ret = n1 * n2, ans += "*";  
			}  
			ans += (num2 + "=" + to_string(ret));  
			cout << ans << endl;  
			cout << ans.size() << endl;  
	}  
	return 0;  
}

http://www.kler.cn/a/580867.html

相关文章:

  • AI算法与应用 全栈开发 前端开发 后端开发 测试开发 运维开发
  • 【阿里云】操作系统控制台——体验与测评
  • c#面试题整理3
  • 探索高性能AI识别和边缘计算 | NVIDIA Jetson Orin Nano 8GB 开发套件的全面测评
  • FreeRTOS第18篇:FreeRTOS链表实现细节06_遍历指针(pxIndex)与调度器的高效协同
  • 2路模拟量同步输出卡、任意波形发生器卡—PCIe9100数据采集卡
  • Flutter中网络图片加载显示Image.network的具体用法
  • [免费]微信小程序(图书馆)自习室座位预约管理系统(SpringBoot后端+Vue管理端)(高级版)【论文+源码+SQL脚本】
  • Vue前端开发-Coupon组件
  • 时序数据库 InfluxDB 3.0 版本性能实测报告:写入吞吐量提升效果验证
  • 鸿蒙跨平台框架ArkUI-X
  • 后 Safe 时代:多签钱包安全新范式与防范前端攻击的新思路
  • Windows控制台函数:设置文字颜色样式函数SetConsoleTextAttribute()
  • SQL 窗口函数之lead() over(partition by ) 和 lag() over(partition by )
  • 批量将 Excel 转换 PDF/Word/CSV以及图片等其它格式
  • 手写Tomcat
  • C++ 内存模型
  • 从头开始开发基于虹软SDK的人脸识别考勤系统(python+RTSP开源)(三)
  • 1688商品列表商品详情API接口全面解析
  • upload-labs详解(13-20)文件上传分析