java实现:简单题,用nums记录控制台传进来的项数,用sum记录总和,初始化0。
now为当前项的值,首项为2。从首项开始 sum += now,now += 3, 最后输出sum即可。
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 nums = in.nextInt();
int sum = 0, now = 2;
for (int i = 0; i < nums; i++){
sum += now;
now += 3;
}
System.out.println(sum);
}
}
} 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(sum(a));
}
}
private static int sum(int n) {
return (3*n *n + n) / 2;
}
} 基本递归
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
System.out.print(sum(2, 3, n));
}
public static int sum(int s, int d, int n) {
if (n == 0) {
return 0;
} else {
return sum(s+d, d, n-1) + s;
}
}
}
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
long total = 0L;
while (m > 0) {
total += 2 + (3 * (--m));
}
System.out.println(total);
}
} /**
* 等差数列求和 (首项+末项)*项数/2
* 末项 a1+(n-)*d
* 公式化简 na1+n*(n-1)*d/2
*/
public static void equidistant100(){
Scanner sc=new Scanner(System.in);
int n = sc.nextInt();
sc.close();
System.out.println(n*2+n*(n-1)*3/2);
} 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 a = in.nextInt();
int sum = 0;
for (int j = 1; j <= a; j++) {
sum += j*3 - 1;
}
System.out.println(sum);
}
} import java.util.*;
import java.math.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
Long n=in.nextLong();
//Sn=a1*n+n(n-1)d/2
Long d=3l;
Long sum=2*n+(n*(n-1)*d)/2;
System.out.print(sum);
}
}