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

Re:1002总结

Posted by lhdtigerblue at 2010-08-01 14:15:28 on Problem 1002
In Reply To:1002总结 Posted by:Thrush at 2010-07-26 23:46:56
> int num(char *tel)
> {
>     int i;
>     int n=0;
> 
>     for ( i=0; i<strlen(tel); i++ )
>         if ( tel[i]>='0' && tel[i]<='9' )
>             n = n * 10 + ( tel[i] - 48 );
>     return n;
> }
> 
> void toTel( int num, char *tel )
> {
>     int index;
> 
>     memset( tel, '0', 8 );
>     tel[8] = '\0';
>     tel[3] = '-';
>     index = 7;
>     while ( num ) {
>         if ( index == 3 ) index--;
>         tel[index--] = num % 10 + 48;
>         num /= 10;
>     }
> }
上面两个转换小函数不错。

> int main()
> {
>     int i;
>     int n;
>     char tel[500];
>     char real_tel[9];
>     int index;
>     int flag;
> 
>     scanf("%d",&n);
>     for ( i=0; i<n; i++ ){
>         scanf("%s",tel);
>         convert(tel,real_tel);
>         index = num(real_tel);
>         record_times[index] ++;
够bt,用这个做数组下标。然后用元素表示重复次数。
sizeof(int) * 10000000 ~= 40000KB, 再加上程序堆栈,逼近了限制。
幸好电话号码是7位而不是8位,也没有加区号。
>     }
>     flag = 0;
>     for ( i=0; i<10000000; i++ )
>         if ( record_times[i] > 1 ){
>             toTel(i,tel);
>             printf("%s %d\n",tel,record_times[i]-0);
>             flag = 1;
>         }
>     if ( flag == 0 )
>         printf("No duplicates.\n");
> 
>     return 0;
> }
> 

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