问题描述:给出一些单词序列,按照以下规则求出单词序列的缩写
⑴小于等于2位字母的单词不要
⑵for,and,the不要,无论大小写
⑶除12点外的单词取其首字母大写按顺序连起来
输入格式:第一行为一个整数n,表示1要缩写的单词序列的个数(n<=100)
接下来n行,每行为一个长度小于100的单词序列,每个单词都由大写或小写字母组成,每个单词直接有一个空格。
输出格式:输出为n行,每行为对应的单词缩写
示例
用户输入:3
association for computer machinery
the lord of rings
rolle
程序输出
ACM
LR
R
用c++解决,新手,求大神,用比较基础的方法,谢谢了
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答
lee_happycpp 2017-11-10 14:25关注代码:
#include<iostream> #include<cstdio> using namespace std; char c[1001],ans[101],f[5]={'\0','f','o','r',' '},a[5]={'\0','a','n','d',' '},t[5]={'\0','t','h','e',' '}; int n; int main(){ int i,j,k,l,m; scanf("%d",&n); getchar(); for(i=1;i<=n;i++){ for(j=0;j<=1000;j++) c[j]='\0'; for(j=0;j<=100;j++) ans[j]='\0'; c[0]=' '; j=0; l=0; while(1){ j++; scanf("%c",&c[j]); if(c[j]=='\n'){ c[j]='\0'; j--; break; } } m=0; while(m<=j){ m++; if(c[m]==' ') continue; if(c[m-1]==' '&&c[m+1]==' ') continue; if(c[m-1]==' '&&c[m+1]!=' '&&c[m+2]==' '){ m++; continue; } char now[5]; for(k=1;k<=4;k++) now[k]=c[m+k-1]; bool f1=1,f2=1,f3=1; for(k=1;k<=4;k++){ if(now[k]!=f[k]) f1=0; if(now[k]!=a[k]) f2=0; if(now[k]!=t[k]) f3=0; } if(f1||f2||f3){ m+=2; continue; } l++; if('a'<=c[m]&&c[m]<='z') ans[l]=c[m]-32; else ans[l]=c[m]; while(c[m]!=' ') m++; } printf("%s\n",ans+1); } }如有答案错误,请谅解(第一次回答嘛)
一个小学生写的,所以有点难看。解决 无用评论 打赏 举报