*翻译电话号码
来源:互联网 发布:中电数据服务 编辑:程序博客网 时间:2024/04/28 16:48
题目描述:
将电话号码 one two...nine zero翻译成1 2...9 0
中间会有double
例如输入:onetwothree
输出:123
输入:onetwodoubletwo
输出:1222
输入:1two2 输出:ERROR
输入:doubledoubletwo 输出:ERROR
有空格,非法字符,两个double相连,double位于最后一个单词,都错误,输出:ERROR
代码:
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { char a[100]="\0",out[100]="\0"; char temp[10]="\0"; gets(a); //对有非法字符打印错误 for(int i=0;a[i]!='\0';i++) { if(a[i]<'a'||a[i]>'z') { printf("ERROR\n"); return 0; } } char b[11][11]={"zero","one","two","three","four","five","six","seven","eight","nine","double"}; //将对应的电话号码变成字符存到out数组中,输入错误时打印错误 int i,k=0; for(i=0;a[i]!='\0';) { strncpy(temp,a+i,6); if(strncmp(temp,b[0],4)==0) { out[k++]='0'; i=i+4; continue; } else if(strncmp(temp,b[1],3)==0) { out[k++]='1'; i=i+3; continue; } else if(strncmp(temp,b[2],3)==0) { out[k++]='2'; i=i+3; continue; } else if(strncmp(temp,b[3],5)==0) { out[k++]='3'; i=i+5; continue; } else if(strncmp(temp,b[4],4)==0) { out[k++]='4'; i=i+4; continue; } else if(strncmp(temp,b[5],4)==0) { out[k++]='5'; i=i+4; continue; } else if(strncmp(temp,b[6],3)==0) { out[k++]='6'; i=i+3; continue; } else if(strncmp(temp,b[7],5)==0) { out[k++]='7'; i=i+5; continue; } else if(strncmp(temp,b[8],5)==0) { out[k++]='8'; i=i+5; continue; } else if(strncmp(temp,b[9],4)==0) { out[k++]='9'; i=i+4; continue; } else if(strncmp(temp,b[10],6)==0) { out[k++]='d'; i=i+6; continue; } else { printf("ERROR\n"); return 0; } } //排除连续d和最后d的情况 if(out[k-1]=='d') { printf("ERROR\n"); return 0; } for(i=0;i<k-1;i++) { if(out[i]=='d'&&out[i]==out[i+1]) { printf("ERROR\n"); return 0; } } //将d转换为d后面的数字符号 for(i=0;i<k-1;i++) { if(out[i]=='d') out[i]=out[i+1]; } printf("%s\n",out); return 0; }或者
#include <iostream> #include <string> using namespace std; void process(string str) { //各字符串的下标即为其对应的阿拉伯数字 char *ch[]={"zero","one","two","three","four","five","six","seven","eight","nine","double"}; char *out[100];//存储结果集 int f=0; int i,j,k; string substr; int len=str.length(); int flag=0;//判断double出现 int flag2=0;//判断内循环有无break for(i=0;i<len;i++) { if(flag2) break; else if(str[i]<97 || str[i]>122)//非字母字符 { out[f++]="error"; break; } else{ //可以看出拼音最短为3个字符,最长为6个字符 for(j=i+2;j<len&&(j-i)<6;j++) { substr=str.substr(i,j-i+1); if(!substr.compare(ch[10]))//出现double { if(i==len-6)//最后一个double { out[f++]="error"; flag2=1; break; }else if(flag){//连续两个double out[f++]="error"; flag2=1; break; }else flag=1; } else{ for(k=0;k<10;k++) { if(!substr.compare(ch[k])) { if(flag){ out[f++]=ch[k]; out[f++]=ch[k]; flag=0; break; } else{ out[f++]=ch[k]; break; } } } } } } } int flag3=0;//看有无error,默认没有error for(int r=0;r<f;r++){ if(out[r]=="error")//有error flag3=1; } if(!flag3) for(r=0;r<f;r++) for(k=0;k<10;k++) { if(out[r]==ch[k]) printf("%d",k); } else printf("error"); printf("\n"); } void main() { string str; while(getline(cin,str)) process(str); }
0 0
- 翻译电话号码
- 翻译电话号码
- *翻译电话号码
- 华为-翻译电话号码
- 电话号码的翻译
- 华为机试 翻译电话号码
- 华为上机—电话号码翻译
- 华为机试之 翻译电话号码
- 华为机试—翻译电话号码
- 华为机试电话号码翻译C++实现
- 华为机试——翻译电话号码
- 电话号码
- 电话号码
- 电话号码
- 电话号码
- 电话号码
- 电话号码
- 电话号码
- Activity的启动流程 Android5.1.1
- Android中RxJava+Retrofit2.0+MVP模式的整合
- GIT和SVN之间的五个基本区别
- 细说 ASP.NET Cache 及其高级用法
- 二叉排序树的建立与遍历
- *翻译电话号码
- 这是我的第一篇博客
- socket阻塞与非阻塞,同步与异步、I/O模型
- 记一次数据库在线current redo损坏故障,2662,ora-600
- 浏览器Quirksmode与CSS1compat
- JMeter特殊情况二:针对某些请求数据每次请求都是变化的情况
- Qt5对付中文真好用
- 二分查找
- reason: '[<__NSDictionary0 0x7fda88f00c90> setValue:forUndefinedKey:]: this class is not key value c