Problem: 107 - The Cat in the Hat
Explain:
Given, Initial Height of a Cat, IH
The number of worker Cats of height 1, WC1
Objective:
Find, sum of all the cats' heights, SCH =?
Number of Not Working Cat, NWC =?
To understand this problem, Let IH = 216, WC1 = 125
“The smallest Cats are of height one;
These are the cats that get the work done;” [Copied from Uva 107]
If, IH = 1 and WC1 = 1,
This Indicate, TC = 1, NWC = 0
This also indicates the termination condition.
“The number of cats inside each (non-smallest) cat's hat is a constant, N. The height of these cats-in-a-hat is 1/(N+1) times the height of the cat whose hat they are in.” [Copied from Uva 107]
Let Constant N = 5,
Challenge:
Find N , i.e.
Nm = WC1 and
(N+1)m = IH
Code:
======
Explain:
Given, Initial Height of a Cat, IH
The number of worker Cats of height 1, WC1
Objective:
Find, sum of all the cats' heights, SCH =?
Number of Not Working Cat, NWC =?
To understand this problem, Let IH = 216, WC1 = 125
“The smallest Cats are of height one;
These are the cats that get the work done;” [Copied from Uva 107]
If, IH = 1 and WC1 = 1,
This Indicate, TC = 1, NWC = 0
This also indicates the termination condition.
“The number of cats inside each (non-smallest) cat's hat is a constant, N. The height of these cats-in-a-hat is 1/(N+1) times the height of the cat whose hat they are in.” [Copied from Uva 107]
Let Constant N = 5,
Not Working Cat ,NWC | Height of Each Cat, Given, IH = 216 | sum of all the cats' heights, SCH |
∑height = 216 | ||
1 | The number of cats inside each (non-smallest) cat's (height = 216) hat is 5, Height = (216/(5+1) = 36 | ∑height = (216/6)*5 + 216=396 |
1+5 | The number of cats inside each (non-smallest) cat's (height = 36) hat is 5, Height = 36/6 = 6 |
∑height = (216/62)*52 + 396 = 546 |
1+5+52 = 31 | The number of cats inside each (non-smallest) cat's (height = 36) hat is 5, Height = 36/6 = 1 |
∑height = (216/63)*53 + 546 =671 |
Break….. |
Challenge:
Find N , i.e.
Nm = WC1 and
(N+1)m = IH
Code:
======
#include<iostream> #include<cstdio> #include<cmath> #define ERROR 1e-8 using namespace std; int main(){ int H, num, m, N; #ifndef ONLINE_JUDGE freopen("a.in","r",stdin); #endif while( scanf( "%d%d", &H, &num ) != EOF && !(H == 0 && num == 0)){ if( H == 1 && num == 1 ){ printf( "0 1\n"); continue; } m = 1; while( H != (int)(pow(pow(num,1.0/m)+1.0, m)+ERROR) ) m++; N = (int)(pow(num,1.0/m)+ERROR); if( N != 1 ) printf( "%d %d\n", (1-num)/(1-N), (H-num)*N+H ); else printf( "%d %d\n", m, (H-num)*N+H ); } return 0; }
No comments:
Post a Comment