面试常见代码考题总结与详解

/*
11
10
----减1
10


110
101
----- 减1
100
011
-----
计算有多少个1
*/

//-------------------

     #include <stdio.h>
     #include <string.h>

     void main()
     {
              int x = 8;
              while((x>=2) && (x%2 == 0)&& (x = (x/2)) );

               printf("x:%d end  \n", x);
     }


 

 

/*
*
*检查2个数的加法,看是否溢出
*	 
*/
	#include <stdio.h>

	bool add( int a, int b, int *c)
	{
     *c = a + b;
     if(a > 0 && b > 0 && *c <0) return false;
     if(a < 0 && b < 0 && *c > 0) return false;
     if(*c < a && *c <b) return false;

     return true;
	}

	void main()
	{

	}

 

/*
*检查负数的16进制在32位上的值
*
*/
	#include <stdio.h>

	void main()
	{
     int a = -2;
     int b = -1;

     printf("a:%x \n",a);

	//FFFF, FFFF
	//0000, 0001

	//FFFF,FFF(0B1110) = -2
	// FFFF,FFF(0B1111) = -1    
	}


 

/*
*写一个函数,找出被重复的数字.时间复杂度必须为 o(N) 函数原型: int do_dup(int a[],int N) 
*do_dup 函数的实现	
*/	
#include <stdio.h>
#include <string.h>

int do_dup(int a[], int n)
{
     int i = 0;
     int temp = 0;

     for(i = 0; i < n; i++)
     {
          if(i == 0)
          {
               temp = a[i];
               continue;
          }     
         
          if((temp + 1) == a[i])
               temp = a[i];
          else
               return temp;    
     }
    
     return 0;
}

void main()
{
     int a[10] = {0,1,2,3,4,5,5,7,8,9};
     int b;

     b = do_dup(a, 10);

     printf("b:%d   \n", b);
}


 

/*
*写出一个函数的指针小应用;
*/

#include <stdio.h>

void fun1()
{
     printf("Hello world!\n");    
}

void main()
{
     void (*fun)();
     void fun2();

     fun = fun1;
    
     fun();
}


 

/*
*请看这个函数有什么毛病,差错经典题
*/
#include <stdio.h>

#define MAX 255

void main()
{
     unsigned char A[MAX], i;

     for(i = 0; i <= MAX; i++)
          A[i] = i;
    
     printf("This program can not exit the loop\n");    
}


 

/*
*指针与指针的指针应用
*/
//(1)
#include <stdio.h>

void main()
{
     int **p;
     int *p1;
     int arr[100];

     arr[0] = 10;
     p1 = &arr[0];
     p = &p1;

     printf("arr[0]:%d \n", **p);
    
}
//(2)
#include <stdio.h>
#include <string.h>

void main()
{
	int **p;
	int a = 190;
	int *b = &a;
	
	p = &b;
	printf("value:%d\n", **p);

}


 

/*
*从0...n的一组数,怎样把
*/

#include <stdio.h>

typedef struct QueueE_
{
     int num;
}QueueE;

struct SqQueue_
{
     QueueE *qe;    
     int front;
     int rear;
}  SqQueue;

#define MAX_SIZE   23
#define M   10

void main()
{
     int i = 0;
     int j = 0;
    
     SqQueue.qe = (struct QueueE_ *)malloc(MAX_SIZE*sizeof(struct QueueE_));
     SqQueue.front = 1;
     SqQueue.rear = 1;
     while(i < MAX_SIZE )
     {
          SqQueue.qe[i].num = i;
          i++;
     }

     printf("i:%d num:%d  \n", SqQueue.rear -1, SqQueue.qe[SqQueue.rear -1].num);
     SqQueue.rear = SqQueue.rear + M;
     while(SqQueue.rear != SqQueue.front)
     {
          printf("i:%d num:%d  \n", SqQueue.rear -1, SqQueue.qe[SqQueue.rear -1].num);
          SqQueue.rear = SqQueue.rear + M;
          if(SqQueue.rear > MAX_SIZE )
               SqQueue.rear = (SqQueue.rear % MAX_SIZE );
     }

}


 

/*
*同上,循环队列的指针
*
*/

#include <stdio.h>

struct queue
{
     int num;
     struct queue *next;
} *queue_st, *head, *temp;

#define N   23
#define M   10
int num[N];
int total = 0;

void main()
{
     int i = 0;
     int j = 0;
    
     head = (struct queue *)malloc(sizeof(struct queue));
     queue_st = head;
     i = 1;
     queue_st->num = i;
     queue_st->next = NULL;

     while(i < N)
     {
          num[i] = i;
          temp = (struct queue *)malloc(sizeof(struct queue));    
          temp->num = i;
          temp->next = head;
          queue_st->next = temp;
          queue_st = temp;
          i++;
     }
     temp = head;
     while(1)
     {
          j = 1;
          while(j <= M)
          {
              temp = temp->next;
               j++;
          }
          total = total + M;
          if(total % N)
              printf("num:%d\n", temp->num);
          else
               break;
     }

}


 

/*
*经典的地址偏移
*
*/

#include <stdio.h>
#include <string.h>

void main()
{
     int a[5] = {1,2,3,4,5};
     int *ptr = (int *)(&a + 1);

     printf("x:%d , %d end  \n", *(a+1), *(ptr-1));
}


 

/*
*sizeof 的开题,我们需要注意些什么?
*/
sizeof.c
#include <stdio.h>
#include <string.h>

