常用算法题目总结一(数组篇)
来源:互联网 发布:淘宝人群标签是什么 编辑:程序博客网 时间:2024/06/03 13:28
如何用递归实现数组求和?
代码如下:
#include "stdafx.h"#include<iostream>#include<string>using namespace std;template<class T>T GetSum(T* a,int n){ if(0==n) { return 0; } return GetSum(a,n-1)+*(a+n-1);}int _tmain(int argc, _TCHAR* argv[]){ int data[]={1,2,3,4,5}; cout<<GetSum<int>(data,5); system("pause"); return 0;}
如何用一个for循环打印出一个二维数组?
代码如下:
#include "stdafx.h"#include<iostream>using namespace std;void PrintArray(){ const int x=2; const int y=3; int data[x][y]={1,2,3,4,5,6}; for(int i=0;i<x*y;i++) { cout<<data[i/y][i%y]<<endl; //这里是重点 }}int _tmain(int argc, _TCHAR* argv[]){ PrintArray(); system("pause"); return 0;}
如何用递归算法判断一个数组是否是递增?
代码如下:
#include "stdafx.h"#include<iostream>using namespace std;template<class T>bool IsIncreaseArray(T* p,int n){ //这里默认数组长度大于等于2,因为数组长度小于2的话,问题无意义。 if(2==n) { return *p<*(p+1); } if(*(p+n-1)>*(p+n-2)) { return IsIncreaseArray(p,n-1); } else { return false; }}int _tmain(int argc, _TCHAR* argv[]){ int data[]={1,2,3,4,5}; cout<<IsIncreaseArray<int>(data,5); system("pause"); return 0;}
二分查找算法的实现
二分查找法也称折半查找法,它的思想是每次都与序列中的中间元素进行比较,前提是数组是有序的。
代码如下:
#include "stdafx.h"#include<iostream>#include<string>using namespace std;//非递归实现template<class T>int BinarySearch(T* a,const int len,const T data){ if(nullptr==a||len<=0) { return -1; } int low=0; int top=len-1; while(low<=top) { int pos=low+(top-low)/2; if(data==a[pos]) { return pos; } else if(data<a[pos]) { top=pos-1; } else { low = pos+1; } } return -1;}//递归实现template<class T>int BinarySearchRecursion(T* a,const T data,int low,int top){ if(nullptr==a||low>top) { return -1; } int pos=low+(top-low)/2; if(data==a[pos]) { return pos; } else if(data<a[pos]) { return BinarySearchRecursion(a,data,low,pos-1); } else { return BinarySearchRecursion(a,data,low+1,pos); }}int _tmain(int argc, _TCHAR* argv[]){ int data[]={1,2,3,4,5}; cout<<BinarySearch(data,5,4); cout<<BinarySearchRecursion(data,4,0,4); system("pause"); return 0;}
如何在排序数组中,找出给定数字出现的次数
下面这段代码是在二分查找法的基础上进行改进,找出最左侧的位置和最右测的位置,做差得出次数。
代码如下:
#include "stdafx.h"#include<iostream>using namespace std;template<class T>int BinarySearchCount(T* a, const int len, const T data, bool isLeft){ int left = 0; int right = len - 1; int last = -1; while (left <= right) { int pos = (left + right) / 2; if (data < a[pos]) { right = pos - 1; } else if (data > a[pos]) { left = pos + 1; } else { last = pos; if (isLeft) { right = pos - 1; } else { left = pos + 1; } } } return last;}int main(){ int data[] = { 0,1,2,2,2,2,2,2,2,34,45,56 }; int low = BinarySearchCount<int>(data, sizeof(data) / sizeof(data[0]), 2, true); int top = BinarySearchCount<int>(data, sizeof(data) / sizeof(data[0]), 2, false); cout << (top - low + 1); system("pause"); return 0;}
如何计算两个有序整型数组(没有重复元素)的交集?
例如 a={0,1,2,3,4};b={1,3,5,7,9}; 交集为{1,3}。
二路归并遍历两个数组
代码如下:
#include "stdafx.h"#include<stdio.h>#include<iostream>#include<vector>#include<map>using namespace std;
int mixed(int a[],int na,int b[],int nb,int m[]){ int i=0,j=0,k=0; while(i<na&&j<nb)//直到有一个数组结束遍历就停止 { if(a[i]==b[j]) { m[k++]=a[i]; ++i; ++j; } else if(a[i]<b[j]) { ++i; } else { ++j; } } return k;}
int _tmain(int argc, _TCHAR* argv[]) { int a[]={0,1,2,3,4}; int b[]={1,3,5,7,9}; int buff[5]; int len=mixed(a,5,b,5,buff); cout<<"mix data:"; for(int i=0;i<len;++i) { cout<<' '<<buff[i]; } return 0; }
如何找出数组中重复次数最多的数?
利用map映射表,first记录数值,second记录数值出现的次数。
代码如下:
#include "stdafx.h"#include<stdio.h>#include<iostream>#include<vector>#include<map>using namespace std;
bool findMaxFreq(int a[],int n,int&val){ if(n<1) return false; std::map<int,int> m_map; //map映射 for(int i=0;i<n;i++) { if(++m_map[a[i]]>=m_map[val]) val=a[i]; } return true;}
int _tmain(int argc, _TCHAR* argv[]) { int a[]={0,1,2,3,3,3,4,4}; int v=0; if(findMaxFreq(a,8,v)) cout<<v; return 0; }
阅读全文
0 0
- 常用算法题目总结一(数组篇)
- 常用算法题目总结二(数组篇)
- 树状数组题目总结(一)
- 常用算法题目总结四(字符串篇)
- 常用算法题目总结三(链表篇)
- 机器学习常用算法总结(一)
- javascript常用数组算法总结
- javascript常用数组算法总结
- javascript常用数组算法总结
- javascript常用数组算法总结
- JavaScript常用数组算法总结
- 常用排序算法总结(一)
- 常用排序算法总结(一)
- 常用排序算法总结(一)
- 常用排序算法总结(一)
- 常用排序算法总结(一)
- Array数组对象常用操作总结(一)
- 操作数组的常用算法总结
- lintcode 整数排序 II | 归并排序(Java)
- Activity跳转Fragment跳转Fragment跳转Fragment跳回Activity
- linux下二进制包,yum安装后Apache、php、mysql默认安装路径
- 从公众号到小程序:CCS小助手,是我无法估量的收获
- 2017年7月6号生活感悟
- 常用算法题目总结一(数组篇)
- 什么是标记语言
- mybatis动态创建临时表
- Oracle 数据库学习笔记04
- mysql事务(mysql TRANSACTION)(一)
- OpenCV2升级到OpenCV3
- linux下搭建简单的git服务器
- 困难的串(Krypton Factor,UVa 129)
- version 1.7.0_17 of the JVM is not suitable for this product , Version 1.8 or greater i s required