您的位置:首页 > 其它

poj1289 The Cat in the Hat

2011-04-11 17:48 225 查看
参考:http://blog.sina.com.cn/s/blog_6635898a0100ik5c.html

题意: 实质就是有两个比分别为n和1/(n+1)的等比数列num和hei,它们的项数相等。num的首项,和hei的尾项均为1。现在只给出hei的 首项init_hei和num的尾项work_num,求出num的除去尾项的总和,以及num和hei各对应项乘积的总和。

思路:设项数为x,先按方程组(n+1)^x = init_hei, n^x = work_num解出n和x。然后按等比数列求和公式,求出num的除去尾项的总 和nowork_num。最后再逐项求和,求出height_sum。

学习点:等比数列相关知识

公差:


求和:




时,等比数列无限项之和

由于当

及 n 的值不断增加时,qn的值便会不断减少而且趋于0,因此无限项之和:



源代码:
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
double num_last, hei_first;
while (2 == scanf("%lf %lf", &hei_first, &num_last))
{
if (!hei_first && !num_last) break;
bool nFlag = false;
double f_num, f_hei;
int q = 0, item_num;
while (!nFlag)
{
item_num = 0;
q ++;
f_num = 1; f_hei = 1;
while (1)
{
if (f_num > num_last || f_hei > hei_first) break;
if (f_num == num_last && f_hei == hei_first)
{
nFlag = true;
break;
}
f_num *= q; f_hei *= (q + 1);
item_num ++;
}
}
double nowork;
if (q == 1) nowork = 0;
else nowork = (num_last - 1) / (q - 1);
double hei_sum = 0;
double hei = hei_first, num = 1;
for (int i = 0; i <= item_num; i ++)
{
hei_sum += (hei * num);
hei /= (q + 1);
num *= q;
}
printf("%.lf %.lf/n", nowork, hei_sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: