常用算法题目总结一(数组篇)

来源:互联网 发布:淘宝人群标签是什么 编辑:程序博客网 时间: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;   }
原创粉丝点击