HDU2072

来源:互联网 发布:泸沽湖摩梭人骗局知乎 编辑:程序博客网 时间:2024/06/07 10:50

          这题WA了多次。

         主要有四方面的问题:①题目说每篇小文章占一行,也就是说每篇文章的字符总数不大于80。(猜想:控制台每行为80个字符长度,之前也有类似一题,

   也是采取80个字符长度,AC了。)②测试数据,应该说每次都会在测试数据上出点问题,花的时间最多的也是调试程序。编写程序之前应该考虑程序对边界数据的处理!

   ③题目说:遇到“#"输入结束是指输入”#“之后,程序结束。并不是每行输入结束的标志!④操作字符串的能力急待提高!

 

          给出一组测试数据:

                                        you are my friend (you之前和friend 之后均无空格)

                                                you are my friend(you之前空格数大于1)

                                        you are          my friend(are和my之前空格数多于1)

                                        you are my friend                         (friend之后有很多空格)

                                        #

 

                                        4

                                        4

                                        4

                                        4

 

 

        贴上我的代码:

    

#include<iostream>#include<string>using namespace std;int main () {   char *c=NULL,*d=NULL;  //指针初始化!!!   char a[81];   char b[41][81];   int i,j,k,len,count;   while(gets(a))   {   if(a[0]=='#') break;   len=strlen(a);   for(i=len-1;i>=0;i--)  //最后的空格,去掉   {   if(a[i]==' ')   a[i]=NULL;   else   break;   }   len=strlen(a);   for(i=0;i<len;i++) //开始的空格,去掉   {   if(a[i]==' ')   a[i]=NULL;   else   {   d=a+i;   break;   }   }   len=strlen(d);    //中间的空格,赋NULL,并记录单词   d[len]=' ';   d[len+1]=NULL;   c=d;   j=0;   for(i=0;i<=len;i++)   {   if(d[i]==' ')     {   d[i]=NULL;   if(d[i+1]!=' ')   {   strcpy(b[j],c);   j++;   c=d+i+1;   }   }   }   count=j;       //处理单词,相同的记一次   for(i=0;i<j;i++)            {   for(k=i+1;k<j;k++)   {   if(strcmp(b[i],b[k])==0 && b[k][0]!='#')   {   count--;   b[k][0]='#';    //#是标志,表示该相同单词已被减过,不能再减   }   }   }   cout<<count<<endl;   }   return 0;}


    


    

       还有一个简单的代码,用到了STL中的set。(个人认为初学者不能过多依赖库中的东西,自己多练习。)

       代码如下(非本人写出,源自网络):

#include<iostream>#include<string>#include<set>#include<sstream>using namespace std;set<string> temp;int main(){string row,input;while( getline(cin,row) && row!="#" ){temp.clear();stringstream str(row);while(str>>input)temp.insert(input);cout<<temp.size()<<endl;//temp.clear();}return 0;}


 

     欢迎拍砖!

    

        

                               

                          

 

 

                                                           

原创粉丝点击