#14217: 請問這樣想法哪裡有錯嗎


a2583669 (YuFong)


先附上程式碼:

#include <iostream>
using namespace std;
int main(){
string a;
while(getline(cin,a))
{
int counter = 1;
for(int i = 0 ; i < a.length() ; i++){
if(a[i] == ' ')
{
counter++;
}
}
cout << counter << endl;
}
return 0;
}

 

單字跟單字中間會有 空白 用空白來算這邊有幾個單字

#14218: Re:請問這樣想法哪裡有錯嗎


asnewchien@gmail.com (david)


先附上程式碼:

#include
using namespace std;
int main(){
string a;
while(getline(cin,a))
{
int counter = 1;
for(int i = 0 ; i < a.length() ; i++){
if(a[i] == ' ')
{
counter++;
}
}
cout << counter << endl;
}
return 0;
}

 

單字跟單字中間會有 空白 用空白來算這邊有幾個單字


這樣的判斷還不夠


a  ,,  d

這樣會算到符號

#14219: Re:請問這樣想法哪裡有錯嗎


a2583669 (YuFong)


先附上程式碼:

#include
using namespace std;
int main(){
string a;
while(getline(cin,a))
{
int counter = 1;
for(int i = 0 ; i < a.length() ; i++){
if(a[i] == ' ')
{
counter++;
}
}
cout << counter << endl;
}
return 0;
}

 

單字跟單字中間會有 空白 用空白來算這邊有幾個單字


這樣的判斷還不夠


a  ,,  d

這樣會算到符號

我把原本的code更改了


#include <iostream>

using namespace std;

int main(){

string a;

while(getline(cin,a))

{

int counter = 1;

for(int i = 0 ; i < a.length() ; i++){

if(i < a.length()-1)

{

if(a[i] == ' ' && ((a[i+1] >= 'A' && a[i+1] <= 'Z') || (a[i+1] >= 'a' && a[i+1] <= 'z')))

{

 

counter++;

}

}

}

cout << counter << endl;

}

return 0;

}

 

這樣還是錯line:6  

正確答案:21

我的答案:19

 

請問有人知道line6是甚麼樣的測資嗎

#14220: Re:請問這樣想法哪裡有錯嗎


asnewchien@gmail.com (david)


你這樣寫

ax,by,cz

算幾個

#14221: Re:請問這樣想法哪裡有錯嗎


a2583669 (YuFong)


你這樣寫

ax,by,cz

算幾個



這樣算1個  

請問這應該是3個嘛!?

#14222: Re:請問這樣想法哪裡有錯嗎


asnewchien@gmail.com (david)


你這樣寫

ax,by,cz

算幾個



這樣算1個  

請問這應該是3個嘛!?


我覺得是 3 個。

#14223: Re:請問這樣想法哪裡有錯嗎


a2583669 (YuFong)


謝謝你 我解決了 真的是您說的那個問題。