栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

xdoj 单词统计

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

xdoj 单词统计

         一开始比较懵逼,有想法不知道怎么具体实现,就一步步探索;在csdn逛了几圈只找到一个可行的算法,大佬的文章

感觉有点繁琐

就照着最初的想法终于敲出来了

暂时还没有找到更简单的算法,希望大佬多多指点

#include
#include
int main() 
{	

//初始化
	char string[100];
	gets(string);
	int len;
	len=strlen(string);
	strlwr(string);
	char word[8];
	gets(word);
//

	char temp[100];//建立临时数组来存每一个单词
	int i=0; 
	int an=0;//记录答案
	int step=0;//指针要移动的位数
	int sum=0;
	for (char *p=string;*p!='';p=p+step)//注意指针的移动,考虑每次循环后的目的
	{	

		step=0;
		for (char*s=p;*s!=' '; ++s)
		{
			if(s-string>len) break;//此处我改了半个小时才确定,记住这个位置
			temp[i]=*s;
			i++;
			step++;
		}
		temp[i]='';
		step++;
		sum+=step;
	
		//printf("#%s",temp);
	
		
	if (!(strcmp(temp,word)))
	{
		an++;
		strcpy(temp," ");//清空数组的小技巧
		i=0;
	}
	else{
		i=0;
		strcpy(temp," ");
	}

	}
//考虑一种特殊情况,从刚才那跳出来说明最后一个单词还没统计
	strcpy(temp," ");
	i=0;
	for(char*last=string+sum;*last!='';last++)
	{
		temp[i]=*last;
		i++;	
	}
	if (!(strcmp(temp,word)))
	{
		an++;
	}
	
	printf("%s %d",word,an);
	return 0;
 } 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/655772.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号