Python có sự lặp lại thực sự mạnh mẽ sẽ giúp bạn ra khỏi đây. Show
Đối với người mới bắt đầu, đừng chuyển
Tuy nhiên, điều này sẽ không hoàn toàn giúp bạn vì chỉ số của phép lặp là cần thiết cho sức mạnh.
Điều này trình bày một vấn đề khác, tuy nhiên. Sức mạnh là ngược. Đối với điều này, bạn muốn 0.
Từ đây bạn chỉ cần xử lý đầu ra của mình:
Xem thảo luận Cải thiện bài viết Lưu bài viết Xem thảo luận Cải thiện bài viết Lưu bài viết Đọc Examples: # Evaluate value of 2x3 - 6x2 + 2x - 1 for x = 3 Input: poly[] = {2, -6, 2, -1}, x = 3 Output: 5 # Evaluate value of 2x3 + 3x + 1 for x = 2 Input: poly[] = {2, 0, 3, 1}, x = 2 Output: 23 # Evaluate value of 2x + 5 for x = 5 Input: poly[] = {2, 5}, x = 5 Output: 15 Bàn luận Bài viết sau đây chứa các chương trình để tính toán một phương trình đa thức cho rằng các hệ số của đa thức được lưu trữ trong một danh sách. Phương trình sẽ thuộc loại:
Phương pháp ngây thơ: Sử dụng cho vòng lặp để tính toán giá trị. Phương pháp tối ưu hóa: Sử dụng phương pháp Horner, để tính toán giá trị.
Example: Python3Các kết quả được tạo ra trong hai bước trên sẽ được thêm Điều này sẽ tiếp tục cho đến khi tất cả các hệ số được bảo hiểm. 1 2 3 4 5 6 7 5 4 5 6 223 4 2 6 7 2 len 0 1 2 3 4 5 6 range 8
9# Evaluate value of 2x3 - 6x2 + 2x - 1 for x = 3 Input: poly[] = {2, -6, 2, -1}, x = 3 Output: 5 # Evaluate value of 2x3 + 3x + 1 for x = 2 Input: poly[] = {2, 0, 3, 1}, x = 2 Output: 23 # Evaluate value of 2x + 5 for x = 5 Input: poly[] = {2, 5}, x = 5 Output: 150 2 # Evaluate value of 2x3 - 6x2 + 2x - 1 for x = 3 Input: poly[] = {2, -6, 2, -1}, x = 3 Output: 5 # Evaluate value of 2x3 + 3x + 1 for x = 2 Input: poly[] = {2, 0, 3, 1}, x = 2 Output: 23 # Evaluate value of 2x + 5 for x = 5 Input: poly[] = {2, 5}, x = 5 Output: 152 9 4 # Evaluate value of 2x3 - 6x2 + 2x - 1 for x = 3 Input: poly[] = {2, -6, 2, -1}, x = 3 Output: 5 # Evaluate value of 2x3 + 3x + 1 for x = 2 Input: poly[] = {2, 0, 3, 1}, x = 2 Output: 23 # Evaluate value of 2x + 5 for x = 5 Input: poly[] = {2, 5}, x = 5 Output: 155 6 range __Value of polynomial is: 56 Value of polynomial is: 57 Output: 5
Độ phức tạp về thời gian: O (N2) Python3Phương pháp được tối ưu hóa: Phương pháp Horner có thể được sử dụng để đánh giá đa thức trong thời gian O (N). Để hiểu phương pháp, chúng ta hãy xem xét ví dụ về 2x3 - 6x2 + 2x - 1. đa thức có thể được đánh giá là ((2x - 6) x + 2) x - 1. Ý tưởng là khởi tạo kết quả là hệ số của XN Đó là 2 trong trường hợp này, nhiều lần nhân kết quả với X và thêm hệ số tiếp theo để kết quả. Cuối cùng, trả về kết quả. & Nbsp; Value of polynomial is: 58 Value of polynomial is: 59 9 1 2 len 3 3 3 9 4 5 6 range range 1 222Các kết quả được tạo ra trong hai bước trên sẽ được thêm Điều này sẽ tiếp tục cho đến khi tất cả các hệ số được bảo hiểm. 1 2 3 4 5 6 7 5 4 5 6 223Value of polynomial is: 56 range 1 17 18Output: Value of polynomial is: 5
for i, x in enumerate(reversed(self.coeffs)): 7for i, x in enumerate(self.coeffs): 2 lenitems = [] for i, x in enumerate(reversed(self.coeffs)): if not x: continue items.append('{}x^{}'.format(x if x != 1 else '', i)) result = ' + '.join(items) result = result.replace('x^0', '') result = result.replace('^1 ', ' ') result = result.replace('+ -', '- ') 0
items = [] for i, x in enumerate(reversed(self.coeffs)): if not x: continue items.append('{}x^{}'.format(x if x != 1 else '', i)) result = ' + '.join(items) result = result.replace('x^0', '') result = result.replace('^1 ', ' ') result = result.replace('+ -', '- ') 4 items = [] for i, x in enumerate(reversed(self.coeffs)): if not x: continue items.append('{}x^{}'.format(x if x != 1 else '', i)) result = ' + '.join(items) result = result.replace('x^0', '') result = result.replace('^1 ', ' ') result = result.replace('+ -', '- ') 5items = [] for i, x in enumerate(reversed(self.coeffs)): if not x: continue items.append('{}x^{}'.format(x if x != 1 else '', i)) result = ' + '.join(items) result = result.replace('x^0', '') result = result.replace('^1 ', ' ') result = result.replace('+ -', '- ') 6 rangeitems = [] for i, x in enumerate(reversed(self.coeffs)): if not x: continue items.append('{}x^{}'.format(x if x != 1 else '', i)) result = ' + '.join(items) result = result.replace('x^0', '') result = result.replace('^1 ', ' ') result = result.replace('+ -', '- ') 8 9# Evaluate value of 2x3 - 6x2 + 2x - 1 for x = 3 Input: poly[] = {2, -6, 2, -1}, x = 3 Output: 5 # Evaluate value of 2x3 + 3x + 1 for x = 2 Input: poly[] = {2, 0, 3, 1}, x = 2 Output: 23 # Evaluate value of 2x + 5 for x = 5 Input: poly[] = {2, 5}, x = 5 Output: 150 2 # Evaluate value of 2x3 - 6x2 + 2x - 1 for x = 3 Input: poly[] = {2, -6, 2, -1}, x = 3 Output: 5 # Evaluate value of 2x3 + 3x + 1 for x = 2 Input: poly[] = {2, 0, 3, 1}, x = 2 Output: 23 # Evaluate value of 2x + 5 for x = 5 Input: poly[] = {2, 5}, x = 5 Output: 152group together like-exponents and sum their coefficients. (In fact, if you were to stick with the Counter representation throughout, you could just return p + q ). items = [] for i, x in enumerate(reversed(self.coeffs)): if not x: continue items.append('{}x^{}'.format(x if x != 1 else '', i)) result = ' + '.join(items) result = result.replace('x^0', '') result = result.replace('^1 ', ' ') result = result.replace('+ -', '- ') 9items = [] for i, x in enumerate(reversed(self.coeffs)): if not x: continue items.append('{}x^{}'.format(x if x != 1 else '', i)) result = ' + '.join(items) result = result.replace('x^0', '') result = result.replace('^1 ', ' ') result = result.replace('+ -', '- ') 4 # Evaluate value of 2x3 - 6x2 + 2x - 1 for x = 3 Input: poly[] = {2, -6, 2, -1}, x = 3 Output: 5 # Evaluate value of 2x3 + 3x + 1 for x = 2 Input: poly[] = {2, 0, 3, 1}, x = 2 Output: 23 # Evaluate value of 2x + 5 for x = 5 Input: poly[] = {2, 5}, x = 5 Output: 155items = [] for i, x in enumerate(reversed(self.coeffs)): if not x: continue items.append('{}x^{}'.format(x if x != 1 else '', i)) result = ' + '.join(items) result = result.replace('x^0', '') result = result.replace('^1 ', ' ') result = result.replace('+ -', '- ') 6 range__54 # Evaluate value of 2x3 - 6x2 + 2x - 1 for x = 3 Input: poly[] = {2, -6, 2, -1}, x = 3 Output: 5 # Evaluate value of 2x3 + 3x + 1 for x = 2 Input: poly[] = {2, 0, 3, 1}, x = 2 Output: 23 # Evaluate value of 2x + 5 for x = 5 Input: poly[] = {2, 5}, x = 5 Output: 150 2 # Evaluate value of 2x3 - 6x2 + 2x - 1 for x = 3 Input: poly[] = {2, -6, 2, -1}, x = 3 Output: 5 # Evaluate value of 2x3 + 3x + 1 for x = 2 Input: poly[] = {2, 0, 3, 1}, x = 2 Output: 23 # Evaluate value of 2x + 5 for x = 5 Input: poly[] = {2, 5}, x = 5 Output: 150 58 59 |