The warren of BASIC programming-java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.