import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
int count = 0;
for (int i = 1; i <= n; i++) {
int number = 3 * i - 1;
count += number;
}
System.out.println(count);
}
sc.close();
}
}
更优解:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
if (n > 1) {
int last = 3 * n - 1;
System.out.println((2 + last) * n / 2);
} else {
System.out.println(2);
}
}
sc.close();
}
}