Intel面试题(原创)

去英特尔面试,被无声无息了。据说是headcount取消了,呜呜。

发出上机题,供广大程序兄弟参考。

题目(上机,一个小时时间):
输入一串数字,如:3 3 1 2 3 4 5 6 7 8 9
第一个数字表示行数,第二个表示列数,后面的表示矩阵里的值。生成的矩阵如下:
1 2 3
4 5 6
7 8 9

 

然后按顺时针方向螺旋输出,在上面的例子中,顺序是:1 2 3 6 9 8 7 4 5

要求输入输出如下:
Input:
3 3 1 2 3 4 5 6 7 8 9
1 3 1 2 3
Output:
1 2 3 6 9 8 7 4 5
1 2 3

 

说实话比较简单,也没规定用什么语言,我用python写的,45分钟的样子:

 

'''
Created on 2010-6-21

@author: maodouzi
'''
import os, sys

def buildMatrix (inputList):
#    print rowNum, colNum
    inputListTrim = inputList[2:]
#    print inputListTrim
    inputMatrix = []
    tmpCount = 0
    
    for tmpRowNum in range(rowNum):
        tmpList = []
        for tmpColNum in range(colNum):
#            print tmpRowNum, tmpColNum, tmpCount
            tmpList.append(inputListTrim[tmpCount])
            tmpCount += 1
        inputMatrix.append(tmpList)
    return inputMatrix

def outputMatrix (inputMatrix):
    inputMatrixBuf = inputMatrix
    outputList = []
    while (len(inputMatrixBuf) > 0):
        tmpList = []
        tmpList = [tmpStr for tmpStr in inputMatrixBuf[0]]
        inputMatrixBuf = inputMatrixBuf[1:]
        outputList += tmpList
    
        if (len(inputMatrixBuf) == 0):
            break
        tmpList = []
        for tmpCount in range(len(inputMatrixBuf)):
            tmpList.append(inputMatrixBuf[tmpCount].pop())
        outputList += tmpList
    
        if (len(inputMatrixBuf) == 0):
            break
        tmpList = []
        tmpListBuf = []
        tmpListBuf = reversed(inputMatrixBuf[-1])
        tmpList = [tmpStr for tmpStr in tmpListBuf]
        inputMatrixBuf = inputMatrixBuf[:-1]
        outputList += tmpList
        
        if (len(inputMatrixBuf) == 0):
            break
        tmpList = []
        for tmpCount in range((len(inputMatrixBuf) - 1), -1, -1):
            tmpList.append(inputMatrixBuf[tmpCount][0])
            inputMatrixBuf[tmpCount] = inputMatrixBuf[tmpCount][1:]
        outputList += tmpList
    
#    print inputMatrixBuf
    return outputList

inputListStr = raw_input("Input:\n")
inputList = inputListStr.split()
#print inputList

if (len(inputList) < 2):
    sys.exit("Could not got row & col numbers!")

rowNum = int(inputList[0])
colNum = int(inputList[1])

if ((rowNum * colNum) != (len(inputList) - 2)):
    sys.exit("Wrong Arguments Number!")
    
inputMatrix = buildMatrix(inputList)
#print inputMatrix
outputList = outputMatrix(inputMatrix)

print "output:"
print " ".join(outputList)

posted @ 2010-07-22 11:00  毛豆子  阅读(1314)  评论(3)    收藏  举报