uva 755 487--3279(字母与数字处理)

题意是给一串字母与数字混合的字符串,每个字母有对应的数字,输入一串,要求判断重复出现的数字。

以前在usaco上做过类似的题目,那个时候说的是手机九宫格的摁键。


代码:

#include 
#include 
#include 
#include 
using namespace std;

int words[26] = {2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9};
int num[100005];

int change(char str[])
{
    int len = strlen(str);
    int tmp = 0;
    for (int i = 0; i < len; i++)
    {
        if (isdigit(str[i]))
            tmp = tmp * 10 + str[i] - '0';
        else if (isalpha(str[i]))
            tmp = tmp * 10 + words[str[i] - 'A'];
        else
            continue;
    }
    return tmp;
}

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    #endif // LOCAL
    int ncase;
    int cnt = 0;
    scanf("%d", &ncase);
    while (ncase--)
    {
        int n;
        char str[200] = {0};
        scanf("%d", &n);
        getchar();
        for (int i = 0; i < n; i++)
        {
            gets(str);
            num[i] = change(str);
        }
        sort(num, num + n);

        int count = 1;
        bool flag = false;
        for (int i = 1; i < n; i++)
        {
            if (num[i] == num[i - 1])
                count++;
            if (num[i] != num[i - 1] || i == n -1)
            {
                if (count != 1)
                {
                    flag = true;
                    printf("%03d-%04d %d\n", num[i - 1] / 10000, num[i - 1] % 10000, count);
                }
                count = 1;
            }
        }
        if (!flag)
            printf("No duplicates.\n");
        if (ncase > 0)
            printf("\n");
    }
    return 0;
}


你可能感兴趣的:(字符串)