日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Skyscraper

發(fā)布時間:2024/10/14 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Skyscraper 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目描述

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

原來是一個等差數(shù)列。把求和公式化簡為是一個一元二次方程在利用韋達(dá)定理求解就行了。

(韋達(dá)定理:x1+x2= -(b/a)? ? ;? ? x1*x2=c/a);

直接求是會超時的。一層循環(huán)是1e9,只要有另一層循環(huán),不管是多小,一般是會超時的。

代碼:
?

#include <iostream> ?
#include <cmath> ?? ?
using namespace std;

int main()
{
?? ?int n;
?? ?while(scanf("%d",&n)&&n!=0) ? ?//可以看出來定是整數(shù)的 。?
?? ?{
?? ??? ?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;?? ?
}?

總結(jié)

以上是生活随笔為你收集整理的Skyscraper的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。