题解 | #等差数列#
等差数列
http://www.nowcoder.com/practice/f792cb014ed0474fb8f53389e7d9c07f
了解等差数列公式即可:
等差数列{an}的通项公式为:an=a1+(n-1)d
前n项和公式为:Sn=n*a1+n(n-1)d/2或Sn=n(a1+an)/2
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
add(n);
}
public static void add(int n){
int sum = 0;
int a;
for (int i = 0; i < n; i++) {
a = 2 + (n - 1) * 3;
sum = n * (2 + a) / 2;
}
System.out.println(sum);
}
}