美团笔试 美团测开笔试 0315

笔试时间:2025年03月15日

历史笔试传送门:

2023春招秋招笔试合集

2024春招秋招笔试合集

第一题

题目

普通快递:以"SF"开头,后跟10位数字特快专递:以"EX"开头,后跟2个大写字母和8位数字国际快递:以"IN"开头,后跟3个大写字母和6位数字,且最后两位数字之和必须是偶数电商快递:纯数字12位,且第一位不为0不符合上述规则的都是无效单号

输入描述

第一行输入n,表示快递的数量

接下来n行,每一行一个字符串表示快递单号

输出描述

对于每一个快递单号,输出其类型:

"Normal"(普通快递)

"Express"(特快专递)

"International"(国际快递)

"E-commerce"(电商快递)

"Invalid"(无效单号)

样例输入

5

SF1234567890

EXAB12345678

INABC123446

123456789012

SF12345

样例输出

Normal

Express

International

E-commerce

Invalid

参考题解

模拟。

C++:[此代码未进行大量数据的测试,仅供参考]

#include <bits/stdc++.h>
using namespace std;
void solve() {
    string s;
    cin >> s;
    auto isUpperCase = [&](char c) -> bool {
        return c >= 'A' && c <= 'Z';
    };
    auto isLowerCase = [&](char c) -> bool {
        return c >= 'a' && c <= 'z';
    };
    auto isDigit = [&](char c) -> bool {
        return c >= '0' && c <= '9';
    };
    if (s.size() < 2) {
        cout << "Invalid" << "\n";
    } else {
        string prefix = s.substr(0, 2);
        if (prefix == "SF") {
            string suffix = s.substr(2);
            if (suffix.size() != 10) {
                cout << "Invalid" << "\n";
            } else {
                bool ok = true;
                for (int i = 0; i < 10; i++) {
                    if (!isDigit(suffix[i])) ok = false;
                }
                if (ok) {
                    cout << "Normal" << "\n";
                } else {
                    cout << "Invalid" << "\n";
                }
            }
        } else if (prefix == "EX") {
            string suffix = s.substr(2);
            if (suffix.size() != 10) {
                cout << "Invalid" << "\n";
            } else {
                bool ok = true;
                for (int i = 0; i < 2; i++) {
                    if (!isUpperCase(suffix[i])) {
                        ok = false;
                    }
                }
                for (int i = 2; i < 10; i++) {
                    if (!isDigit(suffix[i])) {
                        ok = false;
                    }
                }
                if (ok) {
                    cout << "Express" << "\n";
                } else {
                    cout << "Invalid" << "\n";
                }
            }
        } else if (prefix == "IN") {
            string suffix = s.substr(2);
            if (suffix.size() != 9) {
                cout << "Invalid" << "\n";
            } else {
                bool ok = true;
                for (int i = 0; i < 3; i++) {
                    if (!isUpperCase(suffix[i])) {
                        ok = false;
                    }
                }
                for (int i = 3; i < 9; i++) {
                    if (!isDigit(suffix[i])) {
                        ok = false;
                    }
                }
                if ((suffix[8] - '0' + suffix[7] - '0') % 2 != 0) {
                    ok = false;
                }
                if (ok) {
                    cout << "International" << "\n";
                } else {
                    cout << "Invalid" << "\n";
                }
            }
        } else if (isDigit(prefix[0]) && isDigit(prefix[1]) && prefix[0] != '0') {
            string suffix = s.substr(2);
            if (suffix.size() != 10) {
                cout << "Invalid" << "\n";
            } else {
                bool ok = true;
                for (int i = 0; i < 10; i++) {
                    if (!isDigit(suffix[i])) {
                        ok = false;
                    }
                }
                if (ok) {
                    cout << "E-commerce" << "\n";
                } else {
                    cout << "Invalid" << "\n";
                }
            }
        } else {
            cout << "Invalid" << "\n";
        }
    }
}
int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}

Java:[此代码未进行大量数据的测试,仅供参考]

import java.util.Scanner;

public class Main {
    public static boolean isUpperCase(char c) {
        return c >= 'A' && c <= 'Z';
    }

    public static boolean isLowerCase(char c) {
        return c >= 'a' && c <= 'z';
    }

    public static boolean isDigit(char c) {
        return c >= '0' && c <= '9';
    }

    public static void solve(Scanner sc) {
        String s = sc.next();
        if (s.length() < 2) {
            System.out.println("Invalid");
            return;
        }

        String prefix = s.substring(0, 2);
        String suffix = s.substring(2);

        if (prefix.equals("SF")) {
            if (suffix.length() != 10) {
                System.out.println("Invalid");
            } else {
                boolean ok = suffix.chars().allMatch(Character::isDigit);
                System.out.println(ok ? "Normal" : "Invalid");
            }
        } else if (prefix.equals("EX")) {
            if (suffix.length() != 10) {
                System.out.println("Invalid");
            } else {
           

剩余60%内容,订阅专栏后可继续查看/也可单篇购买

2025 春招笔试合集 文章被收录于专栏

2025打怪升级记录,大厂笔试合集 C++, Java, Python等多种语言做法集合指南

全部评论

相关推荐

评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客企业服务