Game Code Programming exercises

Source: Internet
Author: User

Warren

Title Description

Do you have a warren? Yes, the little game is! After rigorous calculation, the small race bought a stock, he knew from the day he bought stocks, 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?

Input:

Input includes multiple sets of data;

Enter one n,1<=n<=10^9 per line.

Output:

Please output how much per share he shares, for each group of data, output one line.

My train of thought:

First we see this sequence problem, the first thought is brute force, with an array to store all the data, this can solve the situation of less data

1 inta[10000];2a[1] =1;3     intFlag =1;4     inttemp =1;5      for(intK =2; k<10000; k++){6         if(flag>0) {A[k] = a[k-1] +1; }7         if(Flag = =0) {A[k] = a[k-1] -1; }8flag--;9         if(Flag <0){TenTemp = temp +1; OneFlag =temp; A         } -}

The above code is a simple traversal, K is the size of n, when my k too big when the program crashes, can not solve 10e9 of the situation, so this problem cannot be solved with this idea. What to do, I think about given n when we can fall a few days according to it before and it goes up for a few days after the last fall, and the first thing I think about is the arithmetic progression summation formula, assuming there's no downside s=n+n (n-1)/2, find out that n is K days a total of n days down. For example 3=2+2* (2-1), the third day experienced a fall, n=2, the stock price changed to k-2* (n-1) = 1; the 4th and 5th days are the same as the 3rd day, and the sixth day 6=3+3* (3-1); n=3, the stock price changes to k-2* (n-1) = 2; So you can see S=n+n (n-1)/2 and s<n+1+ (n+1) *n The situation is the stock price fell n-1 days.

#include"stdafx.h"#include<stdio.h>#include<iostream>using namespacestd;intMain () {intN;  while(cin>>N) {        inti;  for(i =1; I <= N; i++){            ints; S= i + i* (i-1) /2; if(s = = N) { Break; } if(S > N) {i = i-1; Break; } }        if(n = =1) printf ("1\n"); if(n = =2) printf ("2\n"); if(n = =3) printf ("1\n"); if(n>3) printf ("%d\n", N-2* (I-1)); }}

Game Code Programming exercises

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.