POJ 1002 487-3279

//题意是让你找出相同的电话号码,
//问题难在字符与数字的转换上
//先转换为数字进行排序,再转化为字符串输出
//要注意开头为0的电话号码的情况,这个问题彻底把我整无奈了,
//
//关于字符串 与 数字的转换和操作函数还是有待提高
//最后附上我和别人的代码,我代码有点乱,我都不想看了。。。。

 

#include < iostream >
#include
< cstdlib >
#include
< memory >
#include
< string >
using   namespace  std;
int  cmp( const   void *  a, const   void *  b)
{
    
return   * ( int   * )a -* ( int   * )b;
}

int  change( char  c)
{
    
if (c >= ' 0 ' && c <= ' 9 ' )
        
return  c;
    
if (c == ' - ' return  c;
    
switch (c)
    {
        
case   ' A ' : case   ' B ' : case   ' C ' : return   ' 2 '
        
case   ' D ' : case   ' E ' : case   ' F ' : return   ' 3 '
        
case   ' G ' : case   ' H ' : case   ' I ' : return   ' 4 '
        
case   ' J ' : case   ' K ' : case   ' L ' : return   ' 5 '
        
case   ' M ' : case   ' N ' : case   ' O ' : return   ' 6 '
        
case   ' P ' : case   ' R ' : case   ' S ' : return   ' 7 '
        
case   ' T ' : case   ' U ' : case   ' V ' : return   ' 8 '
        
case   ' W ' : case   ' X ' : case   ' Y ' : return   ' 9 '
    }
}
    
int  main()
{
    
int  n,i,j,temp,num[ 100000 ],len,t,tag;
    
char  s[ 10 ],tt;
    
string  ph;
    scanf(
" %d " , & n);
    
for (temp = 0 ;temp < n; ++ temp)
    {
        cin
>> ph;
        len
= ph.length();
        j
= 0 ;
        
for (i = 0 ;i < len; ++ i)
        {
            tt
= change(ph[i]);
            
if (tt != ' - ' )
                s[j
++ ] = tt;
        }
        num[temp]
= atoi(s);
    }
    qsort(num,n,
sizeof (num[ 0 ]),cmp);
    tag
= 0 ;
    
for (i = 0 ;i < n; ++ i)
    {
        
        j
= 1 ;
        
while (i < n - 1 && num[i] == num[i + 1 ])
        {
            
++ j; ++ i;
        }
        
if (j == 1
            
continue ;
        tag
= 1 ;
        t
= num[i];
        
if (t / 1 == 0 ) temp = 7 ;
        
else   if (t / 10 == 0 ) temp = 6 ;
        
else   if (t / 100 == 0 ) temp = 5 ;
        
else   if (t / 1000 == 0 ) temp = 4 ;
        
else   if (t / 10000 == 0 ) temp = 3 ;
        
else   if (t / 100000 == 0 ) temp = 2 ;
        
else   if (t / 1000000 == 0 ) temp = 1 ;
        
else  temp = 0 ;
        sprintf(
& s[temp],  " %d " , num[i]); 
        
for (t = 0 ;t < temp; ++ t)
            s[t]
= ' 0 ' ;
        
for (t = 0 ;t < 8 ; ++ t)
            
if (t == 3 )
                printf(
" - " );
            
else   if (t > 3 )
                printf(
" %c " ,s[t - 1 ]);
            
else
                printf(
" %c " ,s[t]);
        printf(
"  %d\n " ,j);
    }
    
if (tag == 0 )
        printf(
" No duplicates.\n " );
    
return   1 ;
}

 

 

你可能感兴趣的:(poj)