鸡兔同笼pTA显示部分正确

img


pTA显示部分正确

题目看一下

公式错了
应为c = ( 4 * a - b ) / 2;

img


该题可以使用枚举法
代码:


#include <stdio.h>

int main() {
    int c, r, a, b;
    scanf("%d%d", &a, &b);

    for (c = 1; c < a; c++) {
        for (r = 1; r < a; r++) {
            if ((c + r == a ) && (4 * c + 2 * r == b)) {
                printf("鸡有%d只,兔有%d只\n", c, r);
            }
        }
    }

    return 0;
}

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