| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
好奇怪的问题啊,就换一个变量就过了,各位高手 ,谁能解释一下以下是错误程序 ,注意看划线部分
#include<stdio.h>
#include<stdlib.h>
int cmp ( const void *a ,const void *b )
{
int *m = (int *)a, *n = (int *)b ;
if ( *m > *n )
return 1 ;
else
if ( *m == *n )
return 0 ;
else
if ( *m < *n )
return -1;
else
return 0;
}
int main ()
{
int d[26] = { 2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9 };
int i , j , k, n ;
int s[10000] ,tt ;
int no[110000]={0} ;
char temp[500] ;
scanf ( "%d",&n ) ;getchar ();
for ( i = 0 ;i < n ;i ++ )
{
gets ( temp );
for ( j = 0 ; temp[j] ;j ++ )
{
if ( temp[j] >= '0' && temp[j] <= '9' )
no[i] = no[i] * 10 + temp[j] - '0' ;
if ( temp[j] >= 'A' && temp[j] <= 'Z' )
no[i] = no[i] * 10 + d[temp[j] - 'A'] ;
}
}
qsort ( no, n, sizeof ( int ), cmp ) ;
/////////////////////////////////////////////////////
for ( k = i = 0 ; i < n ;) //s[k] 是号码重复次数
{
s[k] = 1 ;
~~~~~~~~~~~~~
for ( j = i + 1;j < n && no[j] == no[i] ;j ++ )
s[k] ++ ;
if ( s[k] > 1 )
{
tt = no[i] / 10000 ;
printf ( "%03d-%04d %d\n",tt ,no[i] % 10000,s[k] );
k ++ ;
~~~~~~~~~
}
i = j ;
}
if ( k == 0 )
printf ( "No duplicates.\n" ) ;
return 0;
}
//////////////////////////////////////
将斜线部分改为以下代码,AC
mark = 1 ;
for ( i = 0 ; i < n ;)
{
count = 1 ;
for ( j = i + 1;j < n && no[j] == no[i] ;j ++ )
count ++ ;
if ( count > 1 )
{
mark = 0 ;
tt = no[i] / 10000 ;
printf ( "%03d-%04d %d\n",tt ,no[i] % 10000,count );
}
i = j ;
}
if ( mark )
printf ( "No duplicates.\n" ) ;
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator