该程序在我的PC上运行良好,但是当我将其提交到编码网站上时,它说有一个细分故障.请指出某人的错误


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天(我希望我不要在这里违反任何规则,请告诉我,如果我是(

以下提出的代码:

  1. 清洁编译
  2. 执行所需功能
  3. 避免使用堆分配功能,例如malloc()
  4. 允许通过#Define语句轻松修改" N"的实际最大值。
  5. 允许通过#Define语句轻松修改最大名称长度。
  6. 将数据表示为简单的struct
  7. 避免从C标准中删除的调用函数(即gets()(。
  8. 因为它是" hackerrank"项目,因此不执行错误检查。
  9. 读取"查询"的空白行时停止
  10. 请注意使用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

相关内容

最新更新