2.2 字符串 参考代码

P5733 [深基6.例1] 自动修正

#include <cstdio>
#include <cstring>
const int N = 105;
char a[N];
int main()
{
// C风格字符串
// scanf在格式串后面的东西给的是内存地址
// 对于int这样的基本类型变量而言,变量名前面取&
// 对于数组a而言,数组名字a相当于&a[0]
scanf("%s", a); // 相当于从a[0]地址开始输入一个字符串
// 假设输入的是 Luogu4!
// 相当于 a[0]='L'; a[1]='u'; ... a[6]='!'; a[7]='\0';
// 字符数组里每一个字符都是一个char
// 'c'->'C' ASCII码差32
// 获取字符数组的“长度”
int len = strlen(a); // 相当于从a[0]开始统计长度(直到遇到'\0'为止)
for (int i = 0; i < len; i++) {
// a[i]
if (a[i]>='a' && a[i]<='z') { // 判断a[i]是否为小写
a[i]-=32; // a[i]=a[i]-'a'+'A';
}
}
printf("%s\n", a);
return 0;
}
#include <cstdio>
#include <cstring>
const int N = 105;
char a[N];
int main()
{
// C风格字符串
// scanf在格式串后面的东西给的是内存地址
// 对于int这样的基本类型变量而言,变量名前面取&
// 对于数组a而言,数组名字a相当于&a[0]
scanf("%s", a + 1); // 相当于从a[1]地址开始输入一个字符串
// 假设输入的是 Luogu4!
// 相当于 a[1]='L'; a[2]='u'; ... a[7]='!'; a[8]='\0';
// 字符数组里每一个字符都是一个char
// 'c'->'C' ASCII码差32
// 获取字符数组的“长度”
int len = strlen(a + 1); // 相当于从a[1]开始统计长度(直到遇到'\0'为止)
for (int i = 1; i <= len; i++) {
// a[i]
if (a[i]>='a' && a[i]<='z') { // 判断a[i]是否为小写
a[i]-=32; // a[i]=a[i]-'a'+'A';
}
}
printf("%s\n", a + 1);
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
// C++风格字符串
string a;
cin >> a;
int len = a.size(); // a.length()
for (int i = 0; i < len; i++) {
if (a[i]>='a' && a[i]<='z') {
a[i] -= 32;
}
}
cout << a << "\n";
return 0;
}

P1914 小书童——凯撒密码

#include <cstdio>
#include <cstring>
const int N = 55;
char a[N];
int main()
{
int n; scanf("%d%s", &n, a + 1);
int len = strlen(a + 1);
for (int i = 1; i <= len; i++) {
a[i] = (a[i]-'a'+n)%26+'a';
}
printf("%s\n", a + 1);
return 0;
}

P5015 [NOIP2018 普及组] 标题统计

#include <iostream>
#include <string>
using namespace std;
int main()
{
string title;
getline(cin, title);
int cnt = 0, len = title.size();
for (int i = 0; i < len; i++) {
if (title[i]!=' ') cnt++;
}
cout<<cnt<<"\n";
return 0;
}

P5734 [深基6.例6] 文字处理软件

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main()
{
int q;
string s;
cin >> q >> s;
for (int i = 0; i < q; i++) {
int op;
scanf("%d", &op);
if (op == 1) {
string t;
cin >> t;
s += t;
cout << s << "\n";
} else if (op == 2) {
int a, b;
scanf("%d%d", &a, &b);
s = s.substr(a, b); // 从位置a开始截取b个字符
cout << s << "\n";
} else if (op == 3) {
int a;
scanf("%d", &a);
string t;
cin >> t;
s.insert(a, t); // s在位置a处插入t
// 比如s="hello";
// s.insert(2,"abc"); s就会变成"heabcllo"
cout << s << "\n";
} else {
string t;
cin >> t;
// s.find(t) 返回s中t第一次出现的位置
// 找不到时返回 string::npos
// 不能直接当成-1来理解,但是可以和-1比较
// 当用一个int类型的变量接收find的结果时
// 如果找不到,就会得到-1
int res = s.find(t); // 相当于s.find(t,0) 0表示从0位置开始找
cout << res << "\n";
}
}
return 0;
}

