您的位置:首页 > 编程语言

奇虎360 2015校园招聘笔试编程题

2017-08-23 20:47 330 查看
题目一:

写一个函数,根据两文件的绝对路径算出相对路径。
如a="/qihoo/app/a/b/c/d/new.c",b="/qihoo/app/1/2/test.c",
那么b相对于a的相对路径是"../../../../1/2/test.c"


#include <stdio.h>
#include <string.h>

int isGoodPath( char *path )
{
if ( path == NULL || *path != '/' )
return 0;
return 1;
}

//the relative path of b
void parseRelativePath( char *patha, char *pathb, char *result )
{
if ( !isGoodPath( patha ) || !isGoodPath( pathb ) )
return ;
int diff, i = 0;
int tempdiff;
int dirDiff = 0;
//找到路径中最后一个相同的目录
while ( *(patha+i) == *(pathb+i) )
{
if ( *(patha+i) == '/' )
{
diff = i;
}
i++;
}
diff++;
tempdiff = diff;
//计算出..的个数
while ( *(patha+diff) != '\0' )
{
if ( *(patha+diff) == '/' )
dirDiff++;
diff++;
}
//拼接出结果
while ( dirDiff != 0 )
{
strcat( result, "../" );
dirDiff--;
}
strcat( result, pathb+tempdiff );
}

int main( void )
{
char *patha = "/qihoo/app/a/b/c/d/new.c";
char *pathb = "/1/2/test.c";

char result[1024];

parseRelativePath( patha, pathb, result );

printf( "%s\n", result );
return 0;
}


题目二:

有一个二维float型矩阵,有m行n列,每一个行和列上都是递减序列,
请编程实现在这个矩阵中查找值等于v的元素的函数,如果找到返回其下标


分析:

1、浮点数比较大小。

2、必须以右上角或者左下角为起点。

#include <stdio.h>
#include <math.h>
#include <unistd.h>

//row -> m, column -> n
int findFromMatrix( float target, float *matrix, int m, int n )
{
const float REALZERO = 10.0e-6;
const float PREALZERO = -1 * 10.0e-6;
for ( int i = 0, j = n-1; j >= 0 && i < m;  )
{
printf( "i : %d\n", i );
printf( "j : %d\n", j );
sleep(1);
if ( fabs(matrix[i*m+j] - target) < REALZERO )
{
return i*m+j;
}
else if ( matrix[i*m+j] - target < PREALZERO )
{
i++;
}
else
{
j--;
}
}
return -1;
}

int main( void )
{
float matrix[] = { 1.0, 2.0, 8.0, 9.0, 2.0, 4.0, 9.0, 12.0, 4.0, 7.0, 10.0, 13.0, 6.0, 8.0, 11.0, 15.0 };
float target = 4.0;
int index = findFromMatrix( target, matrix, 4, 4 );
printf( "%d\n", index );
return 0;
}


题目三:

字符串A和B的最长公共单词


这道题不清楚题意是求最长公共子序列还是将字符串分隔为单词,然后求两个字符串中共有的字符串里面最长的。

我将两种情况下的思路和代码都写下来。

1、求字符串A和B的最长公共子序列。

这是个经典题目,要使用到动态规划。

关于动态规划,参考这里:动态规划

2、将字符串分隔为单词,然后求两个字符串中共有的字符串里面最长的。

这种解决方法,我能想到的思路就是将每个句子中的单词分析出来保存到一个数组中,然后将这两个数组中的单词进行比较,这样的时间复杂度能达到O(m*n),时间效率非常低,还没有遇到什么有效地解法,由于解法很直观就不用关注代码了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  校园招聘 奇虎360