【id:512】【7分】H. 手机短号(函数)
时间限制1s
内存限制128MB
题目描述
手机号是一个11位长的数字串。同时,大家可以申请加入校园网,如果加入成功,你将另外拥有一个短号。假设所有的短号都是是 6+手机号的后5位,比如号码为13512345678的手机,对应的短号就是645678。
自定义函数,参数为手机号(字符串),该手机号为合法手机号,则计算该手机号的短号,并用数组传回。返回1;否则返回0。主函数定义两个串,手机号串和短号串,调用该函数,输出error或得到的短号。
合法手机号判断:11位数字,前三位为:
中国电信:133,153, 180,181,189
中国联通:130,131,132,155,156
中国移动:134,135,136,137,138,182,183,184
(为简化,上述三位数字省去了一些。)
输入
第一行:测试次数
每组测试数据一行,手机号
输出
对每组测试数据,调用函数,根据返回结果,若为1,输出对应短号;若为0,输出error。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int dh(char s1[],char s2[]){
int l=strlen(s1);
int tag=0;
if(l==11&&s1[0]=='1'){
if(s1[1]=='3'){
if(s1[2]!='9')
tag=1;
}
if(s1[1]=='5'){
if(s1[2]=='3'||s1[2]=='5'||s1[2]=='6')
tag=1;
}
if(s1[1]=='8'){
if(s1[2]!='5'||s1[2]!='6'||s1[2]!='7'||s1[2]!='8')
tag=1;
}
}
if(tag==1){
s2[0]='6';
strcpy(s2+1,s1+6);
return 1;
}
else
return 0;
}
int main()
{
int t;
char s1[20],s2[10];
scanf("%d",&t);
getchar();
while(t--){
scanf("%s",s1);
int a=dh(s1,s2);
if(a==1){
printf("%s\n",s2);
}
else{
printf("error\n");
}
getchar();
}
return 0;
}