20180128
1.计算一个四乘四矩阵的所有元素的和,以及对角线之和
[
[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]
]
#encoding=utf-8
a=[
[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]
]
#反对角线的规律
#03
#12
#21
#30
#矩阵的反对角线之和
count=0
for i in a:
print i
for j in i:
count+=j
print count
#矩阵的正对角线之和
count=0
for i in range(len(a)):
count+=a[i][i]
print count
#矩阵所有元素之和
count=0
for i in range(len(a)):
count+=a[i][len(a)-1-i]
print count
2.输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
a=[]
while 1:
try:
user_input = raw_input("pleaser input a number:")
if user_input.strip() =="over":
break
else:
a.append(int(user_input))
except:
print "error ,try again!"
max_num = max(a)
min_num = min(a)
max_num_index = a.index(max_num)
min_num_index = a.index(min_num)
a[0],a[max_num_index]=a[max_num_index],a[0]
a[-1],a[min_num_index]=a[min_num_index],a[-1]
print a
3.有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
a=[1,2,3,4,5,6,7]
m=3
last_m_numbers=a[-m:]
front_numbers=a[:-1-m+1]
print last_m_numbers+front_numbers