标签:笔试题
#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 i = 0;
int count = 0;
for(;i<n; i++)
{
if (count < 0)
{
count = a[i];
}
else
{
count += a[i];
}
}
return count;
}
int main()
{
int a[] = {-1,5,-4,7};
cout << Grial(a, 4) << 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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:笔试题
原文地址:http://blog.csdn.net/liuhuiyan_2014/article/details/46774761