C程序的算法测试总结

编程题:

(1)       一只猴子一天吃半个苹果,第二天吃前一天的一半,十天后,还剩下10个,问总共有多少个苹果,用递归实现,使用递归的优缺点。

#include <stdio.h>

 

int m_count(int d)

{

        int total =0;

        total = d/2;

        while(d < 10)

        {

                d++;

                total = m_count(d);

        }

        return total;

}

 

int main()

{

        int mCount = m_count(1);

        mCount += 10;

        printf("==========%d============/n", mCount);

}

 

(2)       使用乘7的最好方法

int main()

{

        //int a = 1;

        for(int a=1; a < 10; a++)

        {

                int mPre = a-1;

                int res = (a << 3)-1;

                res -= mPre;

                printf("--------%d------------/n",res);

        }

 

}s

 

(3)       判断某个数是否是2N次幂

#include <stdio.h>

 

int main()

{

        for(int i=2 ; i < 30; i++)

                if(i%2 == 0)

                        printf("=======%d : yes=========/n", i);

                else

                        printf("=========%d : No===========/n", i);

 

}

 

(4)       交换两个数,不使用第三个变量

#include <stdio.h>

int main()

{

        int a=10;

        int b=20;

        printf("====Before swap: a=%d b=%d ======/n", a, b);

        a = a+b;

        b = a-b;

        a = a-b;

 

        printf("====After swap: a=%d b=%d ======/n", a, b);

}

 

(5)       写一个双向链表的删除函数。

2.双向链表的删除操作

// * ---------------------------------------- */
// * 
双向链结串列的节点删除                  */
// * ---------------------------------------- */
struct dlist                      // *
双向串列结构宣告
      */
{
   int data;                      // *
节点资料
              */
   struct dlist *front;           // *
指向下一节点的指标
    */
   struct dlist *back;            // *
指向前一节点的指标
    */
};
typedef struct dlist dnode;       // *
双向串列新型态
        */
typedef dnode *dlink;             // *
双向串列指标新型态    */

 

dlink deletenode(dlink head,dlink ptr)
{
   if ( ptr->back == NULL )       // *
是否有前节点          */
   {
      // *
第一种情况: 删除第一个节点
*/
      head = head->front;         // *
指向下一个节点
        */
      head->back = NULL;          // *
设定指向前节点指标
    */
   }
   else
   {
      if ( ptr->front == NULL )   // *
是否有下一个节点
      */
      {
         // *
第二种情况: 删除最後一个节点
*/
         ptr->back->front = NULL; // *
前节点指向
NULL        */
      }
      else
      {
         // *
第三种情况: 删除中间的节点
*/
         ptr->back->front = ptr->front; // *
前节点指向下一节点
*/
         ptr->front->back = ptr->back;  // *
下一节点指向前节点
*/
      }
   }
   free(ptr);                     // *
释回删除节点记忆体
    */
   return head;                   // *
传回串列起始指标
      */
}

 

 

 

 

 

void   GetMemory(char   *p)
{
p   =   (char   *)malloc(100);
}
void   Test(void)  
{
char   *str   =   NULL;
GetMemory(str);
strcpy(str,   "hello   world ");
printf(str);
}

请问运行Test函数会有什么样的结果?

答:程序崩溃。

因为GetMemory并不能传递动态内存,

Test
函数中的   str一直都是   NULL

strcpy(str,   "hello   world ");
将使程序崩溃

 

 

void   GetMemory2(char   **p,   int   num)
{
*p   =   (char   *)malloc(num);
}
void   Test(void)
{
char   *str   =   NULL;
GetMemory(&str,   100);
strcpy(str,   "hello ");
printf(str);
}
请问运行Test函数会有什么样的结果?
答:
1)能够输出hello
2)内存泄漏

1、头文件中的   ifndef/define/endif   干什么用?

 答: 防止该头文件被重复引用

3const   有什么用途?(请至少说明两种)

1)可以定义   const   常量 
2const可以修饰函数的参数、返回值,甚至函数的定义体。被const修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性

 

 

 

Typedef struct Text_st{

       Char a;

}Node;

Printf(“%d/n”, sizeof(Node))

 

答:1

这个题目太简单了,不过我竟然想那么复杂为4对齐。竟然认为是4,真是有疑问的东西一定要验证才行。 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值