验证尼科彻斯定理,即:任何一个整数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
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int m = in.nextInt(); int avg = m * m; for (int i = avg - m + 1; i < avg + m - 1; i += 2) { System.out.print(i + "+"); } System.out.print(avg + m - 1); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int num = in.nextInt(); int start = num*(num-1)+1;//第一个起始数遵循此公式 for(int i = 0; i < num; i++){ if(i == num-1){ System.out.print(start); }else{ System.out.print(start+"+"); } start+=2; } } }
public static void main(String[] args) { Scanner in = new Scanner(System.in); int num = in.nextInt(); int target = num * num * num; //思路:直接查找第一个开始相加的奇数,然后加到目标值即可 int begin = num * num - (num - 1); String answer = ""; int count = 0; for (int i = begin; i <= target; i += 2) { if (count < target) { count += i; if (i == begin) { answer += i; } else { answer = answer + "+" + i; } } else break; } System.out.println(answer); }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int start = num * (num - 1) + 1; StringBuilder sb = new StringBuilder(); for (int i = 0; i < num; i++) { sb.append(start).append("+"); start += 2; } System.out.println(sb.substring(0, sb.length() - 1)); } }
import java.util.Scanner; import java.math.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); double n = in.nextInt(); int m = (int)Math.pow(n, 3.0); for (int i = 1; i <= m; i = i + 2) { int sum = 0; for (int k = 0; k < n; k++) { sum += (i + 2 * k); } if (sum == m) { StringBuilder sb = new StringBuilder(); for (int k = 0; k < n; k++) { sb.append((i+2*k)+"+"); } System.out.println(sb.substring(0, sb.length()-1)); } } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 if (in.hasNext()) { int inputInt = in.nextInt(); int result = (int) Math.pow(inputInt, 3); int firstOdd = 1; int middleValue = result / inputInt; // == pow(inputInt, 2) // 输入值 if (inputInt % 2 == 0) { // 偶数 firstOdd = (middleValue + 1) - ((inputInt / 2) * 2); } else { // 奇数 firstOdd = middleValue - ((inputInt - 1) / 2 * 2); } int testResult = 0; StringBuffer outputResult = new StringBuffer(); for (int i = 0; i < inputInt; i++) { int item = firstOdd + (i *2); testResult += item; outputResult.append(item); if (i < inputInt - 1) { outputResult.append("+"); } } if (testResult == result) { // 验证成功,输出结果 System.out.println(outputResult.toString()); } } } }
import java.util.Scanner; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringJoiner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int pow = (int) Math.pow(n, 3); // 计算起始值 int number = (int) Math.pow(n, 2); int i = 0; if (n % 2 == 1) { i = number - n / 2 * 2; } else { i = number - n / 2 * 2 + 1; } StringJoiner sj = new StringJoiner("+", "", ""); // 循环 int sum = 0; for (int j = 0; j < n; j++) { sj.add(String.valueOf(i + 2 * j)); sum += i + 2 * j; } // 判断n个奇数之和是否等于n的立方 if (sum == pow) { System.out.println(sj); } } }
import java.io.*; public class Main { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str=br.readLine().trim(); if(str.equals("1")) System.out.println(1); else{ StringBuilder sb = new StringBuilder(); int m=Integer.parseInt(str); //分解式的第一个数值=(输入值*(输入值-1))+1 int star= m*(m-1)+1; //拼接连续m个奇数相加的分解式 for(int i=0;i<m;i++){ sb.append(star+"+"); star+=2; } //删除最后一个加号 sb.deleteCharAt(sb.length()-1); System.out.println(sb.toString()); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int a = in.nextInt(); int b = a * a; for (int i = 0, j = 0; j < a; i = i + 2, j++) { System.out.print(b - a + 1 + i); if (j < a - 1) { System.out.print("+"); } } } } }n的平方-n+1就是n个奇数的第一个,然后依次加2就是剩下的n-1个奇数
import java.util.List; import java.util.ArrayList; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int input = in.nextInt(); List<String> numArr = new ArrayList<>(Math.abs(input)); if (input == 0) { System.out.println(0); } else { if (input > 0 ) { for (int i = 0, k = 0; i < Math.abs(input); i++, k += 2) { numArr.add(String.valueOf(input * (input - 1) + 1 + k)); } System.out.println(String.join("+", numArr)); } else { for (int i = 0, k = 0; i < Math.abs(input); i++, k += 2) { numArr.add(String.valueOf(input * (input + 1) + 1 + k)); } System.out.println("-" + String.join("-", numArr)); } } } } }题目说整数,还以为会有负数,结果提了一下发现没有负数的用例
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { static int index=1; public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 ArrayList<String> list=new ArrayList<>(); int num=in.nextInt(); int index=1; for(int i=1;i<=100;i++){ list.add(getResult((int)Math.pow(i,3))); if(list.size()==num){ System.out.print(list.get(num-1)); } } } public static String getResult(int num){ int sum=0; String str=""; for(int i=index;;i++){ if(i%2!=0){ sum+=i; str+=i+"+"; if(sum==num){ index=i+1; return str.substring(0,str.length()-1); } } } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(); for (int i = 0; i < a; i++) { //找规律,规律如代码 System.out.print(a * a - (a - 1) + i * 2); if (i != a - 1) System.out.print("+"); } } }