-->
当前位置:首页 > 题库 > 正文内容

程序填空题:Square Numbers

Luz4年前 (2021-05-10)题库611
The following program reads two positive number n and k in, then sums all the square of numbers from [1,n] that
divided by k evenly, and prints the sum.

For example, for input `10 3`, the numbers in [1,10] are: `1, 2, 3, 4, 5, 6, 7, 8, 9, 10`.

Among them, the numbers divided by 3 evenly are: `3, 6, 9`.

The square of these numbers are: `9, 36, 81`.

The sum of the numbers above is: `126`

Now, fill in the blanks below:

```Java

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
int sum = IntStream.range(@@[1, n+1](1))
.filter(@@[x->x%k==0](2))
.map(@@[x->x*x](2))
.sum();
System.out.println(sum);
in.close();
}
}
```





答案:
第1空:1, n+1

第2空:x->x%k==0

第3空:x->x*x

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。