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 |
通过了,但还是感到有点困惑?说说我通过的程序1002a.c的思路: for ( n ){ 输入 翻译(或者说转换) } 排序; 统计输出; 原来我写的程序(1002b.c)思路是这样的: for ( n ){ 输入 翻译(或者说转换) } 统计重复的号码; 删除重复的以及不用输出(个数小于2)的号码; 把剩下的号码排序; 我觉得1002b.c的想法应该比较好的,因为把不用输出(多余)的号码删除了,所以后面的排序会节省时间的。但是这种想法写出来的程序就是通不过。下面是我这两个程序的代码: /* 1002a.c */ #include <stdio.h> #include <string.h> #include <stdlib.h> #define NUM_MAX_LEN 16 void translate(char *s, int *result){ int length = strlen( s ); int sindex; char c; *result = 0; for( sindex = 0; sindex < length; sindex++ ){ c = s[sindex]; if( c <= '9' && c >= '0'){ *result = ( *result * 10 ) + c - '0'; } else if( c >= 'A' && c <= 'R' ){ *result = ( *result * 10 ) + ( c - 'A' ) / 3 + 2; } else if( c >= 'S' && c <= 'Y' ){ *result = ( *result * 10 ) + ( c - 'A' - 1 ) / 3 + 2; } else if( c == '-' ){ continue; } else{ printf( "invalid char %c\n", c ); } } } int cmp( const void *p, const void *q ){ int *p1, *q1; p1 = ( int* )p; q1 = ( int* )q; return *p1 - *q1; } int main( ){ int n = 0; int i, flag, x, count; int *result; char input[NUM_MAX_LEN]; scanf( "%d", &n ); result = ( int* )malloc( sizeof( int ) * n ); for( i = 0; i < n; i++ ){ scanf( "%s", input ); translate( input, &( result[i] ) ); } qsort( result, n, sizeof( int ), cmp ); x = result[0]; for( i = 1, flag = 0, count = 1; i < n; i++ ){ if( result[i] == x ){ count++; } else{ if ( count > 1 ){ flag = 1; printf( "%03d-%04d %d\n", x / 10000, x % 10000, count ); } x = result[i]; count = 1; } } if ( count > 1 ){ flag = 1; printf( "%03d-%04d %d\n", x / 10000, x % 10000, count ); } if( flag == 0 ){ printf( "No duplicates.\n" ); } return 0; } /* 1002b.c */ /* * 先除去重复的号码,再排序 */ #include <stdio.h> #include <string.h> #include <stdlib.h> #define NUM_MAX_LEN 16 struct tel{ int num; int count; }; void translate(char *s, int *result){ int length = strlen( s ); int sindex; char c; *result = 0; for( sindex = 0; sindex < length; sindex++ ){ c = s[sindex]; if( c <= '9' && c >= '0'){ *result = ( *result * 10 ) + c - '0'; } else if( c >= 'A' && c <= 'R' ){ *result = ( *result * 10 ) + ( c - 'A' ) / 3 + 2; } else if( c >= 'S' && c <= 'Y' ){ *result = ( *result * 10 ) + ( c - 'A' - 1 ) / 3 + 2; } else if( c == '-' ){ continue; } else{ printf( "invalid char %c\n", c ); } } } int cmp( const void *p, const void *q ){ struct tel *p1 , *q1; p1 = ( struct tel *)p; q1 = ( struct tel *)q; return p1->num - q1->num; } int main( ){ int n = 0; int i, j, count; struct tel *result; char input[NUM_MAX_LEN]; scanf( "%d", &n ); result = ( struct tel* )malloc( sizeof( struct tel ) * n ); for( i = 0; i < n; i++ ){ scanf( "%s", input ); translate( input, &( result[i].num ) ); result[i].count = 0; } /* * get rid of the same string */ for( i = 0; i < n; i++){ if( result[i].count == -1 ){ continue; } result[i].count = 1; for( j = i + 1; j < n; j++){ if( result[j].count != -1 && result[j].num == result[i].num ){ result[i].count++; result[j].count = -1; } } } count = 0; for( i = 0; i < n; i++ ){ if( result[i].count > 1 ){ result[count].num = result[i].num; result[count].count = result[i].count; count++; } } if( count == 0 ){ printf( "No duplicates.\n"); return 0; } if( count == 1 ){ printf( "%03d-%04d %d\n", result[0].num / 10000, result[0].num % 10000, result[0].count ); return 0; } /* * arrange the string left */ qsort( result, count, sizeof( struct tel ), cmp ); for( i = 0; i < count; i++ ){ printf( "%03d-%04d %d\n", result[i].num / 10000, result[i].num % 10000, result[i].count ); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator