小算法总结一下

来源:互联网 发布:json html 示例输出 编辑:程序博客网 时间:2024/05/16 19:51

自己来尝试把这些算法写下来, 供参考, 如有误,请指出,谢谢。


1. BD小题

单向链表排序(不限算法)

typedef struct Node

{

    NSUInteger numData;

    struct Node *nextNode;

}LinkNode;


// 用来显示,看是否成功

- (void)showLink:(LinkNode *)theLink{

    struct Node *tmp = theLink;

    for (; tmp->nextNode != NULL; tmp = tmp->nextNode) {

        NSLog(@"cur node numData=%d", tmp->numData);

    }

}


- (void)sortTheLink:(LinkNode *)theLink {

    if (theLink == NULL) {

        return;

    }

    

    LinkNode *p = theLink;

    LinkNode *q = NULL;

    

    for (; p->nextNode != NULL; p=p->nextNode) {

        q = p->nextNode;

        

        for (; q != NULL; q = q->nextNode) {

            if (p->numData > q->numData) {

                NSInteger tem = q->numData;

                q->numData  = p->numData;

                p->numData = tem;

            }

        }

    }

}



- (void)btnClicked:(id)sender {

    // 创建链接

    LinkNode *head = (LinkNode *)malloc(sizeof(LinkNode));

    head->numData =0;

    head->nextNode =NULL;

    

    LinkNode *tmp = head;

    for (NSInteger i=0; i<10; i++) {

// 随意给10个结点,后面将对它们中的内容进行排序

        LinkNode *singleNode = (LinkNode *)malloc(sizeof(LinkNode));

        if (i<5) {

            singleNode->numData =19-i;

        }else {

            singleNode->numData =9-i;

        }

        tmp->nextNode = singleNode;

        singleNode->nextNode =NULL;

        

        tmp = singleNode;

    }

    

    [selfshowLink:head];


    [selfsortTheLink:head];

    

    [selfshowLink:head];

}

输出如下:

2013-05-07 19:06:52.387 listTe[2542:11303] cur node numData=0

2013-05-07 19:06:52.387 listTe[2542:11303] cur node numData=19

2013-05-07 19:06:52.388 listTe[2542:11303] cur node numData=18

2013-05-07 19:06:52.388 listTe[2542:11303] cur node numData=17

2013-05-07 19:06:52.388 listTe[2542:11303] cur node numData=16

2013-05-07 19:06:52.388 listTe[2542:11303] cur node numData=15

2013-05-07 19:06:52.389 listTe[2542:11303] cur node numData=4

2013-05-07 19:06:52.389 listTe[2542:11303] cur node numData=3

2013-05-07 19:06:52.389 listTe[2542:11303] cur node numData=2

2013-05-07 19:06:52.390 listTe[2542:11303] cur node numData=1

//  排序后

2013-05-07 19:06:52.390 listTe[2542:11303] cur node numData=0

2013-05-07 19:06:52.390 listTe[2542:11303] cur node numData=1

2013-05-07 19:06:52.390 listTe[2542:11303] cur node numData=2

2013-05-07 19:06:52.391 listTe[2542:11303] cur node numData=3

2013-05-07 19:06:52.391 listTe[2542:11303] cur node numData=4

2013-05-07 19:06:52.391 listTe[2542:11303] cur node numData=15

2013-05-07 19:06:52.391 listTe[2542:11303] cur node numData=16

2013-05-07 19:06:52.392 listTe[2542:11303] cur node numData=17

2013-05-07 19:06:52.392 listTe[2542:11303] cur node numData=18

2013-05-07 19:06:52.392 listTe[2542:11303] cur node numData=19


2.1. MT小题
整型数转字符串

-(char *)convertToChar:(int)number

{

    int weishu = 0;

    int temp = number;

    

    if (temp == 0) {

        weishu = 1;

    }

    

    for (; temp%10 != 0; temp = temp/10) {

        weishu++;

    }

    

    int cacul = number;

    char *resultStr = (char *)malloc(weishu+1);

    if (resultStr == nil) {

        NSLog(@"error, check it");

        return nil;

    }

    for (int i=weishu-1; i>=0; i--) {

        resultStr[i]=cacul%10 +48;

        cacul = cacul /10;

    }

    resultStr[weishu] ='\0';

    

    NSLog(@"resultString=%s", resultStr);

    return resultStr;

}


