找出两个有序数组中相同的元素
2016-01-07 22:05
357 查看
#include <stdio.h> #include <stdlib.h> #define M 5 #define N 7 void intersection(int array1[], int array2[]) { int i = 0, j = 0; while (i<M&&j<N) { //数组1第一个数和数组2第一个数比较,若小于,再用第二个数和数组2的第一个比较... while (array1[i]<array2[j]) i++; while (array1[i]>array2[j]) j++; while (array1[i] == array2[j]) { printf("%d ", array1[i]); //两个数组最后一个数字相同时 a[M-1] == a[N-1] == -858993460(vs2013环境下) if (i == M - 1 && j == N - 1) { return; } else { i++; j++; } } } } int main() { int array1[M] = { 1, 3, 4, 7, 11 }; int array2 = { 2, 3, 5, 6, 7, 8, 11 }; intersection(array1, array2); system("pause"); return 0; }
相关文章推荐
- 电子游戏编年史【1】——绝赞游戏集合
- 美国白宫发布《网络威慑战略》文件
- C Primer Plus之C预处理器和C库
- 运算符
- Execute a terminal command from a Cocoa app
- Microservice架构模式简介
- 代码比较软件Beyond Compare
- python学习资源
- 应用层的容错与分层设计
- 《软件需求最佳实践》——阅读笔记三
- BZOJ 1227 虔诚的墓主人
- Emacs
- centos7 NTP 服务器配置
- WAS集群安装<一>系统环境配置和规划
- 安装Apache提示APR not found的解决办法
- 斐波那契数列的前20项
- 使用@Controller注解为什么要配置<mvc:annotation-driven />
- iframe跨域访问
- [项目开发管理]
- listview在scrollview中自行滑动(滑动冲突)