Bought a stock, he knew from the day he bought the stock, the stock will have the following changes: The first day unchanged, after a day, fell one day, up two days, fell one day, up three days, fell one day ... And so on
For the convenience of calculation, assuming that each rise and fall are 1, the initial stock price is also 1, please calculate the first n days of the stock to buy how much money per share value?
This is the simple programming question I see on a website. Guys can try it!
Personally think that doing more algorithmic programming problems, is conducive to improve their programming level, machine thinking
The following is a violent answer from a brain-crippled blogger:
public class Demo1 {
public static void Main (string[] args) {
Scanner sc=new Scanner (system.in);
while (Sc.hasnextint ()) {
int A=sc.nextint ();
System.out.println (GetPrice (a));
}
}
public static int GetPrice (int n) {
Double n1=n;
Double m;
M=math.sqrt ((((2*n) +0.25));//This is the formula that the blogger uses to calculate the pen in the book.
int p= (int) Math.floor ((m-0.5));//Rounding down to get the number of days the share price falls
int sum= (int) (n1-(2*p) +2);
return sum;
}
}
Below for everyone to attach the correct answer, Bo Master see after ..... I'm going to cry in the bathroom.
Import Java.util.Scanner;
public class Main {
public static void Main (string[] args) {
Scanner in = new Scanner (system.in);
Integer n = null;
while ((n = in.nextint ())!=null) {
int price = 1;
int I=2;int j=3;
for (int m=1;m<n;m++) {
if (m==i) {
price--;
i=i+j;//clearly can directly find the law of the number of days to fall, with a variable to store it, simple bar
j + +;
}
else{
price++;}
}
SYSTEM.OUT.PRINTLN (price);
}
}
}
For the first time, thank you!
The answer is from a website, not specifically disclosed here, to avoid the suspicion of advertising.
The warren of BASIC programming-java