41(2023.12.3)字符串打题

41(2023.12.3)字符串打题_第1张图片

#include
using namespace std;
#include
int main()
{
	string str;
	int times = 0;
	getline(cin, str);
	for (int i = 0;i < str.length();i++)
	{
		if (str[i] == 'a' || str[i] == 'd' || str[i] == 'g' || str[i] == 'j' ||
			str[i] == 'm' || str[i] == 'p' ||
			str[i] == 't' || str[i] == 'w' || str[i] == ' ')times++;
		else if (str[i] == 'b' || str[i] == 'e' || str[i] == 'h' ||
			str[i] == 'k' || str[i] == 'n' || str[i] == 'q' ||
			str[i] == 'u' || str[i] == 'x')times += 2;
		else if (str[i] == 'c' || str[i] == 'f' || str[i] == 'i' ||
			str[i] == 'l' || str[i] == 'o' || str[i] == 'r' ||
			str[i] == 'v' || str[i] == 'y')times += 3;
		else if (str[i] == 's' || str[i] == 'z')times += 4;

	}
	cout << times << endl;
	
	system("pause");
	return 0;
}

41(2023.12.3)字符串打题_第2张图片 

#include
using namespace std;
int main()
{
	int n;
	cin >> n;
	char arr[101] = {};
	char ch;
	for (int i = 0;i < n;i++)
	{
		cin >> ch;
		arr[i] = ch;
	}
	int times = 0;
	for (int i = 0;i < n;i++)
	{
		if (i == 0 && arr[i] == 'K' && arr[i + 1] == 'K'&&(i+1)

41(2023.12.3)字符串打题_第3张图片 

#include
using namespace std;
#include
int main()
{
	string str;
	cin >> str;
	int l = str.length();
	int times1 = 0;
	int times2 = 0;
	for (int i = 0;i < l;i++)
	{
		if (str[i] == 'b')
		{
			times1++;
			if (str[i + 1] == 'o')
			{
				i++;
				if (str[i + 1] = 'y')i++;
			}
		}
		else if (str[i] == 'o')
		{
			times1++;
			if (str[i + 1] == 'y')
			{
				i++;
			}

		}
		else if (str[i] == 'y')times1++;
		else if (str[i] == 'g')
		{
			times2++;
			if (str[i + 1] == 'i')
			{
				i++;
				if (str[i + 1] == 'r')
				{
					i++;
					if (str[i + 1] == 'l')
					{
						i++;
					}
				}
			}
		}
		else if (str[i] == 'i')
		{
			times2++;
			if (str[i + 1] == 'r')
			{
				i++;
				if (str[i + 1] == 'l')
				{
					i++;
				}
			}
		}
		else if (str[i] == 'r')
		{
			times2++;
			if (str[i + 1] == 'l')
			{
				i++;
			}
		}
		else if (str[i] == 'l')
		{
			times2++;
		}
	}
	cout << times1 << endl << times2 << endl;
	system("pause");
	return 0;
}

41(2023.12.3)字符串打题_第4张图片 

#include
using namespace std;
#include
int main()
{
	string str;
	cin >> str;
	int l = str.length();
	int pos = -1;
	char ch = '1';
	for (int i = 0;i < l;i++)
	{
		if (str[i] == '.')
		{
			pos = i;
			ch = str[i];
		}
		else if (str[i] == '%')
		{
			pos = i;
			ch = str[i];
		}
		else if (str[i] == '/')
		{
			pos = i;
			ch = str[i];
		}
	}
	long long num1, num2;
	long long num3, num4;
	string str1, str2;
	long long result1=0, result2=0;
	if (pos == -1)
	{
		num1 = stoi(str);
		while (num1 != 0)
		{
			result1 = result1 * 10;
			num2 = num1 % 10;
			result1 += num2;
			num1 /= 10;
		}
		cout << result1;
	}
	else if (ch == '.')
	{
		string str1 = str.substr(0, pos );
		string str2 = str.substr(pos + 1, l - 1);
		num1 = stoi(str1);
		num2 = stoi(str2);
		while (num1 != 0)
		{
			result1 *= 10;
			num3 = num1 % 10;
			result1 += num3;
			num1 /= 10;
		}
		while (num2 != 0)
		{
			result2 *= 10;
			num4 = num2 % 10;
			result2 += num4;
			num2 /= 10;
		}
		cout << result1 << '.' << result2 << endl;
	}
	else if (ch == '/')
	{
		string str1 = str.substr(0, pos);
		string str2 = str.substr(pos + 1, l - 1);
		num1 = stoi(str1);
		num2 = stoi(str2);
		while (num1 != 0)
		{
			result1 *= 10;
			num3 = num1 % 10;
			result1 += num3;
			num1 /= 10;
		}
		while (num2 != 0)
		{
			result2 *= 10;
			num4 = num2 % 10;
			result2 += num4;
			num2 /= 10;
		}
		cout << result1 << '/' << result2 << endl;
	}
	else if (ch == '%')
	{
		str1 = str.substr(0, pos);
		num1 = stoi(str1);
		while (num1 != 0)
		{
			result1 = result1 * 10;
			num2 = num1 % 10;
			result1 += num2;
			num1 /= 10;
		}
		cout << result1 << "%" << endl;
	}
	system("pause");
	return 0;
}

//long long 太短了

//只能是字符串 

//难不倒我这聪明脑袋瓜

#include
using namespace std;
#include
int main()
{
	string str;
	cin >> str;
	int l = str.length();
	int pos = -1;
	char ch = '1';
	for (int i = 0;i < l;i++)
	{
		if (str[i] == '.')
		{
			pos = i;
			ch = str[i];
		}
		else if (str[i] == '%')
		{
			pos = i;
			ch = str[i];
		}
		else if (str[i] == '/')
		{
			pos = i;
			ch = str[i];
		}
	}
	long long num1, num2;
	long long num3, num4;
	string str1, str2;
	long long result1=0, result2=0;
	if (pos == -1)
	{
		for (int i = l - 1;i >= 0;i--)
		{
			if (str[i] != '0')
			{
				num1 = i;
				i = -1;
			}
		}
		for (int i = num1;i >= 0;i--)
		{
			cout << str[i];
		}
		cout << endl;
	}
	else if (ch == '.')
	{
		string str1 = str.substr(0, pos );
		string str2 = str.substr(pos + 1, l - 1);
		num1 = stoi(str1);
		num2 = stoi(str2);
		while (num1 != 0)
		{
			result1 *= 10;
			num3 = num1 % 10;
			result1 += num3;
			num1 /= 10;
		}
		while (num2 != 0)
		{
			result2 *= 10;
			num4 = num2 % 10;
			result2 += num4;
			num2 /= 10;
		}
		cout << result1 << '.' << result2 << endl;
	}
	else if (ch == '/')
	{
		string str1 = str.substr(0, pos);
		string str2 = str.substr(pos + 1, l - 1);
		num1 = stoi(str1);
		num2 = stoi(str2);
		while (num1 != 0)
		{
			result1 *= 10;
			num3 = num1 % 10;
			result1 += num3;
			num1 /= 10;
		}
		while (num2 != 0)
		{
			result2 *= 10;
			num4 = num2 % 10;
			result2 += num4;
			num2 /= 10;
		}
		cout << result1 << '/' << result2 << endl;
	}
	else if (ch == '%')
	{
		for (int i = l - 2;i >= 0;i--)
		{
			if (str[i] != '0')
			{
				num1 = i;
				i = -1;
			}
		}
		for (int i = num1;i >= 0;i--)
		{
			cout << str[i];
		}
		cout << "%" << endl;
	}
	system("pause");
	return 0;
}

 41(2023.12.3)字符串打题_第5张图片

 

 

 

 

 

你可能感兴趣的:(算法,c++,数据结构)