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;elsecnt++;}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;elsecnt++;}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;elsecout << "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;elsecout << 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;elsecout << "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++;elsecnt += 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.pswp.cn/web/71937.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/71937.shtml
英文地址,請注明出處:http://en.pswp.cn/web/71937.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

MongoDB 觸發器實現教程

在傳統的關系型數據庫&#xff08;如 MySQL&#xff09;中&#xff0c;觸發器是一種強大的工具&#xff0c;它可以在特定的數據庫操作&#xff08;如插入、更新或刪除&#xff09;發生時自動執行一段代碼。然而&#xff0c;MongoDB 并沒有原生內置的觸發器概念。不過&#xff0…

C#控制臺應用程序學習——3.11

一、整型數字計算 如果我們想執行以下程序&#xff1a;程序提示用戶輸入一個數字并輸出 num 20 的結果&#xff0c;我們的思維應該是這樣的&#xff1a; using System;public class Class1 {public static void Main(string[] args){Console.WriteLine("Enter the first…

【C語言】指針篇

目錄 C 語言指針概述指針的聲明和初始化聲明指針初始化指針 指針的操作解引用操作指針算術運算 指針的用途動態內存分配作為函數參數 指針與數組數組名作為指針通過指針訪問數組元素指針算術和數組數組作為函數參數指針數組和數組指針指針數組數組指針 函數指針函數指針的定義和…

嵌入式音視頻通話SDK組件EasyRTC:全平臺設備兼容,智能硬件里的WebRTC調用實踐

在萬物互聯時代&#xff0c;智能硬件設備對實時音視頻通信的需求呈現爆發式增長。傳統基于PC或移動端的WebRTC方案難以滿足嵌入式設備在資源占用、低延遲傳輸和硬件適配等方面的特殊需求。本文將深入探討如何通過EasyRTC嵌入式音視頻通信SDK在嵌入式設備中實現高效的WebRTC視頻…

Aim Robotics電動膠槍:機器人涂膠點膠的高效解決方案

在自動化和智能制造領域&#xff0c;機器人技術的應用越來越廣泛&#xff0c;而涂膠和點膠作為生產過程中的重要環節&#xff0c;也逐漸實現了自動化和智能化。Aim Robotics作為一家專注于機器人技術的公司&#xff0c;其推出的電動膠槍為這一領域帶來了高效、靈活且易于操作的…

c語言筆記 數組進階題目的理解

題目&#xff1a;聲明一個二維 int 型數組 a&#xff0c;再聲明另一個一維數組指針數組 b&#xff0c;使該數組 b 的每一個指針分別指向二維數組 a 中的每一個元素(即每一個一維數組)&#xff0c;然后利用數組 b 計算數組 a 的和。 圖解&#xff1a;畫圖幫助理解 我們要清楚什…

Photo Works在線圖片編輯器:一鍵修復老照片,輕松煥新記憶

★【概況介紹】 今天突然收到我的朋友電腦出故障了,截圖給我,我一看就知道這個是缺少必要的組件引起的故障。結合這個問題,我來談談自己的解決思路和方法,希望能夠幫助到大家。幫助大家是我最開心的事情。以前只是幫朋友解決問題,沒有記錄下來,剛剛接觸到這個平臺,剛好可…

FANformer:融合傅里葉分析網絡的大語言模型基礎架構

近期大語言模型(LLM)的基準測試結果引發了對現有架構擴展性的思考。盡管OpenAI推出的GPT-4.5被定位為其最強大的聊天模型&#xff0c;但在多項關鍵基準測試上的表現卻不及某些規模較小的模型。DeepSeek-V3在AIME 2024評測中達到了39.2%的Pass1準確率&#xff0c;在SWE-bench Ve…

【 IEEE出版 | 快速穩定EI檢索 | 往屆已EI檢索】2025年儲能及能源轉換國際學術會議(ESEC 2025)

重要信息 主會官網&#xff1a;www.net-lc.net 【論文【】投稿】 會議時間&#xff1a;2025年5月9-11日 會議地點&#xff1a;中國-杭州 截稿時間&#xff1a;見官網 提交檢索&#xff1a;IEEE Xplore, EI Compendex, Scopus 主會NET-LC 2025已進入IEEE 會議官方列表!&am…

藍橋杯練題順序

