C++实现基础的算法

针对找工作时,遇到的数据结构基础算法做一个简单的回顾和积累。

大数相乘:

//大数相乘
int Bigdata_multiple(char string1[], char string2[],int length1,int length2)
{
    int result[length1+length2+2];
    for (int iindex = 0; iindex < length1; iindex ++)
        for (int jindex = 0; jindex < length2; jindex ++)
        result[iindex+jindex+1] + = (string1[iindex]-'0') * (string2[jindex]-'0');

    for (rindex = 1; rindex < length1 + length2; rindex ++)
    {
        result[rindex - 1] + =result[rindex] / 10;
        result[rindex] = result[rindex] % 10;
    }
}

二分查找:

//二分查找
int Binary_search(int num[],int length,int NUM)
{
    int low = 0, high = length - 1;
    int mid;
    while(low <= high){
        mid = (low + high) / 2;
        if (num[mid] < NUM)
        low = mid + 1;
        else if (num[mid] > NUM)
        high = mid - 1;
        else
        return mid;
    }
    return -1;
}

全排列问题:

//针对字符串构造全排列
void Perm(int list[], int begin, int end)
{
    if (begin >= end)
    {
        for(int iindex = 0; iindex < end; iindex++)
        {
            cout<<list[iindex];
        }
        cout<<endl;
    }
    else
    {
        for(int jindex = begin; jindex < end; jindex++)
        {
            swap(list[jindex],swap[begin]);
            Perm(list[],begin+1,end);
            swap(list[jindex],swap[begin]);
        }
    }
}

删除实数前无意义的0:

//删除实数前面无意义的0:
int adjust(int num[],int length)
{
    int j = 1;
    while(num[0]==0)
    {
        do
        {
            num[j-1] = num[j];
            j++;
        }
        while(j<length);
        length--;
    }
}

链表反转问题:

//链表反转
linklist reverse(linklist head)
{
    linklist p,q,pr;
    p = head -> next;
    q = null;
    head->next = null;
    while(p){
        pr = p -> next;//把p-next赋值给pr
        p -> next = q;//把q作为p->next;
        q = p;//把p作为新的q
        p = pr;
    }
    head -> next = q;//反转后第一个节点q
    return head;
}

C++每行输入不同数量的数,并储存在二维向量中:

    int fir,sec;
    int eve_ip;
    cin>>fir;
    cin>>sec;
    vector<vector<int> > all_input(fir);
    for(int row = 0; row < fir; row++)
    {
        do
        {
            cin>>eve_ip;
            all_input[row].push_back(eve_ip);
        }
        while(cin.get()!='\n');
    }

猜你喜欢

转载自blog.csdn.net/LiuPeiP_VIPL/article/details/81674995
今日推荐