P3741 小果的键盘

#include <iostream>
#include <string>
using namespace std;
int main() {
int n;
string s;
cin >> n >> s;
// 1. 统计原有的VK数量
int vk = 0;
for (int i = 0; i <= n - 2; i++) {
if (s[i] == 'V' && s[i+1] == 'K') vk++;
}
// 2.找VVV KKK 开头的KK 结尾的VV
// 0~n-1
// VVV KKK i:1~n-2 s[i-1]==s[i] && s[i]==s[i+1]
// KK i==0 s[i] s[i+1]
// VV i==n-1 s[i] s[i-1]
if (n > 1) {
for (int i = 0; i < n; i++) {
if (i==0 && s[i]=='K' && s[i+1]=='K') {
vk++; break;
}
if (i==n-1 && s[i]=='V' && s[i-1]=='V') {
vk++; break;
}
if (i>=1 && i<=n-2 && s[i-1]==s[i] && s[i]==s[i+1]) {
vk++; break;
}
}
}
cout<<vk<<endl;
return 0;
}

P1597 语句解析

#include <cstdio>
#include <cstring>
const int N = 300;
char s[N];
int a[3]; // a[0]表示变量a,a[1]表示变量b,...
int main()
{
scanf("%s", s+1);
int len = strlen(s+1);
for (int i = 1; i <= len; i+=5) {
// s[i]~s[i+4] ...:=...;
// s[i] 被赋值的变量
// s[i+3] 用来赋值的数/变量
// s[i]-'a' 0/1/2
if (s[i+3]>='0'&&s[i+3]<='9') {
a[s[i]-'a']=s[i+3]-'0'; // 数值赋值给变量
} else {
a[s[i]-'a']=a[s[i+3]-'a']; // 变量的值赋值给变量
}
}
printf("%d %d %d\n", a[0], a[1], a[2]);
return 0;
}

P1055 [NOIP2008 普及组] ISBN 号码

#include <cstdio>
char s[20];
int main()
{
scanf("%s", s);
int sum = 0, num = 0;
for (int i=0; i<=10; i++) {
if (s[i]>='0' && s[i]<='9') {
num++;
sum += num * (s[i]-'0');
}
}
sum%=11;
// s[12](char) sum(int)
// 'X' 10
// '0'~'9' 0~9
if (s[12]=='X' && sum==10) printf("Right\n");
else if (s[12]!='X' && s[12]-'0'==sum) printf("Right\n");
else {
if (sum==10) s[12]='X';
else s[12]=sum+'0';
printf("%s\n", s);
}
return 0;
}

P1125 [NOIP2008 提高组] 笨小猴

#include <cstdio>
#include <cstring>
const int N = 105;
char word[N];
int cnt[26]; // cnt[0]~cnt[25]对应'a'~'z'的出现次数
int main()
{
scanf("%s", word+1);
int len = strlen(word+1);
for (int i=1; i<=len; i++) {
cnt[word[i]-'a']++;
}
int maxn = 0, minn = 100;
for (int i = 0; i < 26; i++) {
if (cnt[i]>0 && cnt[i] > maxn) maxn = cnt[i];
if (cnt[i]>0 && cnt[i] < minn) minn = cnt[i];
}
int num = maxn-minn;
// 判断num是否为质数
// 因子分解 num=p1*p2?
int flag = 1;
for (int i=2; i<=num/i; i++) {
if (num % i == 0) {
// 不是质数
flag = 0; break;
}
}
if (num<2) flag=0; // 0和1需要单独判断
if (flag==1) printf("Lucky Word\n%d\n", num);
else printf("No Answer\n0\n");
return 0;
}

P1957 口算练习题

#include <cstdio>
#include <cstring>
char s[10], out[100];
int main()
{
int n; scanf("%d", &n);
char ch;
for (int i=1; i<=n; i++) {
scanf("%s", s);
int x, y;
if (s[0]>='a' && s[0]<='c') {
scanf("%d%d", &x, &y);
ch=s[0];
} else {
sscanf(s, "%d", &x); // 从字符串s中读入一个整数到x
scanf("%d", &y);
}
if (ch=='a') {
// 将算式输出到字符串out中
sprintf(out, "%d+%d=%d", x, y, x + y);
printf("%s\n%d\n", out, strlen(out));
} else if (ch == 'b') {
sprintf(out, "%d-%d=%d", x, y, x - y);
printf("%s\n%d\n", out, strlen(out));
} else {
sprintf(out, "%d*%d=%d", x, y, x * y);
printf("%s\n%d\n", out, strlen(out));
}
}
return 0;
}

