输入一个整数
。
在一行上输出一个字符串,用于描述这个数列中的元素从小到大相加的形式。元素与元素之间用加号连接。
1
1
2
3+5
3
7+9+11
4
13+15+17+19
最笨的办法
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a = 1;
int result = 0;
for(int i=1; i<=n; i++){
result = a+(i-1)*2;
a = result;
}
for(int i=0; i<n ; i++){
if(i<n-1){
System.out.print(a+"+");
a+=2;
}else{
System.out.print(a);
}
}
}
} import java.util.*;
// 注意类名必须为 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 a = in.nextInt();
//存储结果
List<Integer> nums = new ArrayList<>();
for (int i = 0; nums.size() < a ;i++){
//生成长度为a的list
nums.add(1+2*i);
}
while(sumNum(nums) != a * a * a){
//如果不符合条件,获取下一个奇数
int next = nums.get(a-1) + 2;
//将列表中的第一个奇数移除
nums.remove(0);
//将下一个奇数放入list中
nums.add(next);
}
for (int i = 0; i< a; i++){
System.out.print(nums.get(i));
if (i!=a-1){
System.out.print("+");
}
}
}
}
//将List中的所有数加和
public static int sumNum(List<Integer> nums ){
int sum=0;
for (int i = 0 ; i<nums.size();i++){
sum += nums.get(i);
}
return sum;
}
} 观察得到第一个数值的规律 比如n = 2,第一个数为1 + (2 - 1) * 2 = 3,扩展得n = x时,
第一个数为n = x - 1时的第一个数 + (x - 1) * 2,递归找到n = x时的第一个数大小,公差为2,输出x个值。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int first = dfs(n);
int res = 0;
for (int i = 0; i < n; i++) {
System.out.print(first);
if (i != n - 1) {
System.out.print("+");
}
first += 2;
}
}
private static int dfs(int n) {
int first = 0;
if (n == 1) {
return 1;
}
return (n - 1) * 2 + dfs(n - 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 的区别
int n = in.nextInt();
StringBuffer bf = new StringBuffer();
for(int i =0;i<n;i++){
long a = n*n-n+1+2*i;
bf.append(String.valueOf(a)+"+");
}
String strf = bf.toString();
if(strf.endsWith("+")){
int index = strf.lastIndexOf("+");
strf = (String) strf.subSequence(0, index);
}
System.out.println(strf);
}
} 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 a = in.nextInt();
System.out.println(printManchester(a));
}
}
private static String printManchester(int n) {
int begin = n * n - (n - 1);
StringBuilder s = new StringBuilder();
for (int i = 0; i < n; i++) {
s.append(begin).append("+");
begin += 2;
}
return s.substring(0, s.length() - 1).toString();
}
} 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));
}
}
}
}
} 题目说整数,还以为会有负数,结果提了一下发现没有负数的用例