Python Code to Calculate Sum of Concatenated Integers
This code takes an input 'x' and calculates the sum of all the numbers formed by concatenating consecutive integers up to 'x'. For example, if 'x' is 3, the sum would be 123 + 12 + 3 = 138.
x = int(input())
if x < 0 or x > 6:
print('Date error!')
else:
sum = 0
for i in range(1, x+1):
data = ''
for j in range(1, i+1):
data = data + str(j)
sum = sum + int(data)
print(sum)
The code iterates from 1 to 'x' and for each number 'i', it creates a string 'data' by concatenating integers from 1 to 'i'. This string 'data' is then converted to an integer and added to the 'sum'. Finally, the 'sum' is printed.
原文地址: https://www.cveoy.top/t/topic/opM3 著作权归作者所有。请勿转载和采集!