Giúp đỡ về giải thuật, có hậu tạ.

Mày tìm chuỗi nhị phân độ dài n là ra. Nếu phần tử thứ i là 0 thì đéo cộng, còn thì cộng nhé
 
Cái này đệ quy thôi. Nhưng mà tệp bé thì oki. Chứ tệp lớn thì căng đấy.
Mày làm dc ko? Tệp có 70 số là bé hay lớn?
Làm được pm t đặt hàng trả công đàng hoàng nha
 
Mày convert sang python đi, tao chạy thử nếu chuẩn k bị lỗi thì tao hậu tạ đàng hoàn chứ 50k bõ bèn gì
Trời đụ, mày lên mạng kiếm mấy cái convert js sang python dễ mà:


Python:
targetNumber = 158
arrInput = [1, 3, 9, 14, 29, 33, 37, 48, 49, 51]

arr = sorted(arrInput, reverse=True)

def testSum(callerSum, arrTest, callerResult):
    for multiple in [1, 0]:
        newSum = callerSum + arrTest[0] * multiple
        if newSum > targetNumber:
            continue
        result = callerResult.copy()
        if multiple == 1:
            result.append(arrTest[0])
            if newSum == targetNumber:
                printResult(result)
        if len(arrTest) <= 1:
            continue
        else:
            testSum(newSum, arrTest[1:], result)

def printResult(result):
    print("- ", result)

testSum(0, arr, [])
 
Bố viết cho mày dạng async để khỏi tràn stack luôn (tương đương setTimeout 0 callback cho js)

Python:
import asyncio

targetNumber = 150
arrInput = [
    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
]

# Sort the array in descending order
arr = sorted(arrInput, reverse=True)

loop = asyncio.get_event_loop()

def test_sum(caller_sum, arr_test, caller_result):
    for multiple in [1, 0]:
        new_sum = caller_sum + arr_test[0] * multiple
        if new_sum > targetNumber:
            # If the sum is greater, skip further processing
            continue
        
        result = caller_result.copy()
        if multiple == 1:
            result.append(arr_test[0])
            # If the sum equals the target, print the result
            if new_sum == targetNumber:
                asyncio.get_event_loop().call_soon(print_result, result)
        
        if len(arr_test) <= 1:
            continue
        else:
            asyncio.get_event_loop().call_soon(
                test_sum, new_sum, arr_test[1:], result
            )

def print_result(result):
    print("- ", result)

# Initial call to test_sum
loop.call_soon(test_sum, 0, arr, [])

# Run the event loop
loop.run_forever()
 
Bố viết cho mày dạng async để khỏi tràn stack luôn (tương đương setTimeout 0 callback cho js)

Python:
import asyncio

targetNumber = 150
arrInput = [
    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
]

# Sort the array in descending order
arr = sorted(arrInput, reverse=True)

loop = asyncio.get_event_loop()

def test_sum(caller_sum, arr_test, caller_result):
    for multiple in [1, 0]:
        new_sum = caller_sum + arr_test[0] * multiple
        if new_sum > targetNumber:
            # If the sum is greater, skip further processing
            continue
       
        result = caller_result.copy()
        if multiple == 1:
            result.append(arr_test[0])
            # If the sum equals the target, print the result
            if new_sum == targetNumber:
                asyncio.get_event_loop().call_soon(print_result, result)
       
        if len(arr_test) <= 1:
            continue
        else:
            asyncio.get_event_loop().call_soon(
                test_sum, new_sum, arr_test[1:], result
            )

def print_result(result):
    print("- ", result)

# Initial call to test_sum
loop.call_soon(test_sum, 0, arr, [])

# Run the event loop
loop.run_forever()
Nhìn có vẻ hứa hẹn đó. Để chiều tao test, ổn thì tao gửi $$$ cho mày ngay
 
Tao đang nhìn quy luật dãy số
Tao thấy mày xàm lol trên này nhiều vl mà hoá ra tư duy logic ngu, khả năng đọc hiểu cũng kém nốt, nó cho dãy số tự nhiên bất kỳ + 1 cái ví dụ mà mày lại đi nhìn ví dụ để tìm quy luật thì đúng là phải luyện tập chăm chỉ vl chứ con người mới đẻ ra ko thể ngu thế đc. :D
 
