he Cat in the Hat
he 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 times the height of the cat whose hat they are in.
The smallest cats are of height one;All heights are positive integers.
these are the cats that get the work done.
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
这道题目做下来,看了看提交的 ,提交了有20多遍,真是折磨死我了。这个题目只要是读懂题目,就明白了是怎么回事,但是很容易出现问题,问题出现在以下几个方面了:
1: 根据输入的数据在判断一共猫排了多少行的时候,别根据一个数据来算,要利用另一个数据,虽说 其中的任何一个数据都能确定有多少行,但是这样得出的结果不会是唯一的,而我们常常在得到一个 结 果后就break了,而这个行数可能不能满足另一个数据,这两个数据是相互制约的,因此在得出结 果后 要加一个判断看这个结果是不是满足另一个数据,满足了才break,不满足继续循环。
2: 这道题目卡的精度真是很死 ,以前在对double数据进行强制转换的时候只是(double)a 这样,但 这道题目这样是不行的,在对double型数据进行强制转换的时候,要加0.1,再对其进行强制转换。
#include <stdio.h> #include <math.h> int main() { int i,j,n,m,s,k; int x,y,s1,s2; double t; while(scanf("%d %d",&n,&m)!=EOF) { if(n==0&&m==0) { break; } s1=s2=0; for(x=1;;x++) { t=log((double)n)/log((double)(x+1)); if(fabs(t-(int)(t+0.1))<=1e-7) { y=(int)(t+0.1); if((double)m-pow((double)x,(double)y)<=1e-7) { break; } } } k=1; while(n!=1) { s1+=k; s2+=k*n; n=n/(x+1); k=k*x; } s2+=k; printf("%d %d\n",s1,s2); } return 0; }