验证尼科彻斯定理,即:任何一个整数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
while True: try: m = int(input()) n = m ** 2 result1 = [] result2 = [] if n == 1: print(1) elif n % 2 == 0: for i in range(1,m+1): if (n - i) % 2 != 0: result1.append(n - i) for i in range(1, m + 1): if (n + i) % 2 != 0: result1.append(n + i) print('+'.join(map(str,(sorted(result1))))) elif n % 2 != 0: for i in range(1,m+1): if (n - i) % 2 != 0: result2.append(n - i) for i in range(1, m + 1): if (n + i) % 2 != 0: result2.append(n + i) result2.append(n) print('+'.join(map(str,(sorted(result2))))) except:break发现规律,每个尼科彻斯的数字都是围绕n的平方向两边展开的数字,并且数字的个数为n
import java.util.*; import java.io.*; public class Main { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); while (scanner.hasNext()) { int input = scanner.nextInt(); scanner.nextLine(); getString(input); } scanner.close(); } public static void getString(int input) { // first Num format int curNum = input * (input - 1) + 1; StringBuffer sBuffer = new StringBuffer(); // build string for (int i = 0; i < input; i++) { if (i == input - 1) { sBuffer.append(curNum); } else { sBuffer.append(curNum + "+"); } curNum += 2; } System.out.println(sBuffer); } }
import java.util.Scanner; public class Cube { public static void main(String[] args) { Scanner sc = new Scanner(System.in) ; while(sc.hasNext()){ int N = sc.nextInt() ; String s = decompose(N) ; System.out.println(s); } sc.close(); } private static String decompose(int n) { int [] array = new int[n] ; int mid = n / 2 ; if(n*n % 2 == 0) array[mid] = n*n + 1; else array[mid] = n*n ; for(int i = mid; i >= 1 ; i--){ int temp = array[i] ; array[i - 1] = temp - 2 ; } for(int j = mid ; j < n - 1; j++){ int temp = array[j] ; array[j + 1] = temp + 2 ; } StringBuffer sb = new StringBuffer() ; for(int k = 0 ; k < n ; k++){ if(k != n - 1) sb.append(array[k] + "+") ; } sb.append(array[n - 1]) ; return sb.toString(); } }
while True:
try:
n = int(input())
if n == 1:
print(1)
if n > 1:
list = []
a = n*n-n-1
for i in range(n):
a += 2
list.append(str(a))
print("+".join(list))
except:
break
while True: try: n = int(input()) cube = n**3 ls = [] if n%2==0: #确定起始数字 start = cube//n - 2*(n//2) +1 else: start = cube//n - 2*(n//2) for i in range(n): #从起始数字开始,数n位。 ls.append(str(start+i*2)) print('+'.join(ls)) except: break
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int num = sc.nextInt(); int start = num * num - num; for (int i = 0; i < num; i++) { if (start % 2 != 1) { start ++; } if (i != num - 1) { System.out.print(start + "+"); start += 2; } else { System.out.println(start); } } } } }
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; int middle=n*n; for(int i=0;i<n;i++){ cout<<middle-n+1+2*i; if(i!=n-1) cout<<"+"; } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ int n = scanner.nextInt(); System.out.println(getResult(n)); } scanner.close(); } //中间值是n的平方,利用中间值求出第一个数后依次递增 public static String getResult(int n){ int middle = 0; int begin = 0; String result = ""; middle = n * n; begin = middle - n + 1; for(int i=0; i<n; i++){ result += begin + 2 * i; if(i != n - 1){ result += "+"; } } return result; } }
#include <stdio.h> int main() { int n; while(scanf("%d",&n) != EOF) { int k=n*n-n+1; printf("%d",k); int i=2; for(i;i<=n;i++) { k+=2; printf("+%d",k); } printf("\n"); } return 0; }数学思路
while True: try: num = int(input()) if num == 1: print('1') else: str_out = '' num_p = 0 num_sta = num*(num-1)-1 while num_p < num**3: num_sta += 2 num_p += num_sta str_out = str_out + '+' + str(num_sta) str_out = str_out[1::] print(str_out) except: break
JAVA 等差数列求和公式
import java.util.*; public class Main{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); while(scan.hasNextInt()){ int num = scan.nextInt(); int num3 = (int)Math.pow(num, 3); int a1 = 1; for(int i = 1; i < num3; i += 2){ if(i * num + num * (num - 1) == num3){ a1 = i; } } for(int i = 0; i < num; i++){ if(i == num - 1){ System.out.println(a1 + 2 * i); }else{ System.out.print((a1 + 2 * i) + "+"); } } } } }
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