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. 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 题目意思就是给出一只猫 的高度。它的帽子里能变出 n只猫 每只猫是原来高度的 1 / (n + 1);。 然后这n只又能从帽子变出n只小猫 高度1/(n+1) 知道变出高度为 1的 猫。。 题目的两个输入 是最开始那只猫高度,和最终高度为1的猫的只数。。求高度不是1的猫有多少只,还有所有猫总高度。。 题目总要是考数学,找规律拿第一个例子说 有 n的k次方 = 125 (n + 1)的k次方 =216;算出k和n就可以用等比求和来求 但可能是我算法不是很好,在开头分出了几种特殊情况 AC代码:#include<stdio.h> #include<math.h> #include<iostream> using namespace std; int main () { double x,y; double n; double k; double t = 0; double total = 0; while (cin >> x >>y ) { if ( x == 0 && y == 0) break; if (x == 1 && y == 1) { printf ("0 1\n"); continue; } if (x == 2 && y ==1) { printf("1 3\n"); continue; } if (y == 1){ for(;x != 1;) { total += x; t++; x /= 2; } total += 1; printf("%.0lf %.0lf\n",t,total); t = 0; total = 0; continue; } if (y == x -1) { printf("1 %.0lf\n",x + y); continue; } for ( k = 2;;k++) { if ((pow(x,1.0/k) - 1) - pow(y,1.0/k) < 0.000000001) { n = pow (y,1.0/k); break; } } double n1 = ( n * (1 - pow(n,k-1)) / (1 - n) )+ 1; double n2 = ( x * (1 - pow(n/(n+1),k + 1) ) / (1 - n/(n + 1)) ); printf("%.0lf %.0lf\n",n1,n2) ; } return 0; }