C++笔试题第三波

#include <iostream>
using namespace std;
static int gflags = 0;
//杨氏矩阵的查找。
int FindVal(int (*a)[8],int x,int y,int val)
{
    int i = 0;
    int j = y - 1;
    while (i <= 5 && j >= 0)
    {
        if (a[i][j] > val)
            j--;
        else if (a[i][j] < val)
            i++;
        else
        {
            return a[i][j];
        }
    }
    gflags = 1;
    return -1;
}
int main()
{
    int a[][8] = {1,2,3,4,5,6,7,8,
               2,3,4,5,6,7,8,9,
               3,4,5,6,7,8,9,10,
               4,5,6,7,8,9,10,11,
               12,13,14,15,16,17,18,19};
    cout<<FindVal(a,5,8,12)<<endl;
    return 0;
}



#include<iostream>
#include <assert.h>
#include <string.h>
using namespace std;
//找出字符串中第一次出现一次的那个字母。
char Grial(char *str)
{
    assert(str!=NULL);
    char *p = str;
    int len = strlen(str);
    int i = 0;
    char save[256];//用字符数组来表示。
    memset(save,'0',256);
    for (; i < len; i++)
    {
        save[str[i]]++;
    }
    while (*p != '\0')
    {
        if (save[*p] == '1')
            return *p;
        p++;
    }
}
int main()
{
    char *s1 = "112233455667788";
    cout << Grial(s1)<< endl;
    return 0;
}




#include <iostream>
using namespace std;
//求数组中的最大子数组之和。
int Grial(int a[], int n)
{
    int count = 0;
    int max = 0;
    for (int i = 0; i < n; i++)
    {
        if (count>0)
        {
            count += a[i];
        }
        else
        {
            count = a[i];
        }
        if (max < count)
        {
            max = count;
        }
    }
    return max;
}

int main()
{
    int a[] = { 2, 2, 3, -3, -6, 4, 5, -1, 8 };
    cout << Grial(a, sizeof(a)/sizeof(int)) << endl;
    return 0;
}

//4.旋转数组的最小数字
//题目:
//把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
//输入一个递增排序的数组的一个旋转,
//输出旋转数组中的最小元素。
//例如:数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,最小元素是1。
#include <iostream>
using namespace std;
static int g_flags = 0;
int Grial(int a[], int n)
{
    //二分查找。
    if (a[0] < a[n - 1])return a[0];
    int i = 0;
    int j = n - 1;
    int mid;
    while (i < j)
    {
        mid = (j + i) / 2;
        if (a[mid]>a[i])
        {
            i = mid+1;
        }
        else if (a[mid]<a[i])
        {
            if (mid - 1 == i)return a[mid];
            j = mid-1;
        }
        else
        {
            int k = i;
            for (; k < j; k++)
            {
                if (a[k]>a[k + 1])return a[k+1];
            }
            return a[k];
        }
    }
    return a[mid+1];

}
int main()
{
    //int a[] = { 2,2,3,4,1,2 };
    //int a[] = { 1, 1, 1, 1, 1, 1 };
    //int a[] = {1,1,1,1,0,1};
    int a[] = { 3, 4, 5, 6, 7, 2 };
    cout << Grial(a,6) << endl;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值