洛谷题单——字符串篇

P5733 【深基6.例1】自动修正

#include<stdlib.h>
#include<iostream>
#include<cstring>
using namespace std;

char str[10000];
int main() {
	cin >> str;
	for (int i = 0; i < strlen(str); i++) {
		if (str[i] >= 'a' && str[i] <= 'z')
			str[i] -= 32;
		cout << str[i];
	}

	return 0;
}
#include<stdlib.h>
#include<iostream>
#include<cstring>
using namespace std;

string str;

int main() {
	cin >> str;
	for (int i = 0; i < str.size(); i++) {
		if (str[i] >= 'a' && str[i] <= 'z')
			str[i] = toupper(str[i]);
		cout << str[i];
	}

	return 0;
}

P1914 小书童——凯撒密码

#include<stdlib.h>
#include<iostream>
#include<cstring>
using namespace std;

string str;
int n;

int main() {
	cin >> n>> str;
	for (int i = 0; i < str.size(); i++) {
		if (str[i] >= 97 && str[i] <= 122 - n)
			str[i] += n;
		else if (str[i] > 122 - n)
			str[i] = 96 + (n - (122 - int(str[i])));
		cout << str[i];
	}

	//a是97,z是122

	return 0;
}

P1125 [NOIP2008 提高组] 笨小猴 题解

#include<stdlib.h>
#include<iostream>
#include<cstring>
using namespace std;

string str;
int ans[123], maxn = 0, minn = 1000;

int is_prime(int num) {
	for (int i = 2; i < num; i++)
		if (num % i == 0)
		{
			break;
			return 0;
		}
	return 1;
}

int main() {
	cin >> str;
	for (int i = 0; i < str.size(); i++) {
		ans[int(str[i])]++;
	}
	for (int i = 97; i < 123; i++) {
		if (ans[i] > maxn)
			maxn = ans[i];
		else if (ans[i] < minn && ans[i] != 0)
			minn = ans[i];
	}

	if (is_prime(maxn - minn) && (maxn - minn) != 1&& (maxn - minn) != 0)
		cout << "Lucky Word\n" << maxn - minn;
	else
		cout << "No Answer\n" << "0";

	return 0;
}

P1957 口算练习题

#include<stdlib.h>
#include<iostream>
#include<cstring>
using namespace std;

int n, num1, num2;
char type, line[1000];

int digit_length(int num) {
	int cnt = 0;
	if (num < 0) {
		num = abs(num);
		cnt++;
	}
	else if (num == 0) {
		return 1;
	}

	for (int i=num; i > 0;cnt++) {
		i = i / 10;
	}
	return cnt;
}
void process(char type, int num1, int num2) {
	if (type == 'a') {
		cout << num1 << "+" << num2 << "=" << num1 + num2 << endl;
		cout << digit_length(num1) + digit_length(num2) + digit_length(num1 + num2) + 2 << endl;
	}
	else if (type == 'b') {
		cout << num1 << "-" << num2 << "=" << num1 - num2 << endl;
		cout << digit_length(num1) + digit_length(num2) + digit_length(num1 - num2) + 2 << endl;
	}
	else if (type == 'c') {
		cout << num1 << "*" << num2 << "=" << num1 * num2 << endl;
		cout << digit_length(num1) + digit_length(num2) + digit_length(num1 * num2) + 2 << endl;
	}
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		if (!(cin >> num1))//利用了cin的性质 
		{
			cin.clear();
			cin >> type >> num1;
		}
		cin >> num2;
		process(type, num1, num2);
	}

	return 0;
}

P5015 [NOIP2018 普及组] 标题统计

#include<stdlib.h>
#include<iostream>
#include<string>
using namespace std;

string s;
int cnt = 0;
int main() {
	getline(cin, s);//用getline函数读入一行
	for (int i = 0; i < s.size(); i++) {
		if (s[i] != ' ' && s[i] != '\n')
			cnt++;
	}
	cout << cnt;
    return 0;
}

P5734 【深基6.例6】文字处理软件

#include<stdlib.h>
#include<iostream>
#include<string>

using namespace std;

string s, str;
int n, a, b, q;
int main() {
	cin >> q >> s;
	for (int i = 0; i < q; i++) {
		cin >> n;
		if (n == 1) {
			cin >> str;
			s = s + str;
			cout << s<<endl;
		}
		else if (n == 2) {
			cin >> a >> b;
			s = s.substr(a, b);
			cout << s << endl;
		}
		else if (n == 3) {
			cin >> a >> str;
			s.insert(a, str);
			cout << s << endl;
		}
		else if (n == 4) {
			cin >> str;
			if (s.find(str) < s.size())
				cout << s.find(str) << endl;
			else
				cout << -1 << endl;
		}
	}
    return 0;
}

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