- (void)convertNumberToString:(id)sender {

    [selfconvertToChar:892];

}

输出:

2013-05-07 18:23:32.329 listTe[2542:11303] resultString=892


2.2 MT小题2
计算一个整数数组中所有元素的和
long sum(int *numData, unsigned int count) {
asset(numData != nil);
long result = 0;
for(unsigned i=0; i<count; i++) {
result += numData[i];
}
return result;
}

3.1. ZCSY小题,写strcpy函数
char *my_strcpy(char *dst,const char *src)  
{  
    assert(dst != NULL);  
    assert(src != NULL);  
    char *ret = dst;  
    while((* dst++ = * src++) != '\0') ;  
    return ret;  
}
然而这样的实现没有考虑拷贝时内存重叠的情况,下面的测试用例就能使调用my_strcp函数的程序崩溃:
char str[10]="abc";
my_strcpy(str+1, str);

然而调用系统的strcpy函数程序正常运行,打印str结果为“aabc”!可见系统strcpy函数的实现不是这样的。

strcpy的正确实现应为:

[cpp] view plaincopy
  1. char *my_strcpy(char *dst,const char *src)  
  2. {  
  3.     assert(dst != NULL);  
  4.     assert(src != NULL);  
  5.     char *ret = dst;  
  6.     memcpy(dst,src,strlen(src)+1);  
  7.     return ret;  
  8. }  
memcpy函数实现时考虑到了内存重叠的情况,可以完成指定大小的内存拷贝,这里仅粘帖函数memcpy函数的实现

  1. void * my_memcpy(void *dst,const void *src,unsigned int count)  
  2. {  
  3.      assert(dst);  
  4.      assert(src);  
  5.      void * ret = dst;  
  6.      if (dst <= src || (char *)dst >= ((char *)src + count))//源地址和目的地址不重叠,低字节向高字节拷贝  
  7.      {  
  8.          while(count--)  
  9.          {  
  10.              *(char *)dst = *(char *)src;  
  11.              dst = (char *)dst + 1;  
  12.              src = (char *)src + 1;  
  13.          }  
  14.      }  
  15.      else                       //源地址和目的地址重叠,高字节向低字节拷贝  
  16.      {   
  17.          dst = (char *)dst + count - 1;  
  18.          src = (char *)src + count - 1;   
  19.          while(count--)   
  20.          {  
  21.              *(char *)dst = *(char *)src;  
  22.              dst = (char *)dst - 1;  
  23.              src = (char *)src - 1;  
  24.          }  
  25.     }  
  26.     return ret;  
  27. }  

 杂项

strlen的实现很简单:

如下:

[cpp] view plaincopy
  1. int my_strlen(const char* p)  
  2. {  
  3.     assert(p != NULL);  
  4.     int len =0;  
  5.     while (*p++)  
  6.     {  
  7.         len++;  
  8.       
  9.     }  
  10.     return len;  
  11.   
  12. }  

 

strcmp的实现:

 

[cpp] view plaincopy
  1. int my_cstrcmp(const char* p , const char* q)  
  2. {  
  3.     assert(p != NULL && q != NULL);  
  4.     //循环找到第一个不相等的字符  
  5.     for (; *p == * q; ++p, ++q)  
  6.     {  
  7.         if (*p == 0)  
  8.         {  
  9.             return 0;  
  10.         }  
  11.     }  
  12.     return *(unsigned char*)p - *(unsigned char*)q >0 ? 1:-1;  
  13.   
  14.     
  15. }  

 

[cpp] view plaincopy
  1. strcpy函数实现:  
  2.   
  3.  char* myStrcpy(char* des, const char* source)  
  4. {  
  5.  assert(des != NULL && source != NULL);  
  6.  char* ret = des;  
  7.  while ( (*des++ = *source++) != '/0');  
  8.      
  9.      return ret;  
  10. }  
  

 

