poj1002

题目

电话号码映射到数字,然后排序


代码

#include <iostream>
#include <vector>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
char cmap[] = "22233344455566670778889990";

vector<int> ivector;
void myqsort(int l, int u)
{
    if (l >= u)
        return;

    int i = l;
    int j = u + 1;
    int t = ivector[l];
    while(1){
        do {
            i++;
        } while ((i <= u) && (ivector[i] < t) );
        do {
            j--;
        } while(ivector[j] > t); 
        if (i > j)
            break;
        int tmp = ivector[i];
        ivector[i] = ivector[j];
        ivector[j] = tmp;
    }   

    ivector[l] = ivector[j];

    ivector[j] = t;

    myqsort(l, j - 1);
    myqsort(j + 1, u);
}

int main()
{
    int m;
    cin >> m;
    //string s;
    char s[50];
    for(int j = 0;j < m; ++j){
        fscanf(stdin, "%s", s);
        //cin >> s;
        int num = 0;
        for (int i = 0; s[i]!='\0'; ++i) {
            char c = s[i];
            if (c >= '0' && c <='9'){
                num *= 10;
                num += c - '0';
            } else if ( (c >= 'A') && (c < 'Z') && (c != 'Q')){
                num *= 10;
                num += cmap[c - 'A'] - '0';
            }
        }
        ivector.push_back(num);
    }

    vector<int>::iterator it;
    myqsort(0, ivector.size() - 1);

    int count = 1;
    bool hasDuplicate = false;


    vector<int>::iterator end = ivector.end();
    for(it = ivector.begin() + 1; it < end; it++){
        count = 1;
        while (it < end && *it == *(it - 1)) {
            count++;
            it++;
        }
        if (count > 1) {
            printf("%03d-%04d %d\n", *(it - 1)/10000, *(it - 1)%10000, count);
            hasDuplicate = true;
        }
    }

    if (hasDuplicate == false)
        printf("No duplicates. \n");

    return 0;
}


测试用例

12
4873279
ITS-EASY
888-4567
3-10-10-10
888-GLOP
TUT-GLOP
967-11-11
310-GINO
F101010
888-1200
---4-8-7-3-2-7-9-
487--3279


2  
---3333--3-3-3--
3333333

2  
---3333--3-3-3--
1333333

4
0000000
0010001
0000000
0010001


12
4873279
ITS-QASY
888-4567
3-10-10-10
888-GLOP
TUT-GLOP
967-11-11
310-GINO
F101010
888-1200
---4-8-7-3-2-7-9-
487--3279

5
1234567
1234567
2345678
2345678
3456789



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值