Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

这道题有什么好的办法没有?

Posted by acpigs at 2009-06-20 12:33:17 on Problem 1002
我先将号码一个个读进来,由于号码是7位,于是我在堆中申请了1000*10000的数组,初始化为0,每读入一个号码in,就ar[in]++,最后从1到999-9999读ar数组,若起位置值大于一就打印```
用时570ms。
问有更好的办法没有?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int ar[10000000];
int str[128]={0};
int num;
int in;
void input(const int y)
{
    int i=0;
    char c;
    in = 0;
    while (i<7)
    {
        c = getchar();
        switch (c)
        {
        case '-':
            break;
        case '\n':
            break;
        default:
            in *= 10;
            in += str[(int)c];
            i++;
            break;
        }
    }
}
char tmp[10][5] = {"0","1","ABC2","DEF3","GHI4","JKL5","MNO6","PRS7","TUV8","WXY9"};
void ini()
{
    int i,j;
    for (i=0;i<10;i++)
        for (j=0;tmp[i][j];j++)
            str[(int)tmp[i][j]] = i;
}
void solve_one()
{
    memset(ar,0,sizeof(ar));
    int i,k;
    for (i=0;i<num;i++)
    {
        input(i);
        ar[in]++;
        printf("ans:%d",ar[in]);
    }
    for (i=0,k=0;i<10000000;i++)
    {
        if (ar[i]>1)
        {
            k++;
            printf("%03d-%04d %d\n",i/10000,i%10000,ar[i]);
        }
    }
    if (k == 0)puts("No duplicates.");
}
int main()
{
    ini();
    while (scanf("%d",&num)==1)
        solve_one();
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator