• uva 107 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 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

    这道题开始没读懂,后来似乎懂了,但怎么也做不出来,看了别人的代码才知道开始只有一只猫,我原来以为开始的数量不确定。。。认真读题啊。

    后来也很无语,卡了一天,没什么算法,就是一个等比公式,没想到出现了各种bug。。。后来用了一个 ceil 函数很神奇地过了。没想到 log10() 这个函数相除的时候要用 ceil 。

    下面是做法:

    设开始的猫的高度是 h , N 是每个帽子里面猫的数量,设一个中间变量s。

    猫的高度  h  h / (N + 1)   h / (N + 1)2  …………   h / (N + 1)s

    猫的数量  1   N      N * N     …………   Ns

    h = (N + 1)s

    输入 h 和 x , 则 x = N。和上个式子联立消去中间变量s,得到 log10(h) * log10(N) = x * log10(N + 1)

    剩下的就是等比数列公式了,注意当 N = 1 的时候要讨论。

    最后得到:

    N == 1 时

    notwork = log2(h)   这里要注意,写代码的时候要用换底公式,并且可能因为精度的问题,要用 ceil 函数。

    sumheight = 2 * h - 1;

    N != 1 时

    notwork = (x - 1) / (N - 1)  

    sumheight = h * (N + 1) - x * N

    还有,修改代码的时候出现了各种问题,甚至连公式都打错了,浪费了大量的时间,所以出现错误的时候要淡定,不能慌,认真改,不能犯低级错误。

    今天因为这道题,,卡了很久,,浪费了很多时间啊。。。都是教训.

     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <cstdio>
     4 #include <cmath>
     5 double iabs(double a) {return a > 0 ? a : -a;}
     6 using namespace std;
     7 
     8 int main(void)
     9 {
    10     int h, x;
    11 
    12 #ifndef ONLINE_JUDGE
    13     freopen("at", "r", stdin);
    14     freopen("on", "w", stdout);
    15 #endif
    16     while (cin >> h >> x)
    17     {
    18         if (!h && !x)    break;
    19         int N; 
    20         int notwork, sumheight;
    21         if (x == 1)    
    22         {
    23             notwork = (ceil)(log(h) / log(2));    
    24             sumheight = 2 * h - 1;
    25         }
    26         else
    27         {
    28             for (N = 1; N < h; N++)    
    29             {
    30                 if (iabs(log(h)*log(N) - log(x)*log(N + 1)) < 1e-8)
    31                      break;
    32             }
    33             notwork = (x - 1) / (N - 1);
    34             sumheight = (N + 1) * h - x * N;
    35         }
    36         cout << notwork << ' ' << sumheight << endl;
    37     }
    38 
    39     return 0;
    40 }
  • 相关阅读:
    //设N是一个四位数,它的9倍恰好是其反序数(例如:1234 的反序数是4321),求N的值。
    安装oracle后,电脑变卡变慢的解决办法
    JSONP
    vue-resource发起get、post、jsonp请求
    Vue实例的生命周期
    自定义全局指令让文本框获取焦点
    自定义全局按键修饰符
    es2017 提供的针对字符串填充的函数:padStart、padEnd
    自定义私有过滤器
    fiddler教程
  • 原文地址:https://www.cnblogs.com/liuxueyang/p/2759494.html
Copyright © 2020-2023  润新知