int main() {
int n, found, query_no = 0;
unsigned long int *phone;
char **str, query[30], c;
scanf("%d", &n);
str = (char**) malloc(sizeof(char*) * n); //creating a pointer array of strings
phone = (unsigned long int*) malloc(sizeof(unsigned long int) * n);
for (int i = 0; i < n; i++) {
str[i] = (char*) malloc(sizeof(char) * 30); //iitializing each pointer in arry
}
for (int i = 0; i < n; i++) {
scanf("%s", *(str + i));
scanf("%u", phone + i);
}
gets(query);
char *p;
p = query;
while (*(p = gets(query)) != ' ') //checks if blank line is entered
{
found = 0;
for (int j = 0; j < n; j++) {
/*two pointers are being passed here,*/
/* one to the line entered and */
/* other to each character array pointed to by the pointer array*/
if (strcmp(p, *(str + j)) == 0) {
printf("%s", *(str + j));
printf("=");
printf("%u", *(phone + j));
found = 1;
break;
}
}
if (found == 0) //if record not found, print so
printf("Not found");
printf("n");
}
代码首先输入要插入的记录数量,然后在每条新行中输入一个记录,以名称编号的形式输入记录。然后,用户搜索一个未知的数字记录,如果找到记录,我们打印名称和号码,否则未找到。
来源: hackerrank 30天代码第8天(我希望我不要在这里违反任何规则,请告诉我,如果我是(
以下提出的代码:
- 清洁编译
- 执行所需功能
- 避免使用堆分配功能,例如
malloc()
- 允许通过#Define语句轻松修改" N"的实际最大值。
- 允许通过#Define语句轻松修改最大名称长度。
- 将数据表示为简单的
struct
。 - 避免从C标准中删除的调用函数(即
gets()
(。 - 因为它是" hackerrank"项目,因此不执行错误检查。
- 读取"查询"的空白行时停止
- 请注意使用
size_t
而不是int
,因为这些值永远不会&lt; 0
现在提出的代码:
#include <stdio.h>
#include <string.h>
#define MAX_NAMES 500
#define MAX_NAME_LEN 30
struct phoneBook
{
char name[ MAX_NAME_LEN + 1]; // +1 to allow for NUL terminator
unsigned long phone;
};
struct phoneBook myPhoneBook[ MAX_NAMES ];
int main( void )
{
size_t n;
scanf("%lu", &n);
for (size_t i = 0; i < n; i++)
{
scanf( "%29s", myPhoneBook[i].name );
char *newline;
if( NULL != (newline = strchr( myPhoneBook[i].name, 'n' ) ) )
{
*newline = ' ';
}
scanf( "%lu", &myPhoneBook[i].phone );
}
char query[MAX_NAME_LEN+2]; // +2 to allow for NUL byte and newline
while ( fgets( query, sizeof(query), stdin ) )
{
if( query[0] == 'n' )
break;
char *newline;
if( NULL != (newline = strchr( query, 'n' ) ) )
{
*newline = ' ';
}
size_t found = 0;
for ( size_t j = 0; j < n; j++)
{
/*two pointers are being passed here,*/
/* one to the line entered and */
/* other to each character array pointed to by the pointer array*/
if (strcmp( query, myPhoneBook[j].name ) == 0)
{
printf( "%s", myPhoneBook[j].name );
printf( "=" );
printf( "%lu", myPhoneBook[j].phone );
found = 1;
break;
}
}
if ( !found ) //if record not found, print so
printf("Not found");
printf("n");
}
return 0;
}
注意:printf()
和scanf()
很慢。建议修改建议的代码以使用以下两个函数,因此切勿致电printf()
和scanf()
:
void fastRead( size_t *a );
void fastWrite( size_t a );
inline void fastRead(size_t *a)
{
int c=0;
// note: 32 is space character
// consume leading trash
while (c<33) c=getchar_unlocked();
// initialize result value
*a=0;
// punctuation parens, etc are show stoppers
while (c>47 && c <58)
{ // then in range 0...9
*a = (*a)*10 + (size_t)(c-48);
c=getchar_unlocked();
}
//printf( "%s, value: %lun", __func__, *a );
} // end function: fastRead
inline void fastWrite(size_t a)
{
char snum[20];
//printf( "%s, %lun", __func__, a );
int i=0;
// decompose 'a' into char array
do
{
// 48 is numeric character 0
snum[i++] = (char)((a%10)+(size_t)48);
a=a/10;
}while(a>0);
i=i-1; // correction for overincrement from prior 'while' loop
while(i>=0)
{
putchar_unlocked(snum[i--]);
}
putchar_unlocked('n');
} // end function: fastWrite