下午参加了某知名办公软件的笔试。感觉选择题很简单,下面是算法大题:
题目1. 按从外到里,顺时针方向打印矩阵
如 1 2 3 4
5 6 7 8
9 10 11 12
打印:1,2,3,4,8,12,11,10,9,5,6,7
----这道题是剑指offer里面的原题啊。
// 顺时针打印矩阵.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
#define MAX 100
int visit[MAX][MAX] = {0};
void PrintClocklyMatrix(int a[][4], int col, int row)
{
if (a == NULL || col <= 0 || row <= 0)
return;
int count = 0;
int x = 0;
int y = 0;
while(count < col * row)
{
while (visit[y][x] == 0 && x < col) //向右
{
count ++;
visit[y][x] = 1;
cout << a[y][x++] << ",";
}
x--;
y++;
while (visit[y][x] == 0 && y < row) //向下
{
count ++;
visit[y][x] = 1;
cout << a[y++][x] << ",";
}
y--;
x--;
while (visit[y][x] == 0 && x >= 0) //向左
{
count ++;
visit[y][x] = 1;
cout << a[y][x--] << ",";
}
x++;
y--;
while (visit[y][x] == 0 && y >=0) //向上
{
count ++;
visit[y][x] = 1;
cout << a[y--][x] << ",";
}
y++;
x++;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[4][4] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
PrintClocklyMatrix(a, 4, 4);
return 0;
}
题目2: 输出字符串数组中的重复行
(1)关于字符串数组的存储问题
int _tmain(int argc, _TCHAR* argv[])
{
char* a[]= {"abcdef","xyz", "rstv"}; //一维字符串数组定义
printf("%d\n", sizeof(a)); //3*4 = 12
printf("%x\n",a[0]);
printf("%x\n",a[1]);
printf("%x\n",a[2]);
printf("%c\n",a[0][5]);
printf("%c\n",a[1][0]);
printf("%c\n",a[2][1]);
printf("*************************\n");
char* b[][3] = {"gkj","lmn","opq","1235"}; //二维字符数组定义
printf("%d\n", sizeof(b)); //6*4 = 24
//b[0] 指向第一行
//b[1] 指向第二行
printf("%s\n", b[0][0]);
printf("%c\n", b[0][0][0]);
printf("%s\n", b[1][0]);
printf("%c\n", b[1][0][1]);
return 0;
}
一维字符串数组存储反汇编代码:
char* a[]= {"abcdef","xyz", "rstv"}; //一维字符串数组定义
00FD35EE mov dword ptr [a],offset string "abcdef" (0FD57F0h)
00FD35F5 mov dword ptr [ebp-0Ch],offset string "xyz" (0FD5788h)
00FD35FC mov dword ptr [ebp-8],offset string "rstv" (0FD5780h)
可以看出,本质上是数组中,存储了字符串的地址。
char* b[][3] = {"gkj","lmn","opq","1235"}; //二维字符数组定义
00FD36E0 mov dword ptr [b],offset string "gkj" (0FD5750h)
00FD36E7 mov dword ptr [ebp-2Ch],offset string "lmn" (0FD574Ch)
00FD36EE mov dword ptr [ebp-28h],offset string "opq" (0FD5748h)
00FD36F5 mov dword ptr [ebp-24h],offset string "1235" (0FD5740h)
00FD36FC xor eax,eax
00FD36FE mov dword ptr [ebp-20h],eax
00FD3701 mov dword ptr [ebp-1Ch],eax
二维字符数组和一维字符串数组一样,同样存储的是字符串数组的地址。
b[0]指向数组第一行
printf("%s\n", *(*b+1)); //*(*b+1) --> b[0][1]
b[1]指向数组第二行
printf("%s\n", *(*(b+1)+1)); //*(*(b+1)+1) --> b[1][1]
本题算法实现:
#include <string.h>
#define COL 4
void getRepeatRow(char *p[][COL], int row, int col)
{
int ii = 0;
int jj = 0;
for(int i = 0; i < row; i++)
{
for (int j = i+1; j < row; j++)
{
int k = 0;
int count = 0;
while (k < col)
{
if(strcmp(p[i][k], p[j][k]) == 0)
{
count ++;
ii = i;
jj = j;
}
else
break;
k++;
}
if (count == col-1)
{
printf("%d,%d\n", ii+1, jj+1);
}
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char* a[][COL] = { "abc","wps","ios", "ip",
"hu","wps","office","ip",
"abc","wps","ios","pa",
"hu","wps","office", "ip",
"abc","wps","ios","ip",
"abc","wps", "addr","ip"
};
getRepeatRow(a, 6, 4);
return 0;
}
题目3:实现一个动态数组类myArray,当内存不够的时候,不能重新分配,而是在原来的基础上按n字节分配,
同时实现insert和erase函数
--------本质是希望我们实现STL中的vector。一道非常经典的题目啊。
题目4:走迷宫的题目。
-------记得10月9号的时候参加**秀秀的时候,最后一道题也是类似的走迷宫题。哎,一声长叹,没有好好去小结,笔试题太相似了。