首页 > 代码库 > 487-3279的解法实例

487-3279的解法实例

//解法1
#include<iostream>
#include<string>
using namespace std;
int convert(char a)
{
    int flag=-1;
    if(a==‘A‘||a==‘B‘||a==‘C‘||a==‘2‘)
    flag=2;
    if(a==‘D‘||a==‘E‘||a==‘F‘||a==‘3‘)
    flag=3;   
    if(a==‘G‘||a==‘H‘||a==‘I‘||a==‘4‘)
    flag=4;       
    if(a==‘J‘||a==‘K‘||a==‘L‘||a==‘5‘)
    flag=5;   
    if(a==‘M‘||a==‘N‘||a==‘O‘||a==‘6‘)
    flag=6;   
    if(a==‘P‘||a==‘R‘||a==‘S‘||a==‘7‘)
    flag=7;
    if(a==‘T‘||a==‘U‘||a==‘V‘||a==‘8‘)
    flag=8;
    if(a==‘W‘||a==‘X‘||a==‘Y‘||a==‘9‘)
    flag=9;   
    if(a==‘1‘)
    flag=1;
    if(a==‘0‘)
    flag=0;
    return flag;       
}
char convert1(int a)
{
    char f;
     
    if(a==0)
        f=‘0‘;
    if(a==1)
        f=‘1‘;
     
    if(a==2)
        f=‘2‘;
     
    if(a==3)
        f=‘3‘;
     
    if(a==4)
        f=‘4‘;
     
    if(a==5)
        f=‘5‘;
         
    if(a==6)
        f=‘6‘;
     
    if(a==7)
        f=‘7‘;
     
    if(a==8)
        f=‘8‘;
     
    if(a==9)
        f=‘9‘;   
    return f;       
}
int main()
{
    string s;
    int p,q;
    string temp;
    int temp1;
    string a[10000];
    for(p=0;p<10000;p++)
        a[p]="";
    int sum;
    cin>>sum;
    int count=0;
    for(int i=0;i<sum;i++)
    {
        cin>>s;
        int j;
        int count1=0;
        for(j=0;j<s.length();j++)
        {
            temp1=convert(s[j]);
            if(temp1!=-1)
            {
                if(count1==3)
                {
                a[count]=a[count]+‘-‘;   
                count1++;
                }
                a[count]=a[count]+convert1(temp1);
                count1++;
            }
        }
        count++;
    }
    int w[10000];
    string b[10000];
    int c[10000];
    for(int i=0;i<10000;i++)
    b[i]="";
    for(int i=0;i<count;i++)
    w[i]=1;
    int u=0;
for(int i=0;i<count;i++)
{
    if(w[i]==1)
    {
        int count2=1;
        for(int j=i+1;j<count;j++)
        {
            if(a[i]==a[j])
            {
            w[j]=0;
            count2++;
            }
        }
        if(count2>1)
        {
            b[u]=a[i];
            c[u]=count2;
            u++;           
        }
    }
}
    int d[100000];
    for(int z=0;z<u;z++)
    {
        d[z]=convert(b[z][0]);
    }
    int min;
    int y=0;
    for(int r=0;r<u;r++)
    {
    min=d[y];
    for(int z=0;z<u;z++)
    {
        if(d[z]<min)
        {
            min=d[z];
            y=z;
        }
    }
    cout<<b[y]<<" "<<c[y]<<endl;
    d[y]=100000;
    }
return 0;
}

//解法2

#include <iostream>
#include <map>
#include <string>
 
using namespace std;
 
int main()
{
    char szNumMap[] = "22233344455566670778889990";
    map<string,int> mResultMap;
    string strInputTemp;
    string strResult;
    int nCaseCount = 0;
    cin >> nCaseCount;
    while( nCaseCount > 0 )
    {
        cin >> strInputTemp;
        strResult.clear();
        for( string::size_type i = 0; i < strInputTemp.size(); ++i )
        {
            if ( strInputTemp[i] == ‘-‘ )   continue;
            if ( isdigit( strInputTemp[i] ) )
            {
                strResult += strInputTemp[i];
            }
            else
            {
                strResult += szNumMap[strInputTemp[i]-‘A‘];
            }
        }
        strResult.insert( 3, 1, ‘-‘ );
        mResultMap[strResult]++;
 
        --nCaseCount;
    }
 
    bool bRepeat = false;
    for ( map<string,int>::const_iterator it = mResultMap.begin();
        it != mResultMap.end(); ++it )
    {
        if ( it->second > 1 )
        {
            cout << it->first << " " << it->second << endl;
            bRepeat = true;
        }
    }
    if ( !bRepeat )
    {
        cout << "No duplicates." << endl;
    }
 
    return 0;
}

