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)

转载于:https://www.cnblogs.com/maodouzi/archive/2010/07/22/1782848.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值