本文最后更新于 2024-04-24T21:52:22+08:00
七选五的C-Sharp版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 using RandomList.Core;namespace learn { public static class Constants { public static List<int > right = new List<int > { 1 , 2 , 3 , 4 , 5 }; public static RandomList<int > generate () { var randomList = new RandomList<int > { 1 , 2 , 3 , 4 , 5 , 6 , 7 }; randomList.Randomize(); return randomList; } public static List<int > RandomList_processed () { var temp = generate(); var sample = new List<int >(); for (int i = 0 ; i < 5 ; i++) { int element = temp[i]; sample.Add(element); } return sample; } } public class Compute { public void main_compute () { long n = 10000000 ; int b = 0 ; for (int i = 0 ; i < n; i++) { int a = 0 ; var standard = Constants.right; List<int > random_sample = Constants.RandomList_processed(); for (int j = 0 ; j < 5 ; j++) { int right = standard[j]; int test = random_sample[j]; if (right == test) { a++; } } if (a == 0 ) { b++; Console.WriteLine(b); } } double resuslt = (double )b / n; Console.WriteLine(resuslt); } } }
基础逻辑与之前的python版本没变,就翻译了一下。
学了一阵C#,最大的感受是C#很优雅,对数据的类型有严格的要求。各种编程语言的本质都一样,就是对数据的操作罢了。
numpy & 2048
numpy是python的一个用于数组操作的库
数组和矩阵差不多,或者是说矩阵就是一个数组
在numpy中,创建一个数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import numpy as np array_zero = np.zeros([4 ,4 ])print (array) 显示 [ [0 ,0 ,0 ,0 ] [0 ,0 ,0 ,0 ] [0 ,0 ,0 ,0 ] [0 ,0 ,0 ,0 ]] array_one = np.eye(4 ) print (array_one) 显示 [ [1 ,0 ,0 ,0 ] [0 ,1 ,0 ,0 ] [0 ,0 ,1 ,0 ] [0 ,0 ,0 ,1 ]] new_arr = np.array([[1 ,2 ],[2 ,4 ]]) print (new_arr) 显示 [[1 ,2 ] [2 ,4 ]]
有了数组,我们可以对它进行修改
1 2 3 4 5 6 7 8 9 10 11 12 array_one[row][column] = value array_one[:,column] = array_input array_one[row,:] = array_input1 harray = np.hsplit(array_one,4 ) varray = np.vsplit(array_one,4 ) integrated_arr = np.concatenate(one_arr,another_arr)
还可以读取数组中指定的元素,或者对元素进行操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 arr = array_one[row][column] zero_elements = arr[arr == 0 ].flatten() iter_array = arr.flatten() sorted_array = np.sort(iter_array) ''' (function) def sort( a: _ArrayLike[_SCT@sort], axis: SupportsIndex | None = ..., kind: _SortKind | None = ..., order: str | Sequence[str] | None = ... ) -> NDArray[_SCT@sort] Return a sorted copy of an array. '''
一些其他的操作
1 2 array = np.rot90(arr, -1 ) array = np.rot90(arr, 1 )
有了这些,就可以用numpy打造一个简单的2048了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 import numpy as npimport random as rdimport keyboardimport warningsimport osfrom time import sleep warnings.filterwarnings('ignore' ) global game_array game_array = np.zeros([4 ,4 ]) fixed_number = [2 ,4 ] random_list = [0 ,1 ]def basic_operate (array ): process_array = np.hsplit(array,4 ) collapse = np.zeros([4 ,4 ]) for i in [0 ,1 ,2 ,3 ]: arr = process_array[i] non_zero_elements = arr[arr != 0 ].flatten() zero_elements = arr[arr == 0 ].flatten() sorted_arr = np.concatenate((zero_elements, non_zero_elements)) sorted_arr = sorted_arr.reshape(arr.shape) sub = sorted_arr a = sub[0 ] == sub[1 ] b = sub[1 ] == sub[2 ] c = sub[2 ] == sub[3 ] d = sub[0 ] != sub[1 ] and sub[1 ] != sub[2 ] and sub[2 ] != sub [3 ] if a : if a and b and c : collapse[0 ][i] = 0 collapse[1 ][i] = 0 collapse[2 ][i] = sub[0 ] *2 collapse[3 ][i] = sub[2 ] *2 elif a and b : collapse[0 ][i] = 0 collapse[1 ][i] = sub[0 ] collapse[2 ][i] = sub[1 ] + sub[2 ] collapse[3 ][i] = sub[3 ] elif a and c : collapse[0 ][i] = 0 collapse[1 ][i] = 0 collapse[2 ][i] = sub[0 ] + sub[1 ] collapse[3 ][i] = sub[2 ] + sub[3 ] else : collapse[0 ][i] = 0 collapse[1 ][i] = sub[0 ] *2 collapse[2 ][i] = sub[2 ] collapse[3 ][i] = sub[3 ] if b : if b and c : collapse[0 ][i] = 0 collapse[1 ][i] = sub[0 ] collapse[2 ][i] = sub[1 ] collapse[3 ][i] = sub[2 ] *2 else : collapse[0 ][i] = 0 collapse[1 ][i] = sub[0 ] collapse[2 ][i] = sub[1 ] *2 collapse[3 ][i] = sub[3 ] if c : collapse[0 ][i] = 0 collapse[1 ][i] = sub[0 ] collapse[2 ][i] = sub[1 ] collapse[3 ][i] = sub[2 ] *2 if d: collapse[0 ][i] = sub[0 ] collapse[1 ][i] = sub[1 ] collapse[2 ][i] = sub[2 ] collapse[3 ][i] = sub[3 ] display_array = collapse.astype(int ) return display_array def down_operate (arr ): display_array = basic_operate(arr) return display_arraydef right_operate (arr ): array = np.rot90(arr, -1 ) a = basic_operate(array) display_array = np.rot90(a,1 ) return display_array def up_operate (arr ): array = np.rot90(arr, -2 ) a = basic_operate(array) display_array = np.rot90(a,2 ) return display_array def left_operate (arr ): array = np.rot90(arr, -3 ) a = basic_operate(array) display_array = np.rot90(a,3 ) return display_array def array_operate (n ): for i in range (n): postions_row = rd.randint(0 ,3 ) postions_column = rd.randint(0 ,3 ) value = rd.choice(fixed_number) game_array[postions_row][postions_column] = valuedef check_avilable_position (): global avilable_positions_array avilable_positions_array = np.zeros([4 ,4 ]) avilable_positions_array = np.argwhere(game_array==0 ) def from_available_position_generate (): check_avilable_position() global position shape = np.shape(avilable_positions_array) row_number = shape[0 ] - 1 try : position = rd.randint(0 ,row_number) random_position_array = avilable_positions_array[position,:] value = rd.choice(fixed_number) game_array[random_position_array[0 ]][random_position_array[1 ]] = value v = False return v except ValueError or IndexError: v = True return v def clear_terminal (): os.system('cls' if os.name == 'nt' else 'clear' )def update_terminal (v ): clear_terminal() print ("----------------" ) for i, row in enumerate (game_array): for j, value in enumerate (row): if j < len (row) - 1 : print (f" {get_colored_char(value)} " , end="" ) else : print (f" {get_colored_char(value)} " ) print ("----------------" ) if v : print ("no space warning ! maybe you have died,press q to exit " ) else : pass def get_colored_char (value ): color = get_color(value) if value != 0 : return f"\033[38;5;{color} m{value:2 } \033[0m" else : return " " def get_color (value ): if value == 2 : return 214 elif value == 4 : return 220 elif value == 8 : return 226 elif value == 16 : return 202 elif value == 32 : return 196 elif value == 64 : return 160 elif value == 128 : return 126 elif value == 256 : return 82 elif value == 512 : return 46 elif value == 1024 : return 27 elif value == 2048 : return 21 else : return 15 def main (): array_operate(2 ) global game_array t = False update_terminal(t) condition = True while condition: if keyboard.is_pressed("down" ): game_array = down_operate(game_array) v = from_available_position_generate() update_terminal(v) sleep(0.43 ) elif keyboard.is_pressed("up" ): game_array = up_operate(game_array) v = from_available_position_generate() update_terminal(v) sleep(0.43 ) elif keyboard.is_pressed("left" ): game_array = left_operate(game_array) v = from_available_position_generate() update_terminal(v) sleep(0.43 ) elif keyboard.is_pressed("right" ): game_array = right_operate(game_array) v = from_available_position_generate() update_terminal(v) sleep(0.43 ) elif keyboard.is_pressed("q" ): print ('Good game!' ) condition = False main()
游戏的实现写的很粗糙,但终归还是能跑
七选五全错的概率
七选五要求在7个备选答案中选出5个。
如果一个人随机选择,那么他一个都没撞对的概率是多少?
这是一个错位排列的问题,用数学方法计算十分繁琐,要求要对排列组合有很深的理解
用随机模拟的方法解决要简单一些
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import random right_answer = [1 ,2 ,3 ,4 ,5 ] n = 100000 b = 0 for i in range (1 ,n+1 ): random_list = random.sample(range (1 ,8 ),5 ) print (i/n) a = 0 for t in range (0 ,5 ): if random_list[t] == right_answer[t]: a += 1 else : pass if a == 0 : b += 1 result = b/nprint (result)
这样就很快的算完了,不论是几选几,我们都能快速得到答案
求数列极限
这道题来自于一次考试,常规做法是两边夹逼
求 lim n → ∞ n n 3 + 1 + 2 n n 3 + 2 2 + 3 n n 3 + 3 2 + . . . + n 2 n 3 + n 2 求\lim_{n \to \infty} \dfrac{n}{n^3+1}+\dfrac{2n}{n^3+2^2}+\dfrac{3n}{n^3+3^2}+...+\dfrac{n^2}{n^3+n^2}
求 n → ∞ lim n 3 + 1 n + n 3 + 2 2 2 n + n 3 + 3 2 3 n + ... + n 3 + n 2 n 2
观察到每一项都可以写成
n x n 3 + x 2 \dfrac{nx}{n^3+x^2}
n 3 + x 2 n x
那就可以硬算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import math n = 100 init_value = 0.0 def Xn (x ): return (x*n)/(n**3 +x**2 ) sequences = (Xn(i) for i in range (1 ,n+1 ) ) limit = math.fsum(sequences)print (limit)
学习通脚本
写了一个python脚本用于打开学习通网页,配合油猴脚本可以实现自动刷网课的功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 """需要安装selenium模块及webdriver驱动""" from time import sleepfrom selenium import webdriver options = webdriver.ChromeOptions() options.add_argument( r"--user-data-dir=C:\Users\1\AppData\Local\Google\Chrome\User Data\Default" ) options.add_experimental_option("detach" , True ) browser = webdriver.Chrome(options=options) browser.maximize_window() sleep(1 ) url = "exampleURL" browser.get(url)
当然这段代码只能打开单个网址,没啥用
观察到脚本是用js写的,代码行数达到了惊人的13600行,附上脚本网址
https://docs.ocsjs.com/