㈠將測資讀取為char型態:
/**********************************************************************************/
/* Problem: a011 "幼稚園的算數遊戲" from ACM 494 */
/* Language: CPP (388 Bytes) */
/* Result: AC(4ms, 372KB) judge by this@ZeroJudge */
/* Author: forever41200 at 2012-11-04 11:58:33 */
/**********************************************************************************/
#include<iostream>
using namespace std;
int main(){
int k;
char cha[1000]={0};
while(cin.getline(cha,1000)){
int sum=0;
bool word=false;
for(k=0;k<1000;k++){
if( (cha[k]>=65&&cha[k]<=90) || (cha[k]>=97&&cha[k]<=122) ){
word=true;
}
else
if(word==true){
sum++;
word=false;
}
cha[k]=0;
}
cout<<sum<<"\n";
}
return 0;
}
㈡將測資讀取為string型態:
/**********************************************************************************/
/* Problem: a011 "幼稚園的算數遊戲" from ACM 494 */
/* Language: CPP (432 Bytes) */
/* Result: AC(4ms, 424KB) judge by this@ZeroJudge */
/* Author: forever41200 at 2012-11-04 11:43:58 */
/**********************************************************************************/
#include<iostream>
#include<string.h>
using namespace std;
int main(){
string str;
int k;
while(getline(cin,str)){
char cha[1000]={0};
int sum=0;
bool word=false;
strcpy(cha,str.c_str());
for(k=0;k<1000;k++){
if( (cha[k]>=65&&cha[k]<=90) || (cha[k]>=97&&cha[k]<=122) ){
word=true;
}
else
if(word==true){
sum++;
word=false;
}
}
cout<<sum<<"\n";
}
return 0;
}