P1308 [NOIP2011 普及组] 统计单词数

#include <iostream>
#include <string>
using namespace std;
int main()
{
string word;
getline(cin, word);
for (int i = 0; i < word.length(); i++)
if (word[i] >= 'A' && word[i] <= 'Z') word[i] += 'a' - 'A';
word = " " + word + " ";
string s;
getline(cin, s);
for (int i = 0; i < s.length(); i++)
if (s[i] >= 'A' && s[i] <= 'Z') s[i] += 'a' - 'A';
s = " " + s + " ";
int pos = s.find(word), cnt = 0, ans = 0;
while (pos != -1) {
if (cnt == 0) ans = pos;
++cnt;
pos = s.find(word, pos + 1);
}
if (cnt) cout << cnt << " " << ans << "\n";
else cout << "-1\n";
return 0;
}

P1553 数字反转(升级版)

#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
cin >> s;
if (int(s.find('.')) != -1) {
int idx = s.find('.');
int bg = idx - 1;
while (bg >= 0 && s[bg] == '0') --bg; // 找到小数点前最近的第一个非0位置
if (bg < 0) cout << "0";
for (int i = bg; i >= 0; --i) cout << s[i];
cout << ".";
bg = idx + 1;
while (bg < s.length() && s[bg] == '0') ++bg; // 找到小数点后最近的第一个非0位置
if (bg == s.length()) cout << "0";
for (int i = int(s.length()) - 1; i >= bg; --i) cout << s[i];
cout << endl;
} else if (int(s.find('/')) != -1) {
int idx = s.find('/');
int bg = idx - 1;
while (bg >= 0 && s[bg] == '0') --bg;
if (bg < 0) cout << "0";
for (int i = bg; i >= 0; --i) cout << s[i];
cout << "/";
bg = int(s.length()) - 1;
while (bg > idx && s[bg] == '0') --bg;
if (bg == idx) cout << "0";
for (int i = bg; i > idx; --i) cout << s[i];
cout << endl;
} else if (int(s.find('%')) != -1) {
int idx = s.find('%');
int bg = idx - 1;
while (bg >= 0 && s[bg] == '0') --bg;
if (bg < 0) cout << "0";
for (int i = bg; i >= 0; --i) cout << s[i];
cout << "%\n";
} else {
int bg = int(s.length()) - 1;
while (bg >= 0 && s[bg] == '0') --bg;
if (bg < 0) cout << "0";
for (int i = bg; i >= 0; --i) cout << s[i];
cout << endl;
}
return 0;
}
  • 本题输入的“数字”最多可以达到 20 位,解决本题应从字符串角度考虑

P1598 垂直柱状图

#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int cnt[30];
int main()
{
for (int i = 0; i < 4; ++i) {
string line;
getline(cin, line);
for (int j = 0; j < line.length(); ++j)
if (line[j] >= 'A' && line[j] <= 'Z') ++cnt[line[j] - 'A'];
}
int max_height = 0;
for (int i = 0; i < 26; ++i) max_height = max(max_height, cnt[i]);
for (int i = max_height; i >= 1; --i) {
int ed = 25;
for (; ed >= 0; --ed) if (cnt[ed] >= i) break;
for (int j = 0; j <= ed; ++j)
printf("%c%c", cnt[j] >= i ? '*' : ' ', j == ed ? '\n' : ' ');
}
for (int i = 0; i < 26; ++i) printf("%c%c", 'A' + i, i == 25 ? '\n' : ' ');
return 0;
}
  • 本题对输出图案的要求是每一行最后一个 * 之后没有任何多余字符,因此输出时先去找每一行最后一个 * 对应的字母,再考虑从头输出到这个字母为止
posted @   RonChen  阅读(64)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
点击右上角即可分享
微信分享提示