UVa 755 - 487--3279 Or Poj 1002

UVa上没法提交这题,就去了Poj, poj上提交要略微修改。

方法一:电话号码转换为数字后排序。

#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
const int MAXN = 100004;
const int BUFF_SIZE = 30;
int A[MAXN];
int vis[MAXN];
int n;
char buff[BUFF_SIZE];

int main() {
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    int T;
    //scanf("%d", &T);
    //while(T--) {
        scanf("%d\n", &n);
        int s, k;
        char c;
        for(int i=0; i<n; i++) {
            s = 0;
            while((c = getchar()) != '\n') {
                if(c == '-') continue;
                if(isalpha(c)) {
                    if(c <= 'R') k = (c-'A')/3 + 2;
                    else k = (c-'A'- 1)/3 + 2;
                } else {
                    k = c - '0';
                }
                s = s*10 + k;
            }
            A[i] = s;
        }
        sort(A, A+n);
        A[n] = A[n-1] + 1;
        vis[0] = 1;
        int found = 0;
        for(int i=1; i<=n; i++) {
            vis[i] = 1;
            if(A[i] == A[i-1]) {
                vis[i] += vis[i-1];
            } else if(vis[i-1] > 1){
                printf("%03d-%04d %d\n", A[i-1]/10000, A[i-1]%10000, vis[i-1]);
                found = 1;
            }
        }
        if(!found) printf("No duplicates.\n");
        //if(T) printf("\n");
    //}
    return 0;
}

方法2: poj上比方法1快, 思想就是转换为数字后,计数排序

#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
const int MAXN = 100004;
const int MAXM = 10000000;
int A[MAXN];
int cnt = 0;
int vis[MAXM];
int n;

int main() {
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif

    scanf("%d\n", &n);
    int s, k;
    char c;
    for(int i=0; i<n; i++) {
        s = 0;
        while((c = getchar()) != '\n') {
            if(c == '-') continue;
            if(isalpha(c)) {
                if(c <= 'R') k = (c-'A')/3 + 2;
                else k = (c-'A'- 1)/3 + 2;
            } else {
                k = c - '0';
            }
            s = s*10 + k;
        }
        ++vis[s];
        if(vis[s] == 2) {
            A[cnt++] = s;
        }
    }
    if(cnt) {
        sort(A, A+cnt);
        for(int i=0; i<cnt; i++)
            printf("%03d-%04d %d\n", A[i]/10000, A[i]%10000, vis[A[i]]);
    } else {
        printf("No duplicates.\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值