现在的位置: 首页 > 综合 > 正文

107 – The Cat in the Hat

2018年04月23日 ⁄ 综合 ⁄ 共 2462字 ⁄ 字号 评论关闭

 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. Instead of doing the work alone, it decides to have its helper cats do the work. It keeps its (smaller) helper cats inside its hat. Each helper cat also
has helper cats in its own hat, and so on. Eventually, the cats reach a smallest size. These smallest cats 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. The height of these cats-in-a-hat is tex2html_wrap_inline35 times
the height of the cat whose 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 (of height one), find the number of cats that are not doing any work (cats of 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. Each specification is a single line consisting of two positive integers, separated by white space. The first integer is the height of the initial cat,
and the 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 not working, 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

这道题大意是:一开始有一只猫,高度为H,然后会变出n只小猫,高度为H/(1+n)。之后每只变出来的猫还会变出n只小猫,高度变为其的1/(1+n)。直到最后猫的高度达到1

告诉你一开始第一只猫的高度,以及最后高度为1的猫的数量,让你求所有高度大于1的猫,和所有猫的身高之和。

分析可以得到,(n+1)^k=H,n^k=Y(身高为1的猫的数量)。这道题没有告诉我们精度,所以一开始我以为数字会很大,所以就用了二分法来求k。。结果过了几组样例,可是wa了。

后来我看到别人说,数据都是在int范围内的。所以k的最大值也只有31.。所以可以从1一直枚举到31,总共也才这么多次。

主要就是在考一个精度的问题吧。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int main ()
{
    int x,y,n,k,i;
    while(cin>>x>>y)
    {
        long int sum=0,work=0;
        if (x==0) break;
        if (x==y && y==1)
        {
            cout<<"0 1"<<endl;
            continue;
        }
        for (k=1; x!=int (pow(pow(y,1.0/k)+1.0,k)+0.1); k++);
        n=(pow(y,1.0/k)+0.1);
        for (i=0; i<k; i++)
        {
            work+=pow(n,i);
            sum+=pow(n,i)*x;
            x=x/(1+n);
        }
        sum+=pow(n,k);
        cout<<work<<" "<<sum<<endl;
    }
    return 0;
}
【上篇】
【下篇】

抱歉!评论已关闭.