Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
其实用不着getline,一个一个字符串的输入就可以了,字符串和数字的对应也不用自己写,用map关联容器很方便,线性的实现不会比红黑树高校In Reply To:帮我找找错误 Posted by:00403022 at 2005-04-23 11:39:56 > #include<iostream.h> > #include<string.h> > char word[100][200]; //word存有价值的单词 > long value[100]; //value存单词的价值 > int m,n; //题目输入的m和n > int xh1,xh2; //循环变量 > char *string_to_search; //每次将读入的一行存在这个字符指针中,并对此行检索 > long total_value; //存总 工资? > char *p; //用在strstr函数中 > > void input_words() //输入部分 > { > cin>>m>>n; > for (xh1=0;xh1<m;xh1++) > { > cin.ignore(); > cin.getline(word[xh1],200,' '); > word[xh1][cin.gcount()]='\0'; > cin>>value[xh1]; > } > } > > void compute() > { > total_value=0; //初始化 > cin.ignore(); > string_to_search=new char[200]; > cin.getline(string_to_search,200); > string_to_search[cin.gcount()]='\0'; > while (*string_to_search!='.') //这段没结束,继续统计 > { > for (xh2=0;xh2<m;xh2++) > { > p=string_to_search; > p=strstr(p,word[xh2]); //检索字符串 > while (p!=NULL) > { > if (p==string_to_search && > p+strlen(word[xh2])==string_to_search+strlen(string_to_search) ) > total_value+=value[xh2]; > else > if (p>string_to_search) > { > if (p+strlen(word[xh2])==string_to_search+strlen(string_to_search) && > *(p-1)==' ') > total_value+=value[xh2]; > else > if (*(p-1)==' ' && *(p+strlen(word[xh2]))==' ') > total_value+=value[xh2]; > } > else > { > if (*(p+strlen(word[xh2]))==' ') total_value+=value[xh2]; > } > //以上部分判断是否有单词包含关系 如word 和 or ,并对total_value累加 > p++; //p向后移,为下一句做准备 > p=strstr(p,word[xh2]); //检索string_to_search中的下一个p > } > > } > delete string_to_search; > string_to_search=new char[200]; > cin.getline(string_to_search,200); > string_to_search[cin.gcount()]='\0'; > } > delete string_to_search; > cout<<total_value<<endl; > } > > > > void main() > { > input_words(); > for (xh1=0;xh1<n;xh1++) > compute(); > } > > > 另外,对这样的程序调试有没有好办法,fstream cin后好像cin.getline不能用了 Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator