#44394: python


I_II_III_IV_V (?1?1)

學校 : 不指定學校
編號 : 295016
來源 : [220.129.200.37]
最後登入時間 :
2025-08-23 18:02:20

指數序列和底數序列等比!

eg. 指數=1,3

底數和=6

則最佳底數解1.5,4.5

code 👇

 

 

import math

t = int(input())
for _ in range(t):
    n = int(input())
    a = sum(map(float, input().split()))
    b = list(map(int, input().split()))
    _b = sum(b)
    
    b1 = [a * x / _b for x in b]
    
    # Calculate the logarithmic sum
    log_o = 0
    for i in range(n):
        if b1[i] > 0:
            log_o += b[i] * math.log(b1[i])
        else:
            log_o = float('-inf')  # If b1[i] == 0, the product is zero
            break
    
    # Handle case where product is zero
    if log_o == float('-inf'):
        print("0.000E0")
        continue
    
    # Normalize the logarithm to prevent range errors
    exponent = int(log_o // math.log(10))  # Determine the scientific exponent
    mantissa = math.exp(log_o - exponent * math.log(10))  # Compute mantissa
    
    # Adjust exponent to match the desired format
    exponent_str = str(exponent)  # Convert exponent to string
    formatted_result = f"{mantissa:.3E}".replace("E+0", "E").replace("E+", "E").replace("E-0", "E-")
    formatted_result = formatted_result.replace("E+000", "E")  # Remove extra leading zeroes
    formatted_result = f"{mantissa:.3f}E{exponent}"  # Ensure exact format
    print(formatted_result)



#44396: Re: python


I_II_III_IV_V (?1?1)

學校 : 不指定學校
編號 : 295016
來源 : [220.129.200.37]
最後登入時間 :
2025-08-23 18:02:20

指數序列和底數序列等比!

eg. 指數=1,3

底數和=6

則最佳底數解1.5,4.5

code 👇

 

 

import math

t = int(input())
for _ in range(t):
    n = int(input())
    a = sum(map(float, input().split()))
    b = list(map(int, input().split()))
    _b = sum(b)
    
    b1 = [a * x / _b for x in b]
    
    # Calculate the logarithmic sum
    log_o = 0
    for i in range(n):
        if b1[i] > 0:
            log_o += b[i] * math.log(b1[i])
        else:
            log_o = float('-inf')  # If b1[i] == 0, the product is zero
            break
    
    # Handle case where product is zero
    if log_o == float('-inf'):
        print("0.000E0")
        continue
    
    # Normalize the logarithm to prevent range errors
    exponent = int(log_o // math.log(10))  # Determine the scientific exponent
    mantissa = math.exp(log_o - exponent * math.log(10))  # Compute mantissa
    
    # Adjust exponent to match the desired format
    exponent_str = str(exponent)  # Convert exponent to string
    formatted_result = f"{mantissa:.3E}".replace("E+0", "E").replace("E+", "E").replace("E-0", "E-")
    formatted_result = formatted_result.replace("E+000", "E")  # Remove extra leading zeroes
    formatted_result = f"{mantissa:.3f}E{exponent}"  # Ensure exact format
    print(formatted_result)




看不到?找找看吧