文章
13
粉丝
499
获赞
21
访问
135.5k
测试了“一个字母”,“多个空格”,“长英文字母”都可以运行,实在不知道此题的坑点在哪,一直都是0%
#include<stdio.h>
#include<string.h>
int main()
{
char str[100]={'\0'};
char s1[10]={'\0'};
char s2[10]={'\0'};
char *p = str;
int i=0,max=0;
int j=0;
int flag = 0,fla = 0;
while(gets(str))
{
while((*p)!='\0')
{
i = 0;
j = 0;
while((*p)!=' ')
{
i++;
s1[j++] = *p;
p++;
if(*p=='\0')
{
flag = 1;
break;
}
fla = 1;
}
p++;
if(i>max)
{
strcpy(s2,s1);
max = i;
}
if(fla==1||i==1)
{
fla = 0;
printf("%s %d\n",s1,i);
}
memset(s1,'\0',sizeof(s1));
}
if(flag == 1)
{
flag = 0;
printf("The longest word is: %s\n",s2);
p = str;
max = 0;
memset(s2,'\0',sizeof(s2));
memset(str,'\0',sizeof(str));
}
}
return 0;
}
登录后发布评论
老哥,这是我的C++代码,我测好多都没问题,你帮我测测?
#include<bits/stdc++.h>
using namespace std;
map<int,string> M;
int main(){
string str,temp_str;
int i,j,count;
while(getline(cin,str)){
M.clear();
i=0,count=0;
temp_str="";
for(j=i;str[j]!=' '&&j<str.size();j++){
temp_str+=str[j];
count++;
}
M[count]=temp_str;
cout<<temp_str<<" "<<count<<endl;
for(i=1;i<str.size();i++){
if(str[i-1]==' '&&str[i]!=' '){
count=0;
temp_str="";
for(j=i;str[j]!=' '&&j<str.size();j++){
temp_str+=str[j];
count++;
}
M[count]=temp_str;
cout<<temp_str<<" "<<count<<endl;
}
}
for(map<int,string>::reverse_iterator it=M.rbegin();it!=M.rend();it++){
cout<<"The longest word is: "<<it->second<<endl;
break;
}
}
return 0;
}
werewr wqerqweqwe ueeeeeeeeeeeeeeeeeee
werewr 6
wqerqweqwe@ 10
eeee 20
The longest word is: ueeeeeeeeeeeeeee
你代码确实有问题。。
老哥,你这代码有问题啊!
我测你的:
wqeqweqw qwewqewq wqqqqqqqqqqqqqqqqq
wqeqweqw 8
qwewqewq 8
qq 18 (这明显不对啊!)
The longest word is: wqqqqqqqqqqqqqqq
老哥,我用C++做的,本地也没问题