王道训练营3月12日

找出n个数组中相同的元素

 1 int arrays_common(int arrs[][10], int cnt,  int* res, int len_res )
 2 {
 3     int* index_arr = (int*)calloc(cnt, sizeof(int));         //cnt是第一维 数组的编号
 4     int common_cnt = 0 ;                                    //result中数组的下标
 5     int max_index ;
 6     int min_val ;
 7     int min_index ;
 8     int index ;
 9     while(1)
10     {
11         for(index = 0 ; index < cnt - 1; index ++)
12         {
13             if( arrs[index][index_arr[index]] != arrs[index + 1][index_arr[index + 1]] )
14             {
15                 break ;
16             }
17         }
18         if(index < cnt - 1)
19         {
20             min_index = 0 ;
21             min_val = arrs[0][index_arr[0]] ;
22             for(index = 1; index < cnt; index ++)
23             {
24                 if(arrs[index][index_arr[index]] < min_val)
25                 {
26                     min_index = index ;
27                     min_val = arrs[index][index_arr[index]] ;
28                 }
29             }
30             index_arr[min_index] ++ ;
31         }else
32         {
33             res[common_cnt] = arrs[0][index_arr[0]] ;
34             common_cnt ++ ;
35             for(index = 0 ; index < cnt; index ++)
36             {
37                 index_arr[index] ++ ;
38             }
39         }
40         for(max_index = 0, index = 1; index < cnt; index ++)
41         {
42             if(index_arr[index] > index_arr[max_index])
43             {
44                 max_index = index ;
45             }
46         }
47         if(index_arr[max_index] >= 10)
48         {
49             return common_cnt;
50         }
51     }
52 }
时间: 2024-08-27 18:46:35

王道训练营3月12日的相关文章

王道训练营3月29日

二叉排序树非递归插入代码 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <malloc.h> 5 typedef struct node 6 { 7 int val; 8 struct node* lchild; 9 struct node* rchild; 10 }node,*pnode; 11 12 13 int creatTree(pnode *r

王道训练营3月11日

msmset 1 /*把buffer所指内存区域的前count个字节设置成字符c,返回buffer的指针*/ 2 #include <string.h> 3 extern void *meeset(void *buffer,int c,int count); gets 1 /*gets从标准输入设备读字符串函数.可以无限读取,不会判断上限,以回车结束读取,所以程序员应该确保buffer的空间足够大,以便在执行读操作时不发生溢出.*/ 2 #include <stdio.h> 3 c

王道训练营3月30日

MMAP函数 #include <sys/mman.h> void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offsize); mmap()用来将某个文件内容映射到内存中,对该内存区域的存取即是直接对该文件内容的读写. addr参数来请求使用某个特定的内存地址.如果置为NULL,就将自动分配.这是推荐的做法. length为文件大小. prot为设置的内存段的访问权限. offsize为偏移量.

王道训练营3月16日

fscanf 1 int fscanf(FILE * stream, const char *format, ...); 函数说明:fscanf()会自参数stream 的文件流中读取字符串, 再根据参数format 字符串来转换并格式化数据.格式转换形式请参考scanf(). 转换后的结构存于对应的参数 fprintf 1 int fprintf ( FILE * stream, const char * format, ... ); 函数说明:fprintf()函数根据指定的format(格

王道训练营3月10日

1 void* memset(void *s,int ch,size_t n); 2 //函数解释:将s中的前n个字节用ch替换并返回s. 3 //memset:作用是在一段内存快中填充某个给定的值,他是对较大的结构体或数组进行清零操作的一种最快方法.

王道训练营3月9日

DEBUG NDEBUG trace(),assert()都只是在DEBUG的模式下才起作用的,如果定义了NDEBUG,编译器会认为是非DEBUG的模式(虽然编译出来的程序还是很大,而且还可以进行调试),此时trace(),assert()就没有用了.就如同你编译成release版的时候这些没有用一样. else与最近的if配对 switch 中的case val:  val的值不能为浮点数,只能为字符或整数,如为字符串则用函数strncmp(str1,str2,n);

王道训练营3月7日

1 while(fflush(stdin),scanf("%d %c %d",&num1,&op,&num2)) 2 //刷新缓冲区,逗号表达式的值取最后一个值,scanf函数返回成功读取的变量个数,失败返回EOF

王道训练营3月14日

C++必看书籍: C++编程思想   STL源码剖析  C++primer 简历上的所有项目产生的问题都需要能够讲明白 查找:哈希,二分 排序:快速,冒泡,选择,堆,插入 数据结构:map vector TOP K问题

王道训练营3月8日

移位操作 右移--补符号位 左移--全部补0 右移一位相当于除以二并取整 左移以为相当于乘以二 端存储 http://blog.csdn.net/favory/article/details/4441361 101个数,50个出现了2次,1个出现了1次,求出此数.