验证尼科彻斯定理,即:任何一个整数m的立方都可以写成m个连续奇数之和。
例如:
1^3=1
2^3=3+5
3^3=7+9+11
4^3=13+15+17+19
输入一个正整数m(m≤100),将m的立方写成m个连续奇数之和的形式输出。
数据范围:
进阶:时间复杂度:,空间复杂度:
验证尼科彻斯定理,即:任何一个整数m的立方都可以写成m个连续奇数之和。
例如:
1^3=1
2^3=3+5
3^3=7+9+11
4^3=13+15+17+19
输入一个int整数
输出分解后的string
6
31+33+35+37+39+41
python小白的思路,一步接一步,比起大神的思路,代码还是太复杂了 def getresult(n): if n <= 100: midNumber = 0 n_square = n**2 n_half = int(n/2) n_half_odd = int((n-1)/2) list_front = [] list_last = [] #当输入n为偶数的时候 if n % 2 == 0: for i in range(n_half): list_front.append(n_square-1) n_square = n_square - 2 list_front = list_front[::-1] n_square = n**2 for j in range(n_half): list_last.append(n_square+1) n_square = n_square + 2 list = list_front + list_last else: for i in range(n_half_odd): list_front.append(n_square-2) n_square = n_square -2 list_front = list_front[::-1] n_square = n**2 list_front.append(n_square) for j in range(n_half_odd): list_last.append(n_square+2) n_square += 2 list = list_front + list_last result = str(list[0]) for i in range(1,len(list)): result += "+" + str(list[i]) return result try: while True: a = int(input()) print(getresult(a)) except: pass
import sys for line in sys.stdin: m = int(line.strip()) # n = 0.5 * m * m - 0.5 * m + 1 # sequence: 2n-1, 2n+1, 2n+3, ..., 2n + (2m-3) # so: m^2 - m + 1, m^2 -m + 3, ... m^2 +m -1 sequence = [ str(m * m -m + 1 + 2 * i) for i in range(m) ] print("+".join(sequence))
while True: try: m = int(input().strip()) first_odd = m * m - m + 1 res = [first_odd + 2 * i for i in range(m)] print('+'.join(map(str, res))) except: break方法二:求出m个连续奇数的中间值,从中间值开始,向两边同时加减
while True: try: res = [] m = int(input().strip()) mid = int(m ** 3 / m) if mid % 2 == 1: res.append(mid) for i in range(1, int(m/2)+1): a, b = mid - 2 * i, mid + 2 * i res.extend([a, b]) else: for j in range(int(m/2)): a, b = mid - (1+2*j), mid + (1+2*j) res.extend([a, b]) # a, b = mid - 1, mid + 1 # res.extend([a, b]) # for j in range(1, int(m//2)): # c, d = a - 2 * j, b + 2 * j # res.extend([c, d]) res.sort() print('+'.join(map(str, res))) except: break
# 2020年11月14日17:49:25 ''' m^3 = x1 + x2 + x3 + ... + xm x1、x2、x3、...、xm为m个连续的奇数,设这m个数的均值为average x1 + x2 + x3 + ... + xm = m * average average = m^3 / m = m^2 当m为奇数时:x1 = average - (m-1)/2 * 2 当m为偶数时:x1 = average - m/2 * 2 + 1 x1 = average - m + 1 ''' while True: try: m = int(input()) express = "" # 求这几个和数的均值 average_value = int(m**3/m) # 求起始值 start_value = average_value - m + 1 for i in range(m): express += str(start_value+i*2) if i != m-1: express += "+" print(express) except: break
def NKCS(m): list1 = [] if m % 2==0: middle = m**2-1 list1.append(middle) for i in range(int(m/2)-1,0,-1): list1.append(middle-(2*i)) for i in range(1,int(m/2)+1): list1.append(middle+(2*i)) else: middle = m**2 list1.append(middle) for i in range(int(m/2),0,-1): list1.append(middle+(2*i)) list1.append(middle-(2*i)) return ('+'.join(map(str,sorted(list1)))) while True: try: N = int(input()) print(NKCS(N)) except: break
import sys for line in sys.stdin: n = int(line) res = [] k = n*(n-1)+1 # 初始值 res.append(str(k)) for i in range(1,n): k = k + 2 res.append(str(k)) print('+'.join(res))