×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

thanks!

** Open a file and read every word that contains an 'a'.
int main()
{
int count = 0;
char data[256];
FILE *f = fopen("file.txt", "r");
if (f) while (scanf("%s", data)) count += tolower(*data)=='a';
printf("%d words beginning with a\n", count);
fclose(f);
}

** Write a binary search algorithm.

int binsearch(int t[], int v, int imin, int imax)
{
while (imax>=imin)
{
int imid = (max+min)/2;
if (t[imid]==v) return imid;
if (t[imid]>v) imax=imid-1; else imin=imid+1;
}
return -1;
}

** Insert a number into a linked list in ascending order

void insert(Node **node, int v)
{
Node *tmp = (Node *)malloc(sizeof(Node));
while(*node && (*node)->value>v) node = &(*node)->next;
tmp->value = v;
tmp->next = *node;
*node = tmp;
}
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / 微软的几道面试题,把答案贡献给需要的人
    本文发表在 rolia.net 枫下论坛** Reverse a string

    char *strrev(char *str)
    {
    if (str) {
    char *s=str, *e = s + strlen(s);
    while(s<e) { char tmp=*--e; *e=*s; *s++=tmp; }
    }
    return str;
    }


    ** Reverse a link list

    Node *reverse(Node *list)
    {
    Node *newlist=0;
    while(list)
    {
    Node *tmp= list;
    list = list->next;
    tmp->next = newlist;
    newlist = tmp;
    }
    return newlist;
    }

    ** Remove all elements with a given value from a list

    void remove(Node **node, int v)
    {
    while (*node) {
    if ((*node)->value = v) {
    Node *tmp = *node;
    *node = (*node)->next;
    free(tmp);
    } else node = &(*node)->next;
    }
    }

    ** Remove all elements with a given value from a list
    void remove(Node **node, int v)
    {
    while (*node) {
    if ((*node)->value = v) {
    Node *tmp = *node;
    *node = (*node)->next;
    free(tmp);
    } else node = &(*node)->next;
    }
    }更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • (*node)->value = v should be (*node)->value == v
      • thanks!
        ** Open a file and read every word that contains an 'a'.
        int main()
        {
        int count = 0;
        char data[256];
        FILE *f = fopen("file.txt", "r");
        if (f) while (scanf("%s", data)) count += tolower(*data)=='a';
        printf("%d words beginning with a\n", count);
        fclose(f);
        }

        ** Write a binary search algorithm.

        int binsearch(int t[], int v, int imin, int imax)
        {
        while (imax>=imin)
        {
        int imid = (max+min)/2;
        if (t[imid]==v) return imid;
        if (t[imid]>v) imax=imid-1; else imin=imid+1;
        }
        return -1;
        }

        ** Insert a number into a linked list in ascending order

        void insert(Node **node, int v)
        {
        Node *tmp = (Node *)malloc(sizeof(Node));
        while(*node && (*node)->value>v) node = &(*node)->next;
        tmp->value = v;
        tmp->next = *node;
        *node = tmp;
        }
    • more......
      ** Open a file and read every word that contains an 'a'.
      int main()
      {
      int count = 0;
      char data[256];
      FILE *f = fopen("file.txt", "r");
      if (f) while (scanf("%s", data)) count += tolower(*data)=='a';
      printf("%d words beginning with a ", count);
      fclose(f);
      }

      ** Write a binary search algorithm.

      int binsearch(int t[], int v, int imin, int imax)
      {
      while (imax>=imin)
      {
      int imid = (max+min)/2;
      if (t[imid]==v) return imid;
      if (t[imid]>v) imax=imid-1; else imin=imid+1;
      }
      return -1;
      }

      ** Insert a number into a linked list in ascending order

      void insert(Node **node, int v)
      {
      Node *tmp = (Node *)malloc(sizeof(Node));
      while(*node && (*node)->value>v) node = &(*node)->next;
      tmp->value = v;
      tmp->next = *node;
      *node = tmp;
      }