編程題總結
題目一:輸出無重復的3位數
題目描述
從{1,2,3,4,5,6,7,8,9}中隨機挑選不重復的5個數字作為輸入數組‘selectedDigits’,能組成多少個互不相同且無重復數字的3位數?請編寫程》序,從小到大順序,以數組形式輸出這些3位數
輸入描述:
1 2 3 4 5
輸出描述
123 124 125 132 134 135 142 143 145 152 153 154 213 214 215 231 234 235 241 243 245 251 253 254 312 314 315 321 324 325 341 342 345 351 352 354 412 413 415 421
423 425 431 432 435 451 452 453 512 513 514 521 523 524 531 532 534 541 542 543
核心代碼
題目二:計算無人機飛行坐標
題目描述
編寫一個程序,模擬無人機的飛行路徑。給定一個包含指令的字符串(例如:“RUDDLLUR”),每個指令代表無人機在二維平面上移動的方向 (U: 前、D:后、L:左、R:右),請計算無人機的最終坐標并輸出。
輸入描述:
RUDDLLUR
輸出描述
0 0
核心代碼
def calculate_final_coordinate(instructions):x = 0 y = 0 for instruction in instructions:if instruction == 'U':y += 1 elif instruction == 'D':y -= 1 elif instruction == 'L':x -= 1 elif instruction == 'R':x += 1 return x, yinstructions = input()
final_coordinate = calculate_final_coordinate(instructions)
print(final_coordinate[0],final_coordinate[1])