当前位置:网站首页>Openjudge noi 2.1 1752: chicken and rabbit in the same cage

Openjudge noi 2.1 1752: chicken and rabbit in the same cage

2022-07-07 08:10:00 Jun Yi_ noip

【 Topic link 】

OpenJudge NOI 2.1 1752: Chicken and rabbit in the same cage

【 Topic test site 】

1. enumeration

【 Their thinking 】

solution 1: enumeration

With chicken x only , The rabbit y only , It is known that there is a One foot , So there are
2 x + 4 y = a 2x+4y=a 2x+4y=a
Chicken has at least 0 only , When all feet are chicken feet , Most chickens , Yes a 2 \frac{a}{2} 2a only , therefore 0 ≤ x ≤ a 2 0\le x \le \frac{a}{2} 0x2a
Rabbits have at least 0 only , At most ⌊ a 4 ⌋ \lfloor \frac{a}{4} \rfloor 4a only , therefore 0 ≤ y ≤ ⌊ a 4 ⌋ 0\le y \le \lfloor \frac{a}{4} \rfloor 0y4a
Summed up in :

  • enumerable object :x, y
  • Enumeration range : 0 ≤ x ≤ a 2 0\le x \le \frac{a}{2} 0x2a, 0 ≤ y ≤ ⌊ a 4 ⌋ 0\le y \le \lfloor \frac{a}{4} \rfloor 0y4a
  • Enumeration condition : 2 x + 4 y = a 2x+4y=a 2x+4y=a
    Select all that meet the conditions x And y in ,x+y Maximum and minimum values of .

solution 2: Looking for a regular

If the number of input feet is odd , Then there is no answer that meets the requirements .
When there are as many chickens as possible , Animals should be the most . When there are as many rabbits as possible , Animals should be the least .
Chicken up to a 2 \frac{a}{2} 2a, Then the animals are at most a 2 \frac{a}{2} 2a
Rabbits are at most ⌊ a 4 ⌋ \lfloor \frac{a}{4} \rfloor 4a, be left over a % 4 a\%4 a%4 Every foot is a chicken , Yes a % 4 2 \frac{a\%4}{2} 2a%4 only , At this time, the animals are at least , Yes ⌊ a 4 ⌋ + a % 4 2 \lfloor \frac{a}{4} \rfloor+\frac{a\%4}{2} 4a+2a%4 only .

【 Solution code 】

solution 1: enumeration

#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
int main()
{
    
    int a, mx = 0, mn = INF;
    cin >> a;
    for(int x = 0; x <= a/2; ++x)
        for(int y = 0; y <= a/4; ++y)
        {
    
            if(2*x+4*y == a)
            {
    // Update the maximum and minimum values 
                mx = max(mx, x+y); 
                mn = min(mn, x+y);
            }
        }
    if(mx == 0 && mn == INF)// If not found  
        cout << "0 0";
    else
        cout << mn << ' ' << mx;
    return 0;
}

solution 2: Looking for a regular

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    int a;
    cin >> a;
    if(a % 2 == 1) 
        cout << "0 0";
    else
        cout << a/4+a%4/2 << ' ' << a/2;
    return 0;
}
原网站

版权声明
本文为[Jun Yi_ noip]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070445375306.html