zoukankan      html  css  js  c++  java
  • 杂题 UVAoj 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

      
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 using namespace std;
     6 int main()
     7 {
     8     int H,B; 
     9     while(~scanf("%d%d",&H,&B))
    10     {
    11         if(!(H|B))break;
    12         
    13         if(H==1)
    14         {
    15             printf("%d %d
    ",0,1);
    16             continue;
    17         }
    18         int k;
    19         for(k=1;;k++)
    20             if((int)(0.5+pow(pow(B*1.0,1.0/k)+1.0,k))>=H)
    21                 break;
    22         int N=(int)(0.5+pow(B*1.0,1.0/k)),ans1=0,ans2=0;
    23         for(int i=0;i<k;i++)
    24         {
    25             ans1+=(int)(0.5+pow(N*1.0,i*1.0));
    26             ans2+=((int)(0.5+pow(N*1.0,i*1.0))/pow(N+1.0,i)*H);
    27         }
    28         ans2+=B;
    29         printf("%d %d
    ",ans1,ans2);
    30     } 
    31     return 0;
    32 }
    尽最大的努力,做最好的自己!
  • 相关阅读:
    交换排序------冒泡法 及其优化
    [编程题]最大和子矩阵
    [编程题] N阶楼梯上楼问题
    2017年东北大学计算机专业考博 面试编程题(某教授 实验室)
    幸运的袋子 (牛客网 16年网易内推 编程题)
    OpenvSwitch代码分析之bridge和port
    阅读书籍---程序员必读系列
    嵌入式开发之davinci--- 8168 电源调试总结
    嵌入式开发之davinci--- 8148/8168/8127 中的添加算饭scd 场景检测 文档简介
    嵌入式开发之davinci--- 8148/8168/8127 中的图像缩放sclr、swms之后出现图像视频卡顿、屏幕跳跃的问题
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5249870.html
Copyright © 2011-2022 走看看