可能以故事形式:如从第一层到第十层电梯每层停一次,如何找到第二个大的钻石
1 #include<stdio.h>
2 #include<assert.h>
3 #define MINNUMBER -32768
4 int find_sec_max(int arr[],int size)
5 {
6 assert(arr);
7 int maxnumber=arr[0];
8 int sec_max=MINNUMBER;
9 int i=0;
10 for(i=1;i<size;++i)
11 {
12 if(arr[i]>maxnumber)
13 {
14 sec_max=maxnumber;
15 maxnumber=arr[i];
16 }
17 else
18 {
19 if(arr[i]>sec_max)
20 sec_max=arr[i];
21 }
22 }
23 return sec_max;
24 }
25 int main()
26 {
27 int arr[10]={3,2,4,5,6,7,8,9,9,10};
28 printf("In arr,the second max: %d\n",find_sec_max(arr,10));
29 return 0;
30 }判断单链表是否带环?
1 #include<stdio.h>
2 struct LinkNode
3 {
4 char val;
5 LinkNode* next;
6 };
7 bool check(const LinkNode* head)
8 {
9 if(head==NULL)
10 return false;
11 LinkNode* low=head,*fast=head->next;
12 while(fast&&fast->next)
13 {
14 low=low->next;
15 fast=fast->next->next;
16 if(low==fast)
17 return true;
18 }
19 return false;
20 }找出两个数中最大的一个:有两个int变量A和B,请不要使用if,?:和switch或其他判断语句,找出两个数中最大的一个。
方案一:
int max=((a+b)+abs(a-b))/2;
方案二:
int c=a-b; c=unsigned(c)>>(sizeof(int)*8-1);
写一个函数返回1+2+3+....+n的值(假定结果不会超过长整型变量的范围)
int sum(int n)
{
return ((long)1+n)*n/2;
}本文出自 “小止” 博客,请务必保留此出处http://10541556.blog.51cto.com/10531556/1762729
原文:http://10541556.blog.51cto.com/10531556/1762729