这篇文章给大家介绍如何找出一个整数数组中的第二大数,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
可能以故事形式:如从第一层到第十层电梯每层停一次,如何找到第二个大的钻石
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;
}
关于如何找出一个整数数组中的第二大数就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。