//解法3
typedef struct
{
    int identy;
    char str[9];
    int icount;
}Phone;
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100000
int cmp(const void *a, const void *b)
{
    int *x = (int *)a;
    int *y = (int *)b;
    return (*x) - (*y);
}
int main()
{
    int cnt = 0;   //存放电话号码个数
    int iter = 0;
    int Noduplicates = 0;  //有无重复号码的标志,为0则表示无重复
    int Sum = 0;
    int icount = 0;
    char ch;      // 接收键盘输入字符
    int *PHONE = (int *)malloc(sizeof(int) * (MAXSIZE + 10));
    scanf("%d",&cnt);
    getchar();
     
    //获取所有电话号码,转换为整型数据
    for(iter = 0; iter < cnt; iter++)
    {
        Sum = 0;
        ch = getchar();
        while(ch != ‘\n‘)
        {
            if(ch >= ‘0‘ && ch <= ‘9‘)
            {
                Sum = Sum * 10 + ch - ‘0‘ ;
            }
            else if(ch >= ‘A‘ && ch <= ‘P‘)
            {
                Sum = Sum * 10 + (ch - 65) / 3 + 2 ;
            }
            else if(ch >= ‘R‘ && ch <= ‘Y‘)
            {
                Sum = Sum * 10 + (ch - 66) / 3 + 2 ;
            }
            ch = getchar();
        }
        PHONE[iter] = Sum;
    }
     
    //用快排对电话号码排序
    qsort(PHONE, cnt, sizeof(int), cmp);
     
    //输出排好顺序的号码
    for(iter = 0, icount = 1; iter < cnt - 1 ; iter++)
    {
        icount = 1;
        //统计重复号码个数
        while(PHONE[iter] == PHONE[iter + 1])
        {
            iter++;
            icount++;
        }
        if(icount > 1)
        {
            Noduplicates = 1;
            printf("%03d-%04d %d\n",PHONE[iter] / 10000 ,PHONE[iter] % 10000 ,icount);
        }
    }
    if(Noduplicates == 0)
    {
        printf("No duplicates.\n");
    }
    getchar();
    return 0;
}
//解法4

#include<stdio.h>
#include<string.h>
#include <stdlib.h>
int cmp(const void *a, const void *b){
    return(*(int *)a-*(int *)b);
    }
int main(){
    char str[100];
    int num[26]={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};
    int n, a[100005], l, sum, i, k, flag, t, s, j, p;
    scanf("%d", &n);
    getchar();
    k = 0;
    while (k!=n){
        gets(str);
        l = strlen(str);
        sum = 0;
        for (i=0; i<l; i++){
            if (str[i]>= ‘0‘ && str[i] <= ‘9‘){
                sum = sum*10 + str[i]-‘1‘+1;
                continue;
            }
            if (str[i]>=‘A‘ && str[i]<=‘Z‘){
                if (str[i] == ‘Q‘ || str[i] == ‘Z‘) continue;
                sum = sum * 10 + num[str[i]-‘A‘];
            }
        }
        a[k] = sum;
        k++;
    }
    a[k] = -1;
    qsort(a, k, sizeof(a[0]), cmp);
    t = a[0];
    flag = 0;
    i = 1;
    s = 1;
    while (i!=k+1){
        if (t == a[i]){
            s++;
            flag = 1;
        }else {
            if (s>1){
                p = 1000000;
                for(j=1; j<=3; j++){
                    printf("%d", t/p);
                    t = t%p;
                    p = p/10;
                }
                printf("-");
               
                p = 1000;
                for (j=1; j<=4; j++){
                    printf("%d", t/p);
                    t = t%p;
                    p = p/10;
                }
                printf(" %d\n", s);
            }
            t = a[i];
            s=1;
        }
        i++;
    }
    if (flag == 0){
        printf("No duplicates.\n");
    }
    return 0;
}

  

487-3279的解法实例