有重復,適當選擇題目~共229道題&#xff01; 后續會發題解~ STL&#xff1a;9 3100 反轉字符串 [string簡單]---3100 -CSDN博客 2470 單調棧 [stack簡單]---2470 單調棧 [stack簡單]-CSDN博客 2254 括號匹配&#xff01; [stack簡單]---2254: 括號匹配&#xff01;-CSDN博客 …

react基礎語法視圖層類組件

react基礎語法視圖層&類組件 MVVM *區別mvc&mvvm 兩者的區別&#xff1a; 數據模型去渲染視圖。數據層改了&#xff0c;vue自己會監聽到幫我們拿最新的數據去渲染視圖&#xff1b;構建數據構建視圖&#xff0c;數據驅動的思想。這一套是非常相似的。 視圖中的內容改變&…

開發、科研、日常辦公工具匯總(自用,持續更新)

主要記錄匯總一下自己平常會用到的網站工具&#xff0c;方便查閱。 update&#xff1a;2025/2/11&#xff08;開發網站補一下&#xff09; update&#xff1a;2025/2/21&#xff08;補充一些AI工具&#xff0c;剛好在做AI視頻相關工作&#xff09; update&#xff1a;2025/3/7…

requests中post中data=None, json=None兩個參數區別

在 requests.post() 方法中&#xff0c;data 和 json 主要用于發送請求體&#xff0c;但它們的作用和格式有所不同。 1. data 參數 用途&#xff1a;用于發送表單數據或原始二進制數據。格式&#xff1a; 可以是 字典&#xff08;dict&#xff09;&#xff08;默認會編碼為 a…

51c大模型~合集10

我自己的原文哦~ https://blog.51cto.com/whaosoft/11547799 #Llama 3.1 美國太平洋時間 7 月 23 日&#xff0c;Meta 公司發布了其最新的 AI 模型 Llama 3.1&#xff0c;這是一個里程碑時刻。Llama 3.1 的發布讓我們看到了開源 LLM 有與閉源 LLM 一較高下的能力。 Meta …

架構演變 之 超市進化

1. 單機架構 → 小賣部&#xff08;夫妻店&#xff09; 場景&#xff1a;一個老板包攬所有工作——進貨、擺貨、收銀、打掃&#xff0c;店里只有一個小倉庫。對應架構&#xff1a;所有功能&#xff08;數據庫、業務邏輯、頁面&#xff09;都擠在一臺服務器上。問題&#xff1a…

ubuntu 和 RV1126 交叉編譯Mosqutiio-1.6.9

最近需要交叉編譯mosquitto&#xff0c;遇到一些小問題記錄一下。 1.眾所周知使用它自帶的Makefile編譯的時候&#xff0c;只需要在編譯前&#xff0c;指定它config.mk中的變量&#xff1a;CFLAGS頭文件路徑 和 LDFLAGS庫文件路徑就ok&#xff0c;例子如下&#xff1a; expor…

Photoshop 中如何快速摳圖?

Photoshop 中如何快速摳圖&#xff1f; 摳圖是 Photoshop 中的常見操作&#xff0c;無論是去除背景還是提取特定對象&#xff0c;都需要掌握高效的摳圖技巧。本文將介紹幾種快速摳圖的方法&#xff0c;幫助你輕松完成設計任務。 1. 快速選擇工具&#xff08;Quick Selection T…

解決 React 中的 Hydration Failed 錯誤

解決 React 中的 Hydration Failed 錯誤 React 的 服務器端渲染&#xff08;SSR&#xff09;通過在服務器端生成 HTML 并將其發送給客戶端&#xff0c;幫助提高頁面加載速度和搜索引擎優化&#xff08;SEO&#xff09;。然而&#xff0c;在進行 SSR 后&#xff0c;React 需要進…

如何使用postman來測試接口

一、postman的介紹與下載 可參考&#xff1a; https://blog.csdn.net/freeking101/article/details/80774271 二、api獲取網站 阿里云API應用市場 地址&#xff1a;云市場_鏡像市場_軟件商店_建站軟件_服務器軟件_API接口_應用市場 - 阿里云 三、具體測試過程 可模擬瀏覽…

數據庫系統概論(二)數據模型

數據庫系統概論&#xff08;二&#xff09;數據模型 數據庫系統概論&#xff08;二&#xff09;數據模型前言一、數據建模二、概念模型三、數據模型的三要素四、層次模型五、網狀模型六、關系模型 總結&#xff08;核心概念速記&#xff09;&#xff1a; 數據庫系統概論&#x…