题解 | #等差数列#

等差数列

http://www.nowcoder.com/practice/f792cb014ed0474fb8f53389e7d9c07f

通项公式:

an=a1+(n1)da_n = a_1 + (n - 1) d

n项和:

Sn=n(a1+an)/2S_n = n (a_1 + a_n) / 2

套用公式即可

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int length = sc.nextInt(); // n
            int first = 2, last = 3 * length - 1; // a1, an
            System.out.println((first + last) * length / 2);
        }
        sc.close();
    }
}
全部评论
last写错了,没有加a1和括号,应该是last = first + 3 * (length - 1);
3 回复 分享
发布于 2022-06-06 15:31
last = first +(lengh-1)*3
点赞 回复 分享
发布于 04-15 22:08 上海

相关推荐

迷茫的大四🐶:摊牌了,我是25届的,你们也不招我
点赞 评论 收藏
分享
评论
30
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务