P5733 [深基6.例1] 自动修正
| #include <cstdio> |
| #include <cstring> |
| const int N = 105; |
| char a[N]; |
| int main() |
| { |
| |
| |
| |
| |
| scanf("%s", a); |
| |
| |
| |
| |
| |
| int len = strlen(a); |
| for (int i = 0; i < len; i++) { |
| |
| if (a[i]>='a' && a[i]<='z') { |
| a[i]-=32; |
| } |
| } |
| printf("%s\n", a); |
| return 0; |
| } |
| |
| #include <cstdio> |
| #include <cstring> |
| const int N = 105; |
| char a[N]; |
| int main() |
| { |
| |
| |
| |
| |
| scanf("%s", a + 1); |
| |
| |
| |
| |
| |
| int len = strlen(a + 1); |
| for (int i = 1; i <= len; i++) { |
| |
| if (a[i]>='a' && a[i]<='z') { |
| a[i]-=32; |
| } |
| } |
| printf("%s\n", a + 1); |
| return 0; |
| } |
| |
| #include <iostream> |
| #include <cstring> |
| using namespace std; |
| int main() |
| { |
| |
| string a; |
| cin >> a; |
| int len = a.size(); |
| 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); |
| cout << s << "\n"; |
| } else if (op == 3) { |
| int a; |
| scanf("%d", &a); |
| string t; |
| cin >> t; |
| s.insert(a, t); |
| |
| |
| cout << s << "\n"; |
| } else { |
| string t; |
| cin >> t; |
| |
| |
| |
| |
| |
| int res = s.find(t); |
| cout << res << "\n"; |
| } |
| } |
| return 0; |
| } |
P3741 小果的键盘
| #include <iostream> |
| #include <string> |
| using namespace std; |
| int main() { |
| int n; |
| string s; |
| cin >> n >> s; |
| |
| |
| int vk = 0; |
| for (int i = 0; i <= n - 2; i++) { |
| if (s[i] == 'V' && s[i+1] == 'K') vk++; |
| } |
| |
| |
| |
| |
| |
| |
| 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]; |
| int main() |
| { |
| scanf("%s", s+1); |
| int len = strlen(s+1); |
| for (int i = 1; i <= len; i+=5) { |
| |
| |
| |
| |
| |
| 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; |
| |
| |
| |
| 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]; |
| 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; |
| |
| |
| int flag = 1; |
| for (int i=2; i<=num/i; i++) { |
| if (num % i == 0) { |
| |
| flag = 0; break; |
| } |
| } |
| if (num<2) flag=0; |
| 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); |
| scanf("%d", &y); |
| } |
| if (ch=='a') { |
| |
| 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; |
| 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; |
| 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; |
| } |
- 本题对输出图案的要求是每一行最后一个 * 之后没有任何多余字符,因此输出时先去找每一行最后一个 * 对应的字母,再考虑从头输出到这个字母为止
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?