targetNumber = 150
arrInput = [
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
]
Mày giải thích hộ t đoạn này:
- targetNumber = 150: số 150 này là gì? Ví dụ tệp tao chỉ có 70 số thì cái số này cần thay đổi ntn?
- arrInput = [ ]: tao thử thay đổi số trong tệp, rút xuống 10 số thì code nó đứng yên ko chạy [1,2,3,4,5,6,7,8,9,10] vậy muốn thay đổi thì phải chỉnh sửa ntn?
 
Bố viết cho mày dạng async để khỏi tràn stack luôn (tương đương setTimeout 0 callback cho js)

Python:
import asyncio

targetNumber = 150
arrInput = [
    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
]

# Sort the array in descending order
arr = sorted(arrInput, reverse=True)

loop = asyncio.get_event_loop()

def test_sum(caller_sum, arr_test, caller_result):
    for multiple in [1, 0]:
        new_sum = caller_sum + arr_test[0] * multiple
        if new_sum > targetNumber:
            # If the sum is greater, skip further processing
            continue
       
        result = caller_result.copy()
        if multiple == 1:
            result.append(arr_test[0])
            # If the sum equals the target, print the result
            if new_sum == targetNumber:
                asyncio.get_event_loop().call_soon(print_result, result)
       
        if len(arr_test) <= 1:
            continue
        else:
            asyncio.get_event_loop().call_soon(
                test_sum, new_sum, arr_test[1:], result
            )

def print_result(result):
    print("- ", result)

# Initial call to test_sum
loop.call_soon(test_sum, 0, arr, [])

# Run the event loop
loop.run_forever()
Ôi thôi mày hiểu nhầm đề bài rồi. Đề bài là in ra tất cả các trường hợp có thể của tổng các số bất kỳ trong tệp. Không phải là biết trước tổng rồi tìm các combo có thể ra được số tổng đó.

Tao không có target một số cụ thể nào hết, tao chỉ muốn một danh sách các tổng có thể xảy ra giữa các combo số trong tệp thôi.
 
Quan trọng là lọc được ra hết tất cả các kết quả khả thi. Ví dụ tệp có 10 số nhưng tao cần lọc ra tất cả các kết quả tổng của 5 số, 6 số, hoặc 7-8 số bất kỳ trong tệp thì có code được ko?

Dm treo hẳn 10tr luôn cho tml code được giúp tao.
Chờ tí, tao đang chở khách.😂🤣😂
 
Tao thấy mày xàm lol trên này nhiều vl mà hoá ra tư duy logic ngu, khả năng đọc hiểu cũng kém nốt, nó cho dãy số tự nhiên bất kỳ + 1 cái ví dụ mà mày lại đi nhìn ví dụ để tìm quy luật thì đúng là phải luyện tập chăm chỉ vl chứ con người mới đẻ ra ko thể ngu thế đc. :d
Mày nói cũng đúng,tao hay kiểu tư duy ngược so với truyền thống,tao ngu do gen di truyền
 
Ôi thôi mày hiểu nhầm đề bài rồi. Đề bài là in ra tất cả các trường hợp có thể của tổng các số bất kỳ trong tệp. Không phải là biết trước tổng rồi tìm các combo có thể ra được số tổng đó.

Tao không có target một số cụ thể nào hết, tao chỉ muốn một danh sách các tổng có thể xảy ra giữa các combo số trong tệp thôi.
Ơ địt mẹ...
 
from itertools import combinations

def sum_of_combinations(numbers):
total_sum = 0
for r in range(1, len(numbers) + 1):
for combo in combinations(numbers, r):
total_sum += sum(combo)
return total_sum

# Example usage
numbers = [1, 2, 3]
result = sum_of_combinations(numbers)
print("Sum of all possible combinations:", result)
 
from itertools import combinations

def sum_of_combination(numbers):
combination_sums = []
for r in range(1, len(numbers) + 1):
for combo in combinations(numbers, r):
combination_sums.append(sum(combo))
return combination_sums

# Example usage
numbers = [1, 2, 3]
combination_sums = sum_of_combination(numbers)
print("Sums of possible combinations:", combination_sums)
 

Có thể bạn quan tâm

Top