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
北京大学暑期课《ACM/ICPC竞赛训练》面向全球招生!

1002为什么总是报Wrong Answer啊 测了很多数据都没问题的啊

Posted by yuyishuoyu at 2018-05-15 17:48:16
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_PHONE_RECORD_COUNT = 1000

struct phone_record {
    char *phone;
    int count;
};

struct phone_record *phons[1000];
int phoneCount = 0;

char checkReflect(char c) {
    if (c >= '0' && c <= '9') return c;
    if (c == 'Q' || c == 'Z' || c == '-') return ' ';
    if (c < 'A' || c > 'Z') return ' ';
    if (c <= 'C') return '2';
    if (c <= 'F') return '3';
    if (c <= 'I') return '4';
    if (c <= 'L') return '5';
    if (c <= 'O') return '6';
    if (c <= 'S') return '7';
    if (c <= 'V') return '8';
    if (c <= 'Y') return '9';
    return ' ';
}

char *getPhone(const char *s) {
    char c;
    // 这里因为电话号码最多就7个数字+一个分隔符已经一个结尾表示共9个字符
    char *ret = (char *)malloc(9);
    char *t = ret;
    while (c = *s++) {
        c = checkReflect(c);
        if (c == ' ') continue;
        *t++ = c;
        if (t - ret == 3) {
            *t++ = '-';
        }
    }
    *t = '\0';
    return ret;
}

void addRecord(char *phone) {

    int i = 0;
    struct phone_record *t_pr;

    for (i = 0 ; i < phoneCount ; i ++) {
        struct phone_record *pr = phons[i];
        if (!strcmp(pr->phone, phone)) {// 为0表示相等
            pr->count ++;
            break;
        }
    }

    if (i == phoneCount) {
        t_pr = (struct phone_record *)malloc(sizeof(struct phone_record));
        t_pr->count = 1;
        t_pr->phone = (char *) malloc(9);
        strcpy(t_pr->phone, phone);
        phons[i] = t_pr;
        phoneCount ++;
    }

}

void sortRecords() {

    int i,j;
    for (i = 0 ; i < phoneCount ; i ++) {
        for (j = i + 1 ; j < phoneCount ; j ++) {
            struct phone_record *pr = phons[i];
            struct phone_record *pr2 = phons[j];
            if (strcmp(pr->phone, pr2->phone) > 0) {
                // 交换
                struct phone_record *p = phons[i];
                phons[i] = phons[j];
                phons[j] = p;
            }
        }
    }
}

void printRecords() {
    int i = 0;
    int flag = 0;
    for (i = 0 ; i < phoneCount ; i ++) {
        struct phone_record *pr = phons[i];
        if (pr->count <= 1) continue;
        printf("%s %d\n", pr->phone, pr->count);
        flag = 1;
    }
    if (flag == 0) {
        printf("No duplicates.\n");
    }
}

int main() {

    // 数量
    int i, size;
    char line[80];
    char *p;

    scanf("%d", &size);
    for (i = 0 ; i < size ; i ++) {
        scanf("%s", line);
        p = getPhone(line);
        addRecord(p);
    }

    sortRecords();
    printRecords();

}

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