itoa 函数实现:

[cpp] view plaincopy
  1. void itoaTest(int num,char str[] )     
  2. {     
  3.        int sign = num,i = 0,j = 0;     
  4.        char temp[11];     
  5.        if(sign<0)//判断是否是一个负数     
  6.        {     
  7.               num = -num;     
  8.        };     
  9.        do    
  10.        {     
  11.               temp[i] = num%10+'0';             
  12.               num/=10;     
  13.               i++;     
  14.        }while(num>0);     
  15.        if(sign<0)     
  16.        {     
  17.               temp[i++] = '-';     
  18.        }     
  19.        temp[i] = '/0';     
  20.        i--;     
  21.        while(i>=0)     
  22.        {     
  23.               str[j] = temp[i];     
  24.               j++;     
  25.               i--;     
  26.        }     
  27.        str[j] = '/0';     
  28. }     

 

atoi函数实现:

 

[cpp] view plaincopy
  1. int atoiTest(char s[])     
  2. {     
  3.  int i = 0,sum = 0,sign;    //输入的数前面可能还有空格或制表符应加判断     
  4.  while(' '==s[i]||'/t'==s[i])     
  5.  {     
  6.   i++;     
  7.  }     
  8.  sign = ('-'==s[i])?-1:1;     
  9.  if('-'==s[i]||'+'==s[i])     
  10.  {     
  11.   i++;     
  12.  }     
  13.  while(s[i]!='/0')     
  14.  {     
  15.   if (s[i] >= 0 && s[i] <= '9')  
  16.   {  
  17.    sum = s[i]-'0'+sum*10;     
  18.    i++;    
  19.   }  
  20.   else  
  21.   {  
  22.    break;  
  23.   }  
  24.      
  25.  }         
  26.  return sign*sum;     
  27. }     
  

 反转字符串递归实现:

 

[cpp] view plaincopy
  1. char* reverse(char *str)  
  2.   
  3. {  
  4.   
  5. assert(str != NULL);  
  6.   
  7. int iLen = strlen(str);  
  8.   
  9. char c;  
  10.   
  11. int i;  
  12.   
  13.   
  14.   
  15.   
  16.   
  17. if (iLen<2)  
  18.   
  19. {  
  20.   
  21. return NULL;  
  22.   
  23. }  
  24.   
  25. else  
  26.   
  27. {  
  28.   
  29. //先把首尾交换  
  30.   
  31. c = str[0];  
  32.   
  33. str[0] = str[iLen-1];  
  34.   
  35. str[iLen-1] = c;  
  36.   
  37.   
  38.   
  39. //如果长度为2的话,返回null  
  40.   
  41. if (iLen == 2)  
  42.   
  43. {  
  44.   
  45. return NULL;  
  46.   
  47. }  
  48.   
  49.   
  50.   
  51.   
  52.   
  53. // 辅助空间,用来保存原字符串没有反转的部分  
  54.   
  55. char *str2 = (char*)malloc(iLen-1);  
  56.   
  57. memset(str2, '/0', iLen-1);  
  58.   
  59.   
  60.   
  61. //拷贝字符串  
  62.   
  63. for (i=0; i<iLen-2; i++)  
  64.   
  65. {  
  66.   
  67. str2[i] = str[i+1];  
  68.   
  69. }  
  70.   
  71.   
  72.   
  73. //反转中间字符串  
  74.   
  75. reverse(str2);   
  76.   
  77.   
  78.   
  79. //把反转后的字符串copy到原字符串  
  80.   
  81. for (i=0; i<iLen-2; i++)  
  82.   
  83. {  
  84.   
  85. str[i+1] = str2[i];  
  86.   
  87. }  
  88.   
  89.   
  90.   
  91.   
  92.   
  93. free(str2);  
  94.   
  95. str2 = NULL;  
  96.   
  97. }  
  98.   
  99.   
  100.   
  101.   
  102.   
  103. return str;   
  104.   
  105. }