这道是要是全手撸的话的确会很麻烦……
#include <cctype> #include <iostream> #include <set> #include <sstream> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); const set<string> exclude{"the", "a", "an", "of", "for", "and"}; string line; int n; cin >> n; cin.get(); for (int i = 0; i < n; ++i) { getline(cin, line); for (auto &i : line) i = tolower(i); string temp; istringstream in(line); int cnt = 0; while (in >> temp) { if (exclude.find(temp) == exclude.end()) ++cnt; } cout << "case #" << i << ":\n" << cnt << '\n'; } }
char ban[6][5]={“the”,”a”,”an”,”of”,”for”,”and”}; int makeit(char *a){ int sum=0; int length=strlen(a); char temp[100][100]; int cnt=0,ct=0; bool flag=false; for(int i=0; i<=length; i++){ if(isspace(a[i])||a[i]==’\0’){ temp[ct][cnt]=’\0’; for(int i=0; i<6; i++){ if(strcmp(temp[ct],ban[i])==0) flag=true; } ct++; cnt=0; if(!flag) sum++; flag=false; } else if(!isspace(a[i])){ temp[ct][cnt++]=a[i]; } } return sum; } int main(){ int t,ans; char vis[200]; scanf(“%d”,&t); getchar(); for(int k=0; k<t; k++){ gets(vis); int length=strlen(vis); for(int i=0; i=’A’&&vis[i]<=’Z’)vis[i]+=32; ans=makeit(vis); printf(“case #%d:\n%d\n”,k,ans); } }
几个地方注意下(含AC程序)
using namespace std; const string Ignore[6]={“the”,”a”,”an”,”of”,”for”,”and”}; int T; string line; string Tolower(const string& strs) { string ret=”“; int ll=strs.length(); for(int i=0;i<ll;i++) ret+=tolower(strs[i]); return ret; } int ignore(const string& strs) { for(int i=0;i<6;i++) if(Tolower(Ignore[i])==Tolower(strs)) return 1; return 0; } int main() { cin>>T; getline(cin,line); for(int step=0;step>tmp) if(!ignore(tmp)) ans++; printf(“case #%d:\n%d\n”,step,ans); } return 0; }
1.全部转为小写比较 2.stringstream+getline读取(如果用C++的话)
#include <bits/stdc++.h> using namespace std; set<string> se{"the","a","an","of","for","and"}; int main() { int n,cnt = 0;cin>>n; string s; getchar(); //消除回车影响 while(n--) { getline(cin,s); for(int i = 0;i < s.length();i++) { s[i] = tolower(s[i]); } int num = 0; printf("case #%d:\n",cnt++); int i,j; for(i = 0;i < s.length();i = j+1) { for(j = i;j < s.length() && s[j] != ' ';j++); //j指向空格位置 string danci = s.substr(i,j-i); if(se.find(danci) == se.end()) //没找到 num++; } cout<<num<<endl; } return 0; }
大写字母也要考虑 怎么一下子发了三条= = [em:14][em:14][em:14]
真的是郁闷,每次都被坑,说好的字符串长度小于等于100呢……
几个地方注意下(含AC程序) 3.常量数组的使用
大写字母也要考虑 strlwr
大写字母也要考虑 [em:02][em:02]
大写字母也要考虑 是的全要考虑= =差点被整死。。
大写字母也要考虑 还是 全部字母大写 就好了。。。
行末 经验证 没多余空格。。。 [em:13][em:13]
大写字母也要考虑 贴下代码
using namespace std; char word[20][10]= {“the”,”a”,”an”,”of”,”for”,”and”,”THE”,”The”,”A”,”An”,”AN”,”OF”,”Of”,”For”,”FOR”,”And”,”AND”}; bool cmp(char *s) { bool sign=true; for(int i=0; i<17; i++) if(strcmp(word[i],s)==0) sign=false; return sign; } void solve() { char s[M]; gets(s); char tmp[N]; int cnt=0,start=0,len=strlen(s); while(startstart&&s[len-1]==’ ‘) –len; for(int i=start; i<len; i++) { int k=0; for(int j=i; s[j]!=’ ‘&&s[j]; j++,i++) tmp[k++]=s[j]; tmp[k]=0; if(cmp(tmp))cnt++; } cout<<cnt<<endl; } int main() { int i,t; scanf(“%d”,&t); getchar(); for (i=0; i<t; i++) { printf(“case #%d:\n”,i); solve(); } return 0;
}
全部变小写
using namespace std;
int main() { int cs; cin>>cs; for(int i=0;i>c) { if(getchar()==’\n’){strcpy(s[k++],c);break;} strcpy(s[k++],c);
} int mark=0; for(int n=0;n<k;n++) { for(int j=0;j<100;j++) { if(s[n][j]>='A'&&s[n][j]<='Z'){s[n][j]=s[n][j]-('A'-'a');} } if(strcmp(s[n],"the")==0||strcmp(s[n],"a")==0||strcmp(s[n],"an")==0||strcmp(s[n],"of")==0||strcmp(s[n],"for")==0||strcmp(s[n],"and")==0) mark+=1; }cout<<"case #"<<i<<":"<<endl<<k-mark<<endl; }
这道题用python比较好处理,把每个分割处理的单词变成小写,然后检测是不是在stopwords里面,如果不在就加1。遍历全部的单词即可得到最终的单词。
stop_words = [‘the’,’a’,’an’,’of’,’for’,’and’] for i in range(int(input())): len_num = 0 one_s = input().split(‘ ‘) for one_word in one_s: if one_word.lower() not in stop_words: len_num += 1 print(‘case #’+str(i)+’:’) print(len_num)
using namespace std; void tolowcase(char word){ int n=0; while(word[n]!=’\0’){ word[n]=(word[n]<97)?word[n]+32:word[n]; n++; } } int strlen(char str){ int n=0; while(str[n]!=’\0’){ n++; } return n; } int equalstr(char s1,char s2){ if(strlen(s1)!=strlen(s2))return 0; else{ for(int i=0;i<=strlen(s1)-1;i++){ if(s1[i]!=s2[i])return 0; } return 1; } } int iskeyword(char word){ tolowcase(word); char keys[6]{“and”,”a”,”an”,”the”,”of”,”for”}; for(int i=0;i<=5;i++){ if(equalstr(word,keys[i]))return 1; } return 0; } int getstr(char *str){ char c=getchar(); if(c!=’\n’){ str[0]=c; int i=1; while((c=getchar())!=’ ‘){ if(c==’\n’){ str[i]=’\0’; return -1; }; str[i]=c; i++; } str[i]=’\0’; return 0; }else{ return -1; } } int main(){ int N; cin>>N; getchar(); for(int i=0;i<=N-1;i++){ int n=0; char word[20]; while(1){ int x=getstr(word); if(!iskeyword(word)){ n++; } if(x==-1)break; } cout<<”case #”<<i<<”:”<<endl<<n<<endl; } }
#include <bits/stdc++.h> using namespace std; int T, k; void ini(vector<string> &v) { v.push_back("the"); v.push_back("a"); v.push_back("an"); v.push_back("of"); v.push_back("for"); v.push_back("and"); } void lower(string &s) { for(int i=0;i<s.length();++i) if(s[i]>='A'&&s[i]<='Z') s[i]+=32; } int main() { vector<string> v;ini(v); int l=v.size(); cin>>T; getchar(); while(T--){ string s;getline(cin, s); s+=' ';//哨兵 int len=s.length(); lower(s); string temp=""; int ans=0; for(int i=0;i<len;++i){ if(s[i]==' ') { int flag=0; for(int i=0;i<l;++i) if(temp==v[i]) flag=1; if(!flag) ans++; temp=""; } else temp+=s[i]; } printf("case #%d:\n%d\n",k++,ans); } return 0; }
这道是要是全手撸的话的确会很麻烦……
include
include
include
include
char ban[6][5]={“the”,”a”,”an”,”of”,”for”,”and”};
int makeit(char *a){
int sum=0;
int length=strlen(a);
char temp[100][100];
int cnt=0,ct=0;
bool flag=false;
for(int i=0; i<=length; i++){
if(isspace(a[i])||a[i]==’\0’){
temp[ct][cnt]=’\0’;
for(int i=0; i<6; i++){
if(strcmp(temp[ct],ban[i])==0)
flag=true;
}
ct++;
cnt=0;
if(!flag)
sum++;
flag=false;
}
else if(!isspace(a[i])){
temp[ct][cnt++]=a[i];
}
}
return sum;
}
int main(){
int t,ans;
char vis[200];
scanf(“%d”,&t);
getchar();
for(int k=0; k<t; k++){
gets(vis);
int length=strlen(vis);
for(int i=0; i=’A’&&vis[i]<=’Z’)vis[i]+=32;
ans=makeit(vis);
printf(“case #%d:\n%d\n”,k,ans);
}
}
几个地方注意下(含AC程序)
include
using namespace std;
const string Ignore[6]={“the”,”a”,”an”,”of”,”for”,”and”};
int T;
string line;
string Tolower(const string& strs)
{
string ret=”“;
int ll=strs.length();
for(int i=0;i<ll;i++) ret+=tolower(strs[i]);
return ret;
}
int ignore(const string& strs)
{
for(int i=0;i<6;i++)
if(Tolower(Ignore[i])==Tolower(strs)) return 1;
return 0;
}
int main()
{
cin>>T;
getline(cin,line);
for(int step=0;step>tmp)
if(!ignore(tmp)) ans++;
printf(“case #%d:\n%d\n”,step,ans);
}
return 0;
}
1.全部转为小写比较
2.stringstream+getline读取(如果用C++的话)
大写字母也要考虑
怎么一下子发了三条= = [em:14][em:14][em:14]
真的是郁闷,每次都被坑,说好的字符串长度小于等于100呢……
几个地方注意下(含AC程序)
3.常量数组的使用
大写字母也要考虑
strlwr
大写字母也要考虑
[em:02][em:02]
大写字母也要考虑
是的全要考虑= =差点被整死。。
大写字母也要考虑
还是 全部字母大写 就好了。。。
行末 经验证 没多余空格。。。
[em:13][em:13]
大写字母也要考虑
怎么一下子发了三条= = [em:14][em:14][em:14]
大写字母也要考虑
贴下代码
include
include
include
include
include
include
define M 2000
define N 150
using namespace std;
char word[20][10]= {“the”,”a”,”an”,”of”,”for”,”and”,”THE”,”The”,”A”,”An”,”AN”,”OF”,”Of”,”For”,”FOR”,”And”,”AND”};
bool cmp(char *s)
{
bool sign=true;
for(int i=0; i<17; i++)
if(strcmp(word[i],s)==0)
sign=false;
return sign;
}
void solve()
{
char s[M];
gets(s);
char tmp[N];
int cnt=0,start=0,len=strlen(s);
while(startstart&&s[len-1]==’ ‘)
–len;
for(int i=start; i<len; i++)
{
int k=0;
for(int j=i; s[j]!=’ ‘&&s[j]; j++,i++)
tmp[k++]=s[j];
tmp[k]=0;
if(cmp(tmp))cnt++;
}
cout<<cnt<<endl;
}
int main()
{
int i,t;
scanf(“%d”,&t);
getchar();
for (i=0; i<t; i++)
{
printf(“case #%d:\n”,i);
solve();
}
return 0;
}
大写字母也要考虑
贴下代码
include
include
include
include
include
include
define M 2000
define N 150
using namespace std;
char word[20][10]= {“the”,”a”,”an”,”of”,”for”,”and”,”THE”,”The”,”A”,”An”,”AN”,”OF”,”Of”,”For”,”FOR”,”And”,”AND”};
bool cmp(char *s)
{
bool sign=true;
for(int i=0; i<17; i++)
if(strcmp(word[i],s)==0)
sign=false;
return sign;
}
void solve()
{
char s[M];
gets(s);
char tmp[N];
int cnt=0,start=0,len=strlen(s);
while(startstart&&s[len-1]==’ ‘)
–len;
for(int i=start; i<len; i++)
{
int k=0;
for(int j=i; s[j]!=’ ‘&&s[j]; j++,i++)
tmp[k++]=s[j];
tmp[k]=0;
if(cmp(tmp))cnt++;
}
cout<<cnt<<endl;
}
int main()
{
int i,t;
scanf(“%d”,&t);
getchar();
for (i=0; i<t; i++)
{
printf(“case #%d:\n”,i);
solve();
}
return 0;
}
大写字母也要考虑
贴下代码
include
include
include
include
include
include
define M 2000
define N 150
using namespace std;
char word[20][10]= {“the”,”a”,”an”,”of”,”for”,”and”,”THE”,”The”,”A”,”An”,”AN”,”OF”,”Of”,”For”,”FOR”,”And”,”AND”};
bool cmp(char *s)
{
bool sign=true;
for(int i=0; i<17; i++)
if(strcmp(word[i],s)==0)
sign=false;
return sign;
}
void solve()
{
char s[M];
gets(s);
char tmp[N];
int cnt=0,start=0,len=strlen(s);
while(startstart&&s[len-1]==’ ‘)
–len;
for(int i=start; i<len; i++)
{
int k=0;
for(int j=i; s[j]!=’ ‘&&s[j]; j++,i++)
tmp[k++]=s[j];
tmp[k]=0;
if(cmp(tmp))cnt++;
}
cout<<cnt<<endl;
}
int main()
{
int i,t;
scanf(“%d”,&t);
getchar();
for (i=0; i<t; i++)
{
printf(“case #%d:\n”,i);
solve();
}
return 0;
}
全部变小写
include
include
include
include
include
include
using namespace std;
int main()
{
int cs;
cin>>cs;
for(int i=0;i>c)
{ if(getchar()==’\n’){strcpy(s[k++],c);break;}
strcpy(s[k++],c);
}
这道题用python比较好处理,把每个分割处理的单词变成小写,然后检测是不是在stopwords里面,如果不在就加1。遍历全部的单词即可得到最终的单词。
stop_words = [‘the’,’a’,’an’,’of’,’for’,’and’]
for i in range(int(input())):
len_num = 0
one_s = input().split(‘ ‘)
for one_word in one_s:
if one_word.lower() not in stop_words:
len_num += 1
print(‘case #’+str(i)+’:’)
print(len_num)
include
using namespace std;
void tolowcase(char word){
int n=0;
while(word[n]!=’\0’){
word[n]=(word[n]<97)?word[n]+32:word[n];
n++;
}
}
int strlen(char str){
int n=0;
while(str[n]!=’\0’){
n++;
}
return n;
}
int equalstr(char s1,char s2){
if(strlen(s1)!=strlen(s2))return 0;
else{
for(int i=0;i<=strlen(s1)-1;i++){
if(s1[i]!=s2[i])return 0;
}
return 1;
}
}
int iskeyword(char word){
tolowcase(word);
char keys[6]{“and”,”a”,”an”,”the”,”of”,”for”};
for(int i=0;i<=5;i++){
if(equalstr(word,keys[i]))return 1;
}
return 0;
}
int getstr(char *str){
char c=getchar();
if(c!=’\n’){
str[0]=c;
int i=1;
while((c=getchar())!=’ ‘){
if(c==’\n’){
str[i]=’\0’;
return -1;
};
str[i]=c;
i++;
}
str[i]=’\0’;
return 0;
}else{
return -1;
}
}
int main(){
int N;
cin>>N;
getchar();
for(int i=0;i<=N-1;i++){
int n=0;
char word[20];
while(1){
int x=getstr(word);
if(!iskeyword(word)){
n++;
}
if(x==-1)break;
}
cout<<”case #”<<i<<”:”<<endl<<n<<endl;
}
}