面试题/笔试题

题目1:求整数反序

(阿里网上面试编程题)

1
2
3
4
5
6
7
8
input:123
output:321

input:-580
output:-85

input:65
output:56

解答:

1
2
3
4
5
6
7
8
9
10
11
12
def reverse(a):
x = abs(a) # 求绝对值
result=0
while (x!=0):
result = result*10+ int(x%10)
x = int(x/10)

# 将正负号还原
if (a<0):
result = -result

return result

题目2:

(阿里网上编程题)
在N*N的方格中,站在原点的人能看到几个点?

1
2
3
4
5
input:3
output:5

input:4
output:9

解答:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import numpy as np
def f(N):
N = int(N)
m = [] #用来存放所有点的方向
for i in range(1,N):
for j in range(1,N):
v = i/j #计算每个点对原点的方向
m.append(v)
m=np.array(m[1:])

m_unique = [] #存放不重复点的方向
for i in m:
if i not in m_unique:
m_unique.append(i)

return len(m_unique)+2 #输出