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

wrong answer,不会改了,望指点

Posted by frkstyc at 2004-10-23 12:04:44 on Problem 1002
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <cstdio>

using namespace std;

int map[] = {2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 0, 7, 7, 8, 8, 8, 9, 9, 9, 0};

typedef struct
{
    int num, count;
} NUM;

typedef vector<NUM> NUMBERS;

bool operator<(const NUM &lhs, const NUM &rhs)
{
    return lhs.num < rhs.num;
}

bool operator==(const NUM &lhs, const NUM &rhs)
{
    return lhs.num == rhs.num;
}

int transform(char *line)
{
    int num = 0;
    int i;
    int len = strlen(line);
    for(i = 0; i < len; ++i)
    {
        if(line[i] >= '0' && line[i] <= '9')
        {
            num = num * 10 + line[i] - '0';
        }
        if(line[i] >= 'A' && line[i] <= 'Z')
        {
            num = num * 10 + map[line[i] - 'A'];
        }
    }
    return num;
}

void output(NUM& a)
{
    char str[8];
    sprintf(str, "%7d", a.num);
    int i;
    for(i = 0; i < 8; i++)
    {
        if(i == 3)
        {
            cout << '-';
        }
        if(str[i] == ' ')
        {
            cout << '0';
        }
        else
        {
            cout << str[i];
        }
    }
    cout << ' ' << a.count << endl;
}

int main(int argc, char *argv[])
{
    NUMBERS phonebook;
    long n;
    char line[80];
    cin >> n;
    //cin.getline(line, sizeof(line), '\n');
    while(cin.get()!='\n')
    {
        ;
    }
    while(n > 0)
    {
        --n;
        cin.getline(line, sizeof(line), '\n');
        int num = transform(line);
        NUM temp = {num, 1};
        NUMBERS::iterator ite = find(phonebook.begin(), phonebook.end(), temp);
        if(ite == phonebook.end())
        {
            phonebook.push_back(temp);
        }
        else
        {
            ++ite->count;
        }
    }
    sort(phonebook.begin(), phonebook.end());
    NUMBERS::iterator ite;
    for(ite = phonebook.begin(); ite < phonebook.end(); ++ite)
    {
        if(ite->count > 1)
        {
            output(*ite);
        }
    }
    for(ite = phonebook.begin(); ite->count <= 1 && ite < phonebook.end(); ++ite)
    {
        ;
    }
    if(ite == phonebook.end())
    {
        cout << "No duplicates." << endl;
    }
    return EXIT_SUCCESS;
}

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