FOJ 2025 count

一,问题描述

二,问题分析

1.对于 one 遍历字符串 如果遇到字符 ‘o’ ,查看接下来的两位字符是不是 ‘n’ 和 ‘e’,注意边界条件的判断

其他同理  

三,代码解答

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

int onefun(string str) {
	int res = 0;
	if (str.size() < 3) return res;		//如果输入的字符串的长度小于3,则直接返回0
	for (int i = 0; i < str.size()-2; i++) {
		if (str[i] == 'o' && str[i+1]=='n' &&str[i+2]=='e') {
			res++;
		}
		else
		{
			continue;
		}
	}
	return res;
}


int twofun(string str) {
	int res = 0;
	if (str.size() < 3) return res;		//如果输入的字符串的长度小于3,则直接返回0
	for (int i = 0; i < str.size() - 2; i++) {
		if (str[i] == 't' && str[i + 1] == 'w' && str[i + 2] == 'o') {
			res++;
		}
		else
		{
			continue;
		}
	}
	return res++;
}

int threefun(string str) {
	int res = 0;
	if (str.size() < 5) return res;
	for (int i = 0; i < str.size() - 4; i++) {			//注意遍历边界的判断
		if (str[i] == 't' && str[i + 1] == 'h' && str[i + 2] == 'r' && str[i + 3] == 'e'&& str[i + 4] == 'e') {
			res++;
		}
		else
		{
			continue;
		}
	}
	return res++;
}

int main() {
	string numstr;
	while (getline(cin, numstr)) {
		int onenumber = onefun(numstr);
		int twonumber = twofun(numstr);
		int threenumber = threefun(numstr);
		cout << "one" << " " << onenumber << endl;
		cout << "two" << " " << twonumber << endl;
		cout << "three" << " " << threenumber << endl;
	}
	return 0;
}
发布了54 篇原创文章 · 获赞 14 · 访问量 3579

猜你喜欢

转载自blog.csdn.net/q2511130633/article/details/105227264
今日推荐