Skyscraper
題目描述
Mr. Port plans to start a new business renting one or more floors of the new skyscraper with one giga floors, MinatoHarukas. He wants to rent as many vertically adjacent floors as possible, because he wants to show advertisement on as many vertically adjacent windows as possible. The rent for one floor is proportional to the floor number, that is, the rent per month for the n-th floor is n times that of the first floor. Here, the ground floor is called the first floor in the American style, and basement floors are out of consideration for the renting. In order to help Mr. Port, you should write a program that computes the vertically adjacent floors satisfying his requirement and whose total rental cost per month is exactly equal to his budget.
 For example, when his budget is 15 units, with one unit being the rent of the first floor, there are four possible rent plans, 1+2+3+4+5, 4+5+6, 7+8, and 15. For all of them, the sums are equal to 15. Of course in this example the rent of maximal number of the floors is that of 1+2+3+4+5, that is, the rent from the first floor to the fifth floor.
?
輸入
The input consists of multiple datasets, each in the following format.
 b?
 A dataset consists of one line, the budget of Mr. Port b as multiples of the rent of the first floor. b? is a positive integer satisfying 1 < b < 109.
 The end of the input is indicated by a line containing a zero. The number of datasets does not exceed 1000.
?
輸出
For each dataset, output a single line containing two positive integers representing the plan with the maximal number of vertically adjacent floors with its rent price exactly equal to the budget of Mr. Port. The first should be the lowest floor number and the second should be the number of floors.
?
樣例輸入
15 16 2 3 9699690 223092870 847288609 900660121 987698769 999999999 0?
樣例輸出
1 5 16 1 2 1 1 2 16 4389 129 20995 4112949 206 15006 30011 46887 17718 163837 5994原來是一個等差數列。把求和公式化簡為是一個一元二次方程在利用韋達定理求解就行了。
(韋達定理:x1+x2= -(b/a)? ? ;? ? x1*x2=c/a);
直接求是會超時的。一層循環是1e9,只要有另一層循環,不管是多小,一般是會超時的。
代碼:
 ?
#include <iostream> ?
 #include <cmath> ?? ?
 using namespace std;
int main()
 {
 ?? ?int n;
 ?? ?while(scanf("%d",&n)&&n!=0) ? ?//可以看出來定是整數的 。?
 ?? ?{
 ?? ??? ?for(int i=(int)sqrt(2*n); i>=1 ;i--) ? //邊界問題也是要注意。
 ?? ??? ?{
 ?? ??? ??? ?double temp=(double)i;
 ?? ??? ??? ?if( (2*n)/temp-(2*n)/i==0 ) ? ? ? //精度損失。?
 ?? ??? ??? ?{
 ?? ??? ??? ??? ?int flag=(2*n-i*i)/i;
 ?? ??? ??? ??? ?double abc=(double)i;
 ?? ??? ??? ??? ?if( (2*n-abc*abc)/abc - flag!=0 ?)
 ?? ??? ??? ??? ??? ?continue;
 ?? ??? ??? ??? ?int ans=(flag+1)/2;
 ?? ??? ??? ??? ?if( ?(flag+1)/2.0-ans!=0 ?)continue;
 ?? ??? ??? ??? ?cout<<ans<<" "<<i<<endl;?? ?
 ?? ??? ??? ??? ?break;
 ?? ??? ??? ?}?? ? ?? ?
 ?? ??? ?}
 ?? ?}?? ?
 ?? ?return 0;?? ?
 }?
總結
以上是生活随笔為你收集整理的Skyscraper的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 中石油oj 2654: 序列合并
- 下一篇: Find Integer(费马大定理的使
