注意输入全是0的情况。
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; bool cmp(char a[],char b[]){ return ( strcmp(a,b)>0 ); } int main(){ char str[100000]; char *res[100000]; int i,cnt,l; int RES; int sign; while(NULL!=gets(str)){ cnt=0; l=strlen(str); if( strcmp(str,"#")==0 ) break; strcat(str," "); sign=0; for(i=l-1;i>=0;--i){ if(sign==0&&str[i]!=' '&&str[i+1]==' '){ str[i+1]='\0'; sign=1; } if(sign==1){ if(i==0&&str[i]!=' '){ res[cnt]=&str[i]; sign=0; cnt++; } if(str[i]!=' ') continue; if(str[i]==' '){ res[cnt]=&str[i+1]; sign=0; cnt++; } } } sort(res,res+cnt,cmp); RES=1; RES=RES<cnt?RES:cnt; for(i=1;i<cnt;++i){ if( strcmp(res[i],res[i-1])==0 ) continue; else RES++; } printf("%d\n",RES); } return 0; }