算法学习(十五)

1.Share Price Volatility

说明:选择股价波动更大的股票,例如,如果股票的初始价格是50英镑(而不是20英镑),而当保罗卖出股票时,他的股价已经上涨到52英镑,他的200股股票的利润只有400英镑。

然而,在买入和卖出时,他的收益是100英镑,所以他真正的收益只有96英镑——超过一半的钱是被经纪人赚的!经纪人的佣金为每笔交易的1%。

保罗的选股规则:如果佣金为 0.5,那么股票标准偏差应等于或大于2时,保罗才会选择这支股票。

例如,如果价格在7天内为99英镑,而在其他7天内为101美元,那么平均价格为100英镑,而经纪人的佣金(每股)为1英镑。标准差也会是1,所以这些股票不值得被买卖。

输入数据:包含用于计算的股票数量(股票类型或名称)。

下面各行包含股票的描述——股票名称(四个拉丁字母),然后是14个值——在过去的两周内股票每天的价格。

答案:应该包含一些股票的名字,这些股票的波动性足够大,即符合保罗的标准(和它们在输入中给出的顺序相同)。

例如:

input data:
2
JOOG 99 99 99 99 99 99 99 101 101 101 101 101 101 101
GOLD 95 105 95 105 95 105 95 105 95 105 95 105 95 105

answer:
GOLD

测试数据:

23
FOTA 20 21 21 21 21 22 23 22 21 20 20 21 21 22
PNSN 26 27 28 27 28 26 25 26 24 23 21 20 22 20
VDKL 57 54 54 56 55 57 61 64 65 66 66 65 68 67
ZEOD 23 22 21 20 21 20 20 21 21 21 20 19 18 18
JABA 21 23 26 28 28 28 27 23 27 24 23 24 26 26
ASDF 157 160 163 163 166 164 168 171 170 166 165 164 163 164
FLNT 23 24 28 25 27 30 29 26 26 23 24 20 17 15
YUKA 33 34 33 32 32 33 33 35 33 35 35 33 35 33
DALG 79 78 80 81 81 80 82 81 80 82 82 81 83 83
GOLD 217 216 216 217 216 216 216 217 218 218 219 220 219 220
JOOG 56 53 56 58 56 54 55 58 59 59 58 57 55 53
CKCL 68 69 69 71 71 70 69 68 66 66 64 63 61 62
WOWY 24 24 27 26 25 24 28 30 33 30 34 33 35 37
SLVR 26 28 27 28 27 27 26 25 26 26 24 25 26 27
JEOP 23 23 26 29 26 27 23 20 16 18 21 23 19 23
BLEP 81 79 81 79 80 79 81 82 79 77 75 76 76 78
SUGR 24 23 21 21 20 19 18 16 18 17 15 13 12 11
MYTH 25 24 24 25 26 26 25 24 23 24 25 26 27 26
FANT 116 114 115 119 119 119 117 118 118 119 123 122 119 115
OBAM 24 21 18 20 17 16 16 18 16 14 11 11 9 9
SAKK 24 26 26 26 26 24 22 22 22 21 21 19 19 17
INSX 26 26 26 28 27 28 26 25 23 21 22 22 23 24
IMIX 42 40 40 39 39 41 42 40 38 36 34 36 37 35

代码如下:

 1 from math import sqrt
 2 test_cases = int(input())
 3 
 4 def Standard_Deviation(attr):  # 构造标准差
 5     M = sum(attr) / len(attr)
 6     D = [(M - i)**2 for i in attr]
 7     d = sqrt(sum(D) / len(D))
 8     return d
 9 
10 for _ in range(test_cases):
11     data = [i for i in input().split()]
12     name = data[0]
13     values = [int(i) for i in data[1:]]
14     comission= (sum(values) / len(values)) * 0.01 # 佣金 
15     standred = Standard_Deviation(values)
16     if standred / comission >= 4:  # 标准差是佣金的4倍以上时
17         print(name, end=' ')
18 
19 输出:PNSN VDKL ZEOD JABA FLNT CKCL WOWY SLVR JEOP SUGR MYTH OBAM SAKK INSX IMIX

2.Insertion Sort(插入排序)

说明:非常常见的算法,要求计算数组中每个数用插入法排序时,移动的步数,即多少位元素被移位。

例如:3 1 2 5

[3], 1, 2, 5            T = 1, it should be inserted before 3
[3, 3], 2, 5            part of array starting from 3 is shifted, 1 is overwritten
[1, 3], 2, 5            1 is restored from T to the proper place

[1, 3], 2, 5            T = 2, it should be inserted before 3
[1, 3, 3], 5            part of array starting from 3 is shifted, 2 is overwritten
[1, 2, 3], 5            2 is inserted from T to sub-array

[1, 2, 3], 5            T = 5, it should be inserted... after all other elements, nice!
[1, 2, 3, 5]            nothing to shift! we just extend sub-array to have 5 in it.

所以结果:

input data:
4
3 1 2 5

answer:
1 1 0

问题叙述:

您得到了一组要排序的值。实现所描述的算法,并在每次传递时打印出数组中有多少元素被移位。

输入数据:第一行中包含的数组大小。

第二行包含数组本身(所有元素都是不同的)。

答案:应该包含移动的步数,即显示每个传递中有多少元素被移动。

测试数据:

133
126 163 189 177 74 30 185 155 102 66 10 179 34 137 123 49 3 65 112 45 190 5 78 119 92 136 118 101 64 82 91 41 156 120 146 106 159 48 172 169 27 7 150 55 108 15 167 153 6 171 32 124 168 62 181 69 125 160 166 20 81 186 131 14 100 165 114 68 12 157 99 21 67 197 96 89 87 61 53 164 191 107 88 98 104 94 79 115 51 178 72 75 180 141 77 29 54 121 18 113 147 140 152 57 193 23 16 71 28 196 43 73 103 127 25 144 26 85 142 80 44 33 50 158 83 139 154 135 111 9 109 187 93

代码如下:

 1 test_case = int(input())
 2 Arrays = [int(i) for i in input().split()]
 3 
 4 def insertion_sort(A):  # 构造插入排序法
 5     M = []   # 一个列表
 6     for j in range(1, len(A)):
 7         key = A[j]
 8         i = j - 1
 9         m = 0
10         while i >= 0 and A[i] > key:
11             A[i+1] = A[i]
12             i = i - 1
13             m += 1   # 计算总共移了多少步,即转移了多少元素
14         M.append(m)  # 添加到列表中
15         A[i + 1] = key
16     return M  # 返回所有移位的列表
17 
18 for i in insertion_sort(Arrays):
19     print(i, end=' ')
20 
21 
22 输出:0 0 1 4 5 1 4 6 8 10 2 10 6 8 12 16 12 9 15 0 20 12 10 13 8 12 15 21 17 17 26 6 12 8 17 6 30 5 6 37 39 11 32 21 41 7 12 46 6 42 19 8 38 3 36 21 12 11 53 37 2 23 58 36 13 32 46 63 17 40 62 49 0 42 45 46 58 60 15 1 39 49 45 41 48 56 36 70 8 61 60 7 28 62 84 75 36 90 42 27 30 26 79 1 94 98 72 94 1 90 72 53 37 101 32 102 68 33 72 97 101 94 24 71 37 28 41 56 125 57 6 71
原文地址:https://www.cnblogs.com/zt19994/p/7486532.html