🌕🌗🌑🌑🌑

# 題目連結

  • 題目連結
  • Online Judge
  • uDebug

# 題目說明

Time limit: 3.000 seconds

# 題目

Joana loves playing with odd numbers. In the other day, she started writing, in each line, an odd number of odd numbers. It looked as follows:

135791113151719212325272931...\begin {matrix} &1 & & & & & & \\ &3 &5 &7 & & & &\\ &9 &11 &13 &15 &17 & &\\ &19 &21 &23 &25 &27 &29 &31\\ &... & & & & & & \end {matrix}

On a certain line Joana wrote 55 odd numbers. Can you discover the sum of the last three numbers written in that line? Can you do this more generally for a given quantity of odd numbers?

Given the number N of odd numbers in a certain line, your task is to determine the sum of the last three numbers of that line.

# Input

The input is a sequence of lines, one odd number N (1 < N < 1000000000) per line

# Output

For each input line write the sum of the last three odd numbers written by Joana in that line with N numbers. This sum is guaranteed to be less than 263.

# Sample Input

3
5
7

# Sample Output

15
45
87

# 解題技巧

這題雖然不難,但是要想得到運算的方法:

  • line : 計算出 N 是第幾行
  • totalNum : 再利用行數計算出,到該行總共有幾個數字
  • lastNum : 最後利用總數字個數計算出該行最後一個奇數
  • ans : 藉由最後一個數字算出答案

提示: 因為 1 < N < 1000000000 ,所以要利用 long

# Solution

Main.java
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLong()) {
            long N = sc.nextLong();
            long line = (N + 1) / 2;
            long totalNum = line * line;
            long lastNum = 2 * (totalNum) - 1;
            long ans = (lastNum - 2) * 3;
            System.out.println(ans);
        }
        sc.close();
    }
}
單字

** **
!! !!

片語 & 搭配詞

!! !!