unsigned int y = 1234;
unsigned int *x;


struct AA_
{
     double a:1;
     double b:1;
//     long b:2;

} AA;

struct BB_
{
     char ch;
     short sh;
     int   in;
} BB;

struct CC_
{
     char ch;
     int  in;
     short sh;
}CC;


struct s1
{
     int i:8;
     int j:4;
     int a:3;
     double b;
}S1;


void main()
{
	printf("AA:%d \n",sizeof(AA));
	printf("BB:%d \n",sizeof(BB));
	printf("CC:%d \n",sizeof(CC));
	printf("S1:%d \n",sizeof(S1));
}


 

/*
*注意,传参a++
*/

#include <stdio.h>
#include <string.h>

int SQUARE(int a)
{
     int x = 0;
     x = (a)*(a);
     printf("a:%d  ---\n", a);
     return x;
}

void main()
{
     int a = 5;
     int b;

     b = SQUARE(a++);
     printf("b:%d   \n", b);
}


 

/*
*字符串转整型变量
*/
#include <stdio.h>
#include <string.h>

int str2in(char *str)
{
     int len = strlen(str);
     int temp = 0;
     int srcLen = len;
     int srcNum = 0;
     int tempNum = 1;
     int j = 0;

     while(len>0)
     {
          temp = (int)((char)str[len-1] - '0');
          j = srcLen - len;
          while(j)
          {
               tempNum *= 10;
               j--;
          }
          temp = temp * tempNum + srcNum;
          srcNum = temp;    
          tempNum = 1;
          --len;
     }

     return temp;
}


void main()
{

     printf("num:%d  --\n",str2in("12045"));

}


 

/*
*2条有序的列表,变成1条有序列表
*/
/*

(1)        (2)

  0          3
  1          4
  2          5
  3          6
  4          7
  5          8
  6          9
  7    
*/
//方法1
#include <iostream> 
using namespace std; 

struct Link
{
     int num;
     struct Link *next;
}*pLinkB,*pLinkA;

void TwoInOne(struct Link *La, struct Link *Lb)
{
     struct Link *hLa, *hLb;
     struct Link *tmpLa, *tmpLb;
     struct Link *tmpLaNe, *tmpLbNe;

     tmpLa = hLa = La;
     tmpLb = hLb = Lb;

     while(tmpLa->next)
     {
          tmpLaNe = lmpLa->next;
          while( (tmpLb->num >= tmpLa->num) && (tmpLb->num <= tmpLaNe->num))
          {    
               tmpLbNe = tmpLb->next;

               tmpLa->next = tmpLb;
               tmpLb->next = tmpLaNe;
               tmpLa = tmpLa->next;

               tmpLb = tmpLbNe;
               if(tmpLb == NULL)
                    return;    
          }
         
          tmpLa = tmpLa->next;
     }

     if(tmpLb != NULL)
          tmpLa->next = tmpLb;

}
 
void main()
{
	pLinkA = malloc(sizeof(struct Link) * 7);
	pLinkB = malloc(sizeof(struct Link) * 8);

	//....初始化链表数据未做

	TwoInOne(  pLinkA, pLinkB);
}
 


 

//方法二:递归法
#include <iostream> 
using namespace std; 

struct Link
{
     int num;
     struct Link *next;
}*pLinkB,*pLinkA;

if(a0 < b0)
{
     (a0->next, b0);
}
else
{
     (b0->next,a0);
}

void recursion_2in1(struct Link *hLa, struct Link *hLb)
{
     struct Link *tmp;
     int tmpIn;


     if(hLa == NULL) return;
     if(hLb == NULL) return;

     if(hLa->num > hLb-> num)
     {
          tmp = hLa;
          tmpIn = hLb->num;
          tmp->num = hLb->num;
          hLb->num = tmpIn;

          hLa->next = hLb;
          hLa = hLa->next;
          hLa->next = tmp;

         
          recursion_2in1(hLa, hLb->next);
     }
     else
     {
          recursion_2in1(hLa->next, hLb);
     }

}

void main()
{
	pLinkA = malloc(sizeof(struct Link) * 7);
	pLinkB = malloc(sizeof(struct Link) * 8);

	//....初始化链表数据未做

	recursion_2in1(  pLinkA, pLinkB);

}


 

/*
*查找第二大的数
*/
#include <iostream> 
using namespace std; 

int find_sec_max(int arr[], int count)
{
     int sec_max = arr[0];
     int max = arr[0];
     int i = 0;

     for(i = 0; i < count; i++)
     {
          if(max < arr[i])
          {
               sec_max = max;
               max = arr[i];         
          }    
     }

     return sec_max;
}

int main() {

     int arr[] = {0,1,7,2,3,4,8,5,6,9};    
     int ret = 0;

     ret = find_sec_max(arr, 10);

     cout << "ret:" << ret << endl;

    return 0; 
}


 

//
//CPU大小端问题如何判断?
//

#include <stdio.h>

bool IsBigEndian()
{
	int a = 0x1234;
	char b = a;//*(char *)&a;

	printf("0x%02x\n", b);
	if (b == 0x12)
		return 1;
	
	return 0;
}

int main()
{
	IsbigEndian();

	return 0;
}


 

//
//sizeof 的又一种喜欢出题的方法
//

#include <stdio.h>

int main()
{	
	char *a = "123456";
	char a1[] = "123456";

	cout << "*a:" << sizeof(a) << endl;	
	cout << "a1:" << sizeof(a1) << endl;

	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值