用C ++中给定的头和腿数计算动物园中的动物数
输入-
heads = 60 legs = 200
输出-
Count of deers are: 40 Count of peacocks are: 20
说明-
let total number of deers to be : x Let total number of peacocks to be : y As head can be only one so first equation will be : x + y = 60 And deers have 4 legs and peacock have 2 legs so second equation will be : 4x + 2y = 200 Solving equations then it will be: 4(60 - y) + 2y = 200 240 - 4y + 2y = 200 y = 20 (Total count of peacocks) x = 40(Total count of heads - total count of peacocks)
输入-
heads = 80 Legs = 200
输出-
Count of deers are: 20 Count of peacocks are: 60
说明-
let total number of deers to be : x Let total number of peacocks to be : y As head can be only one so first equation will be : x + y = 80 And deers have 4 legs and peacock have 2 legs so second equation will be : 4x + 2y = 200 Solving equations then it will be: 4(80 - y) + 2y = 200 320 - 4y + 2y = 200 y = 60 (Total count of peacocks) x = 20(Total count of heads - total count of peacocks)
以下程序中使用的方法如下
输入动物园中头和腿的总数
创建一个函数来计算鹿的数量
在函数内部,将count设置为[(legs)-2*(heads))/2
返回计数
现在,通过从动物园的头部总数中减去鹿的总数来计算孔雀。
打印结果。
示例
#include <bits/stdc++.h>
using namespace std;
//计算鹿计数的函数
int count(int heads, int legs){
int count = 0;
count = ((legs)-2 * (heads))/2;
return count;
}
int main(){
int heads = 80;
int legs = 200;
int deers = count(heads, legs);
int peacocks = heads - deers;
cout<<"Count of deers are: "<<deers<< endl;
cout<<"Count of peacocks are: " <<peacocks<< endl;
return 0;
}输出结果
如果运行上面的代码,我们将获得以下输出-
Count of deers are: 20 Count of peacocks are: 60