题解 | #等差数列#

等差数列

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

import java.util.Scanner;
import java.util.Stack;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
    Scanner scan = new  Scanner(System.in);
        int a =scan.nextInt();
        int b = 2;
        Stack stack = new Stack<Integer>();
        stack.push(b);
        for (int i=1;i<a;i++){
            b=b+3;
            stack.push(b);
        }
        Integer num = 0;
        while(!stack.isEmpty()){
            num +=(Integer)stack.pop();
        }
        System.out.println(num);
    }
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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