python 解决我的简单线性搜索算法问题.它只是简单的算法问题

eh57zj3b  于 2023-01-04  发布在  Python
关注(0)|答案(2)|浏览(114)

我可以得到输入,但之后我想不出好办法,我觉得还是把每个值都保存为双精度表,通过播放来使用处理for语句的方法比较好,但那样的话,你要把for门转3次,有没有更简单的方法,你能用Python解决这个问题,请帮帮我。
这是我的问题

**The question
**Oh-seok wants to buy a laptop. There are n kinds of laptops on display in the store.
Oh-seok is interested in cpu speed, RAM, and the size of hard disk. (FYI, I am not interested in specification of anything else.)

Oh-seok has a rule to buy a laptop.
Rule 1) If the specifications of one laptop are all lower than those of another laptop, the laptop is not bought because it is out of fashion.
Rule 2) Choose the cheapest laptop that is judged to be out of fashion.

There are so many laptops that Oh-seok tries to find a laptop that fits his condition by creating a program with coding.

Print out the number of the laptop you want to buy with the program Oh-seok made.
Input type
The first line is given the number of laptops (1≤n≤100)
From the second line, n lines are given the specifications of the laptop. Each line is given the laptop's specifications in order of cpu speed, RAM size, hard disk size, and price.
• 1,000≤cpu speed≤4,200
• 256≤Ram size≤4,096
• 1≤Hard disk size≤500
• 100≤Price≤1,000 (all laptops are priced differently).)

Output type
Print out the number of the laptop that Oseok will buy. The number of the laptop means the order of each line in which a given laptop specification is written in the input data.

Input/output example data
Input Example 1
5
2100 512 150 200
2000 2048 240 350
2300 1024 200 320
2500 2048 80 300
2000 512 180 150

'

N = int(input())
information = [[] * (N) for _ in range(N)] # laptop's information. 

#input (cpu , ram , harddisk, price)
for _ in range(N):
    tmp = list(map(int,input().split()))
    for i in range(4):
        information[i].append(tmp[i])
print(information)

'

This is my input code. 
but I can't programming how to compare each others. 
plz coding how to compare it.
eufgjt7s

eufgjt7s1#

我看不出有什么更好的方法比详尽地比较每一台笔记本电脑与每一个其他的,在所有的标准,以检查是否一台机器是过时的。
如果一台机器过时了,你可以在下一次的比较中把它排除在外。

8hhllhi2

8hhllhi22#

这里有一个程序,通过创建一个评级系统,并排名不同的笔记本电脑“CPU + RAM + HDD -价格=排名值”找到最好的笔记本电脑
如果你想找到最差的笔记本电脑,你可以反过来说:“price - CPU - RAM - HDD =排名值”//也许你必须用 * 2调整之前的价格值。
如果CPU对您来说更重要,那么您可以使用如下乘数:“CPU * 0.2 + RAM +硬盘-价格=排名值”
不同的笔记本电脑规格存储在input.txt中,如下所示

2100 512 150 200
2000 2048 240 350
2300 1024 200 320
2500 2048 80 300
2000 512 180 150

我不是很习惯python,也许有一个更优雅的解决方案...

#!/usr/bin/python3

input_array = []
#Para 1 = CPU; 2 = RAM; 3 = HDD; 4 = Price
parameter = 4

#Open File with parameter's in it //only for reading
with open('input.txt', 'r') as input_file:
    #read line by line
    input_array = input_file.readlines()
    #print(len(input_array))

#make array with max size of lines times pc parameter
val = [None] * len(input_array) * parameter

#print(input_array)

#remove new line element from input_array and store it in a new array
for idx, line in enumerate(input_array):
    val[idx] = line.strip()
    #print (idx)
    #print(val[idx])

#print(val)

#remove None value from array
res = []
for tmp_val in val:
    if tmp_val != None:
        res.append(tmp_val)

j = 0
j_old = 0
best_opt = 0
for idx, str_val in enumerate(res):
    #print (idx)
    res[idx] = str_val.split()
    #print(res[idx])
    j = 0
    #calc rating for every pc
    for opt, i in enumerate(res[idx]):
        #print(i)
        #add first 3 values //because more CPU more RAM more HDD = better
        if opt < 3:
            j = j + int(i)
            #print("idx %d" % (j))
        else:
            #sub last value //because less price = better
            j = j - int(i)
            #print("last idx %d" % (j))
            #check if rating is higher as last pc
            if j > j_old:
                j_old = j
                #remember highest rated pc
                best_opt = idx
        #print(j_old)

#print best rated pc
print("Best option: %d" % (best_opt + 1))

相关问题