0
Follow
0
View

# 2991: [NOIP2015] Gold T1

ds1989126 注册会员
2023-02-25 18:13

? How can I get a negative number after running?

tuoluzhe 注册会员
2023-02-25 18:13
< div class = "md_content_show e397 data - v - 3967" = "" >

code modified

luilibo 注册会员
2023-02-25 18:13
DDN911 注册会员
2023-02-25 18:13

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > the code idea is calculated according to the gold distribution regularity of the title, but there are some problems.

First, when calculating the number of gold coins obtained every day for i days, the code uses i * i, which is wrong. It should be calculated according to the problem rule, that is, when the knight receives N gold coins every day for N consecutive days, he will receive N+1 gold coins every day for N+1 consecutive days. So you should use i < = k ? i: k + 1.

Second, the variable a in the code is not used and can be removed.

Finally, sum < can be used to judge the boundary conditions; = n to see if it exceeds the number of gold coins issued in the previous K days.

Below is the modified code, can AC this problem.

``````import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int k = scanner.nextInt();
int coins = 0;
int days = 0;
int n = 1;
while (days < k) {
for (int i = 0; i < n && days < k; i++) {
coins += n;
days++;
}
n++;
}
System.out.println(coins);
}

}

``````

dying87 注册会员

Publish Time
2023-02-25 18:13
Update Time
2023-02-25 18:13