码迷,mamicode.com
首页 > 其他好文 > 详细

【蓝桥杯竞赛】例题: 487-3279

时间:2015-04-29 00:11:44      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

技术分享

 

分析:

str数组接受不规范的电话字符串,number数组储存标准化的电话号码,qsort(stdlib库函数)将number数组排序,通过检测排序后的number数组得知同一个号码的重复次数,

compare函数用于qsort函数的实现(类似于实现接口)

strandradStr函数用于是电话号码的标准化

源码:

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

char str[20];
char number[10000][9];

//char* elem1 char* elem2
int compare(const void *elem1, const void *elem2)
{
    return(strcmp((char*)elem1, (char*)elem2));
}

void strandradStr(int i)
{
    bool flag = false;
    int j;
    for(j = 0; str[j] != ‘\0‘; j++)
    {
        if(str[j] == ‘-‘)
            flag = true;
        if(str[j]==‘A‘ || str[j]==‘B‘ || str[j]==‘C‘)
            str[j] =  ‘2‘;
        else if(str[j]==‘D‘ || str[j]==‘E‘ || str[j]==‘F‘)
            str[j] =  ‘3‘;
        else if(str[j]==‘G‘ || str[j]==‘H‘ || str[j]==‘I‘)
            str[j] =  ‘4‘;
        else if(str[j]==‘J‘ || str[j]==‘K‘ || str[j]==‘L‘)
            str[j] =  ‘5‘;
        else if(str[j]==‘M‘ || str[j]==‘N‘ || str[j]==‘O‘)
            str[j] =  ‘6‘;
        else if(str[j]==‘P‘ || str[j]==‘R‘ || str[j]==‘S‘)
            str[j] =  ‘7‘;
        else if(str[j]==‘T‘ || str[j]==‘U‘ || str[j]==‘V‘)
            str[j] =  ‘8‘;
        else if(str[j]==‘W‘ || str[j]==‘X‘ || str[j]==‘Y‘)
            str[j] =  ‘9‘;
    }
    int k = 0;
    for(j = 0; str[j] !=‘\0‘ && flag; j++)
    {
        if(str[j] != ‘-‘)
        {
            str[k] = str[j];
            k++;
        }
    }
    if(flag)
        str[k] = ‘\0‘;
    int m;
    for(m = 0; str[m] != ‘\0‘; m++)
        //printf("%s\n", str);
        number[i][m] = str[m];
    str[m] = ‘\0‘;
}

int main()
{
    int n;
    scanf("%d" , &n);
    for(int i = 0; i < n; i++)
    {
        scanf("%s", str);
        //将输入字符串格式化为数字
        //保存在number[i][9]
        strandradStr(i);
    }

    //number数组排序
    qsort(number, n, 9, compare);

    bool noduplicate = true;
    int i = 0;
    int j = 0;
    while(i<n)
    {
        j = i;
        i++;
        while(i<n && strcmp(number[j],number[i]) == 0)i++;
        if(i - j > 1)
        {
            printf("%s %d\n", number[j], i-j);
            noduplicate = false;
        }
    }
    if(noduplicate)
    {
        printf("No duplicates.\n");
    }
    return 0;
}

【蓝桥杯竞赛】例题: 487-3279

标签:

原文地址:http://www.cnblogs.com/superkrissV/p/4464282.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!