华科15_1

来源:互联网 发布:静默卸载软件 bat 编辑:程序博客网 时间:2024/06/16 00:29

问题:

输入一个字符串,并且进行验证。

条件:

长度必须为11

第一位必须为1

第二位必须为3,5,6,8中的一个

其余任意一位必须为0-9中的一个

分析:

根据规则进行判断即可。

代码:

#include <iostream>  #include <stdio.h>   #include <string.h>  #include <math.h>  #include <vector>  #include <queue>  #include <stack>  #include <map>  #include <string>  #include <algorithm>  #include <iomanip>#define MAX 1000using namespace std;/* run this program using the console pauser or add your own getch, system("pause") or input loop */bool isPhoneNum(char * str){//1.长度必须为11位int length=strlen(str) ;if(length != 11){return false;}else{//2.第一位必须为1 第二位为3,5,6,8 其余位必须为0-9中的一个数if(str[0] == '1') {if(str[1] == '3' || str[1] == '5'||str[1] == '6'||str[1] == '8'){//3.其余位必须为0-9中的一个数for(int i=2;i<length;i++) {if(str[i] >'9' || str[i] < '0'){return false;}}}else{return false;}}else{return false;}}return true;}int main(int argc, char** argv) {/*freopen("file/input.txt","r",stdin);freopen("file/output.txt","w",stdout);*/char str[MAX];while(gets(str)){if(isPhoneNum(str)){printf("是一个正确的电话号码!\n");}else{printf("不是一个正确的电话号码!\n");}}return 0;}/*测试用例: 15735186582 157351865822 25735186582 13735186582 16735186582 18735186582 12735186582 17735186582 19735186582 1573518658a */


原创粉丝点击