#include <iostream>
#include <string>

using namespace std;

string a, b;
int main() {
    getline(cin, a);
    getline(cin, b);

    for (int i = 0; i < a.length(); ++i) {
        a[i] = tolower(a[i]);
    }
    for (int i = 0; i < b.length(); ++i) {
        b[i] = tolower(b[i]);
    }
    a = ' ' + a + ' ';
    b = ' ' + b + ' ';
    //先看看会不会找不到,用a.find()和string::npos
    if (b.find(a) == string::npos) {
        cout << -1 << endl;
    }
    //如果找得到
    else {
        int alpha = b.find(a);
        int beta = b.find(a), s = 0;
        while (beta != string::npos) {
            ++s;//计数器
            beta = b.find(a, beta + 1);
        }
        cout << s << " " << alpha << endl;
    }
    return 0;
}

R128814774 记录详情

#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;

string s;
int num[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 ans = 0;
int main() {
	getline(cin, s);
	for (int i = 0; i < s.size(); i++) {
		if (s[i] >= 97 && s[i] <= 122)
			ans = ans + num[s[i] - 97];
		if (s[i] == ' ')
			ans++;
	}

	cout<<ans;
}

P3741 honoka的键盘

#include<iostream>
#include<string>
using namespace std;

int num = 0, ans = 0;
string s;
int main()
{
	cin >> num >> s;
	for (int i = 0; i < s.size(); i++)
	{
		if (s[i] == 'V' && s[i + 1] == 'K')
		{
			ans++;
			s[i] = 'X';
			s[i + 1] = 'X';
		}
	}
	for (int i = 0; i < s.size(); i++)
	{
		if (s[i] != 'X' && s[i] == s[i + 1])
		{
			ans++;
			break;
		}
	}
	printf("%d", ans);
	return 0;
}

P1321 单词覆盖还原

#include<iostream>
#include<stdlib.h>
#include<string>

using namespace std;
int boy=0, girl=0, len;   
string st;  
int main()
{
    getline(cin, st);
    len = st.size();    

    for (int i = 0; i < len - 2; i++) 
        if (st[i] == 'b' || st[i + 1] == 'o' || st[i + 2] == 'y')boy++;

    for (int i = 0; i < len - 3; i++) 
        if (st[i] == 'g' || st[i + 1] == 'i' || st[i + 2] == 'r' || st[i + 3] == 'l')girl++;

    cout << boy << endl << girl;  
    return 0;
}

P1553 数字反转(升级版)

P1603 斯诺登的密码

#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;

char dic[30][20] = { "zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty","a","both","another","first","second","third" };//对应
int di[30] = { 0,1,4,9,16,25,36,49,64,81,00,21,44,69,96,25,56,89,24,61,0,1,4,1,1,4,9 };
unsigned long long int a[10], top, flag;
int i, j;
char s[100];
int main()
{
    for (i = 1; i <= 6; i++)
    {
        scanf("%s", &s);//%s读入遇到空格就停止
        for (j = 1; j <= 26; j++)
        {
            if (!strcmp(s, dic[j]))//strcmp(s1,s2);如果他们相同,返回0
            {
                a[++top] = di[j];//用数组存储
                break;//立即停止寻找
            }
        }
    }
    sort(a + 1, a + top + 1);//贪心,使越小的数越靠前输出
    for (i = 1; i <= top; i++)
    {
        if (flag)//如果不是第一位
        {
            printf("%.2d", a[i]);//限制格式输出
        }
        else
        {
            if (a[i])
            {
                printf("%d", a[i]);
                flag = 1;
            }
        }
    }
    if (!flag)
        printf("0");//特判
    return 0;
}

P1200 [USACO1.1] 你的飞碟在这儿 Your Ride Is Here

#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
string fir, sec;
int pro1 = 1, pro2 = 1;


int main() {
	getline(cin, fir);
	getline(cin, sec);
	for (int i = 0; i < fir.size(); i++) {
		pro1 = pro1 * int(fir[i] - 64);
	}
	for (int i = 0; i < sec.size(); i++) {
		pro2 = pro2 * int(sec[i] - 64);
	}

	if (pro1 % 47 == pro2 % 47)
		cout << "GO";
	else
		cout << "STAY";
}

P1597 语句解析

#include <iostream>
#include <string>
using namespace std;	
int a, b, c;	
string s;	
int main()
{
    cin >> s;	
    for (int i = 0; i < s.size(); i += 5)	
    {	
        int vf;	
        char cvf = s[i + 3], cvt = s[i];	
        if (cvf == 'a') vf = a;	//所赋值为a
        else if (cvf == 'b') vf = b;	
        else if (cvf == 'c') vf = c;	
        else vf = cvf - '0';	
        if (cvt == 'a') a = vf;
        if (cvt == 'b') b = vf;	
        if (cvt == 'c') c = vf;	
    }
    cout << a << " " << b << " " << c;	//输出
    return 0;
}

P1598 垂直柱状图

#include<stdlib.h>
#include<iostream>
#include<string>
using namespace std;
string s;
int bucket[100],maxn=0;
int main() {
	for (int i = 0; i < 4; i++) {
		getline(cin, s);
		for (int j = 0; j < s.size(); j++) {
			if (s[j] >= 'A' && s[j] <= 'Z') {
				bucket[s[j]]++;
			}
		}
	}
	
	for (int i = 'A'; i <= 'Z'; i++) {
		maxn = max(maxn, bucket[i]);
	}
	for (int i = maxn; i > 0; i--) {
		for (int j = 'A'; j <= 'Z'; j++) {
			if (bucket[j] >= i)
				printf("* "); 
			else 
				printf("  ");
		}
		cout << endl;
	}
	for (int i = 'A'; i <='Z'; i++)
		printf("%c ", i);//输出a~z

}

### 关于洛谷中的C语言实现 对于洛谷上的编程目,通常可以通过阅读目描述并理解其需求来设计解决方案。以下是一个通用的方法论用于解决此类问: #### 方法概述 1. **分析目要求**:仔细阅读目说明,明确输入输出格式以及边界条件。 2. **算法选择**:基于目复杂度选取合适的算法数据结构。 3. **编码实现**:使用C语言编写代码,并注意语法细节。 --- ### 示例解析 假设我们正在处理的是一个典型的洛谷目——计算函数 `fun(a, b)` 的值[^4]。此的核心在于逻辑分支的选择与数值比较操作。 ```c #include <stdio.h> int fun(int a, int b) { if (b >= 0 && a >= 0) { return a; } else if (b < 0 && a >= 0) { return 0; } else if (a < 0 && b < 0) { return a; } return 0; // 默认返回值 } int main() { int a, b; scanf("%d %d", &a, &b); printf("%d\n", fun(a, b)); return 0; } ``` 上述代码实现了样例中提到的功能,即根据不同情况下的参数组合返回相应的结果[^4]。 --- ### 另一实例:Lucky Word 判断 针对 Lucky Word 的判定问,核心思路是对字符串中字符频率进行统计,并进一步验证差值是否为质数[^1]。 ```c #include <stdio.h> #include <stdbool.h> #include <string.h> bool is_prime(int n) { if (n <= 1) return false; for (int i = 2; i * i <= n; ++i) { if (n % i == 0) return false; } return true; } int main() { char s[100]; int freq[26] = {0}; scanf("%s", s); for (size_t i = 0; i < strlen(s); ++i) { if ('a' <= s[i] && s[i] <= 'z') { freq[s[i] - 'a']++; } } int min_freq = 1e9, max_freq = 0; for (int i = 0; i < 26; ++i) { if (freq[i]) { min_freq = fmin(min_freq, freq[i]); max_freq = fmax(max_freq, freq[i]); } } int diff = max_freq - min_freq; if (is_prime(diff)) { printf("Lucky Word\n%d\n", diff); } else { printf("No Answer\n0"); } return 0; } ``` 这段代码完成了对给定字符串的字母频次统计,并最终依据特定规则判断是否存在幸运词及其对应的差异值[^1]。 --- ### 编程注意事项 当提交代码至在线评测平台时可能出现多种状态反馈,具体含义如下: - Accepted 表明程序运行无误且输出完全匹配预期; - Wrong Answer 意味着实际输出与测试用例不符; - Time Limit Exceeded 和 Memory Limit Exceeded 分别表示超出时间或内存限制; - Runtime Error 常见原因包括但不限于数组越界访问、非法解引用等; - Compile Error 显示编译阶段存在问,需修正源码后再尝试提交[^2]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值