UVA107 - The Cat in the Hat

精度控制+查找

题目:

The Cat in the Hat

 

 The Cat in the Hat 

 

Background

(An homage to Theodore Seuss Geisel)

The Cat in the Hat is a nasty creature,
But the striped hat he is wearing has a rather nifty feature.

With one flick of his wrist he pops his top off.

Do you know what's inside that Cat's hat?
A bunch of small cats, each with its own striped hat.

Each little cat does the same as line three,
All except the littlest ones, who just say ``Why me?''

Because the littlest cats have to clean all the grime,
And they're tired of doing it time after time!

 

The Problem

A clever cat walks into a messy room which he needs to clean. Insteadof doing the work alone, it decides to have its helper cats do thework. It keeps its (smaller) helper cats insideits hat. Each helper cat also has helper cats in its own hat,and so on. Eventually, the cats reach a smallest size. These smallestcats have no additional cats in their hats.These unfortunate smallest cats have to do the cleaning.

The number of cats inside each (non-smallest)cat's hat is a constant, N. Theheight of these cats-in-a-hat is tex2html_wrap_inline35 times the height of the catwhose hat they are in.

The smallest cats are of height one;
these are the cats that get the work done.

All heights are positive integers.

Given the height of the initial cat and the number of worker cats (ofheight one), find the number of cats that are not doing any work (catsof height greater than one) and also determine the sum of all the cats'heights (the height of a stack of all cats standing one on top of another).

 

The Input

The input consists of a sequence of cat-in-hat specifications. Eachspecification is a single line consisting of two positive integers,separated bywhite space. The first integer is the height of the initial cat, andthe second integer is the number of worker cats.

A pair of 0's on a line indicates the end of input.

 

The Output

For each input line (cat-in-hat specification),print the number of cats that are notworking, followed by a space, followed by the height of the stack of cats.There should be one output line for each input line other than the ``0 0''that terminates input.

 

Sample Input

 

216 125
5764801 1679616
0 0

 

Sample Output

 

31 671
335923 30275911

解答:

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
	int h,m;
	while(scanf("%d%d",&h,&m)!=EOF)
	{
		int n=1,k;
		if(h==0&&m==0)
			break;
		while(fabs((log((double)m)/log((double)h))-(log(double(n))/log((double)(n+1))))>1e-9)
			n++;
		k=(int)(0.5+log((double)h)/log((double)(n+1)));
		if(n==1)
			printf("%d %d\n",k,(int)(0.5+h*(1-pow(((double)n/(1+n)),k+1))/(double)(1-((double)n)/(1+n))));
		else
			printf("%d %d\n",(int)(0.5+(1-pow(n,k))/(1-n)),(int)(0.5+h*(1-pow(((double)n/(1+n)),k+1))/(double)(1-((double)n)/(1+n))));
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值