C--去掉+86
时间: 1ms 内存:128M
描述:
小明突然发现自己手机上的部分手机号前面多了+86,如+8618253556785。为了使用方便,小明想将这些多余的前缀去掉,请你帮小明编写程序去掉这些多余的前缀。
输入:
长度为14或11的字符串,只包含‘+’、0~9字符,
有多个字符串,每个一行。
输出:
去掉+86格式统一的手机号,每个手机号占一行。
示例输入:
+8618253591234
18253593244
+8618345456743
示例输出:
18253591234
18253593244
18345456743
提示:
参考答案(内存最优[920]):
#include "stdio.h"
#include "string.h"
int main()
{
int i;
char str[101];
int flag=0;
while(~scanf("%s",str))
{
if(str[0]=='+')
{
for(i=3;i<14;i++)
printf("%c",str[i]);
putchar('\n');
}
else
printf("%s\n",str);
}
return 0;
}
参考答案(时间最优[0]):
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char phone[20];
while(cin>>phone)
{
int len = strlen(phone);
if(len == 11)
cout<<phone<<endl;
else
{
for(int i = 3; i<len; i++)
cout<<phone[i];
cout<<endl;
}
}
return 0;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。