總共3道編程題,我還是考這種題考少了,機考的時候有點懵,考完回頭看才發現自己太菜了哈哈哈,不多說上代碼(回來之后重新做的)
-
第1題(還沒寫完???)
https://blog.nowcoder.net/n/d906173636514377a3438623ae3bf7a2
給定一個僅包含0和1的N*N二維矩陣,請計算二維矩陣的最大值,計算規則如下:
每行元素按下標順序組成一個二進制數(下標越大越排在低位),二進制數的值就是該行的值。矩陣各行值之和為矩陣的值。
允許通過向左或向右整體循環移動每行元素來改變各元素在行中的位置。 比如:
[1,0,1,1,1]向右整體循環移動2位變為[1,1,1,0,1],二進制數為11101,值為29。
[1,0,1,1,1]向左整體循環移動2位變為[1,1,1,1,0],二進制數為11110,值為30。
輸入描述:
輸入的第一行為正整數,記錄了N的大小,0 < N <= 20。
輸入的第2到N+1行為二維矩陣信息,行內元素半角逗號分隔。
輸出描述:
矩陣的最大值
class Solution: def get_sum(self, n, list_num): sum1 = 0 for idx, value in enumerate(list_num): if value == 0: sum1 += 0 else: sum1 += value * 2 ** (n - 1 - idx) return sum1 def sum_list1(self, nums): n = int(nums[0]) sum_max = 0 nums[:] = nums[1:] for str in nums: list_num = [int(s) for s in str.split(",")] max_num = self.get_sum(n, list_num) for i in range(1, n): # 每次左移動一位 list_temp = list_num[i:]+list_num[:i] # 獲取移位后的和 temp = self.get_sum(n, list_temp) # 判斷最大值 max_num = max(max_num, temp) # print(list_num, max_num) # 計算每行矩陣和 sum_max += max_num return sum_max
以上是比較笨的辦法,有一個更好的 https://blog.csdn.net/cxh21627/article/details/125002305
def sum_list2(self, nums): n = int(nums[0]) nums[:] = nums[1:] sum_max = 0 for num in nums: temp = num.replace(",", "") max_num = int(temp, 2) for i in range(1, n): max_num = max(max_num, int(temp[i:]+temp[:i], 2)) sum_max += max_num return sum_max
- 第2題
https://leetcode-cn.com/circle/discuss/fhOTlt/
某探險隊負責對地下洞穴進行探險。探險隊成員在進行探險任務時,隨身攜帶的記錄器會不定期地記錄自身的坐標,但在記錄的間隙中也會記錄其他數據。探索工作結束后,探險隊需要獲取到某成員在探險過程中相對於探險隊總部的最遠的足跡位置。
儀器記錄坐標時,坐標的數據格式為(x,y),如(1,2)、(100,200),其中0<x<1000,0<y<1000。同時存在非法坐標,如(01,1)、(1,01),(0,100)屬於非法坐標。
設定探險隊總部的坐標為(0,0),某位置相對總部的距離為:xx+yy。
若兩個座標的相對總部的距離相同,則第一次到達的坐標為最遠的足跡。
若記錄儀中的坐標都不合法,輸出總部坐標(0,0)。
備注:不需要考慮雙層括號嵌套的情況,比如sfsdfsd((1,2))。
輸入描述:
字符串,表示記錄儀中的數據。
如:ferga13fdsf3(100,200)f2r3rfasf(300,400)
輸出描述:
字符串,表示最遠足跡到達的坐標。
如: (300,400)
示例1
輸入
ferg(3,10)a13fdsf3(3,4)f2r3rfasf(5,10)
輸出
(5,10)
說明
記錄儀中的合法坐標有3個: (3,10), (3,4), (5,10),其中(5,10)是相距總部最遠的坐標, 輸出(5,10)。
示例2
輸入
asfefaweawfaw(0,1)fe
輸出
(0,0)
說明:記錄儀中的坐標都不合法,輸出總部坐標(0,0)
def func2(info): length = len(info) max_index = "(0,0)" i = 0 while i < length: max_dis = 0 # 一對對 查找()位置 l_index = info.find("(", i) r_index = info.find(")", i) i = r_index + 1 if l_index >= 0 and r_index >= 0: temp = info[l_index + 1:r_index].split(",") else: break if temp[0][0:1:] != '0' and temp[1][0:1:] != '0': x = int(temp[0]) y = int(temp[1]) if 0 < x < 1000 and 0 < y < 1000 and (x ** 2 + y ** 2) > max_dis: max_dis = (x ** 2 + y ** 2) max_index = "(" + temp[0] + "," + temp[1] + ")" return max_index
-
第3題
https://blog.csdn.net/Angel_Emma/article/details/100546831
假設有一組非負整數,對數據元素重新排列,使其構成的整數最大,輸出該整數。如輸入11,20,3,7.輸出732011
我的機考題是輸入“90 12 123 78” 這種
def func3(mock_input): nums = mock_input.split(" ") length = len(nums) # 將所有nums[i]轉換為int # nums = list(map(int, nums)) for i in range(length): for j in range(i + 1, length): if nums[i] + nums[j] < nums[j] + nums[i]: temp = nums[i] nums[i] = nums[j] nums[j] = temp
#針對 0 0情況,應該輸出0
result = str(int("".join(nums)))
return result