解题思路

这是一个字符串处理和验证问题。具体要求:

  1. 验证手机号是否合法,需要满足:
    • 长度为11位数字
    • 前三位为指定的运营商号段
  2. 根据号段判断运营商:
    • 电信:133,153,180,181,189
    • 联通:130,131,155,185,186
    • 移动:135,136,150,182,188
  3. 输出对应运营商名称,非法号码输出-1

代码

#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;

class PhoneValidator {
private:
    // 运营商号段集合
    unordered_set<string> telecom {"133", "153", "180", "181", "189"};
    unordered_set<string> unicom {"130", "131", "155", "185", "186"};
    unordered_set<string> mobile {"135", "136", "150", "182", "188"};
    
public:
    string validate(string phone) {
        // 检查长度
        if (phone.length() != 11) {
            return "-1";
        }
        
        // 获取前三位
        string prefix = phone.substr(0, 3);
        
        // 判断运营商
        if (telecom.count(prefix)) {
            return "China Telecom";
        }
        if (unicom.count(prefix)) {
            return "China Unicom";
        }
        if (mobile.count(prefix)) {
            return "China Mobile Communications";
        }
        
        return "-1";
    }
};

int main() {
    int t;
    cin >> t;
    PhoneValidator validator;
    
    while (t--) {
        string phone;
        cin >> phone;
        cout << validator.validate(phone) << endl;
    }
    return 0;
}
import java.util.*;

public class Main {
    // 运营商号段集合
    private static final Set<String> telecom = new HashSet<>(Arrays.asList(
        "133", "153", "180", "181", "189"));
    private static final Set<String> unicom = new HashSet<>(Arrays.asList(
        "130", "131", "155", "185", "186"));
    private static final Set<String> mobile = new HashSet<>(Arrays.asList(
        "135", "136", "150", "182", "188"));
    
    public static String validate(String phone) {
        // 检查长度
        if (phone.length() != 11) {
            return "-1";
        }
        
        // 获取前三位
        String prefix = phone.substring(0, 3);
        
        // 判断运营商
        if (telecom.contains(prefix)) {
            return "China Telecom";
        }
        if (unicom.contains(prefix)) {
            return "China Unicom";
        }
        if (mobile.contains(prefix)) {
            return "China Mobile Communications";
        }
        
        return "-1";
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        
        while (t-- > 0) {
            String phone = sc.next();
            System.out.println(validate(phone));
        }
    }
}
def validate_phone(phone: str) -> str:
    # 运营商号段集合
    telecom = {'133', '153', '180', '181', '189'}
    unicom = {'130', '131', '155', '185', '186'}
    mobile = {'135', '136', '150', '182', '188'}
    
    # 检查长度
    if len(phone) != 11:
        return "-1"
    
    # 获取前三位
    prefix = phone[:3]
    
    # 判断运营商
    if prefix in telecom:
        return "China Telecom"
    if prefix in unicom:
        return "China Unicom"
    if prefix in mobile:
        return "China Mobile Communications"
    
    return "-1"

def main():
    t = int(input())
    for _ in range(t):
        phone = input().strip()
        print(validate_phone(phone))

if __name__ == "__main__":
    main()

算法及复杂度

  • 算法:字符串处理 + 集合查找
  • 时间复杂度: - 为测试用例数量,每次验证为
  • 空间复杂度: - 只需要存储固定的号段集合