22 条题解

  • 25
    @ 2023-7-14 20:35:35

    事先声明:我没抄任何人,不是整合,代码都是我自己写的,方法也是我在网上查的,没看任何人的。


    第一种方法:假设法

    #include <iostream>
    using namespace std;
    int main()
    {
        int head,foot,a;
        cin >> head >> foot;
        a = 4 * head;
        a -= foot;
        a /= (4 - 2);
        cout << a << " " << (head - a);
    
        return 0;
    }
    

    解析:

    假设所有头都是兔子的,那么脚应有4 head 只。我们用 4 head 减去 foot,得出数量差,因为我们把一些鸽鸽假设成了兔子,少了两只鸽鸽的玉足,所以拿差除以兔子脚与鸽鸽玉足的差(4-2)即可得出鸽鸽的数量,兔子数量就是 head 减鸽鸽数了。


    第二种方法:凌迟法

    #include <iostream>
    using namespace std;
    int main()
    {
        int head,foot,a;
        cin >> head >> foot;
        a = foot - head - head;
        a /= 2;
        cout << (head - a) << " " << a;
    
        return 0;
    }
    

    解析:

    我们先砍掉所有Chickens and rabbits的一只 jio(即foot - head),我们忍不住再砍一只 jio,(foot - head)这个时候鸡已经一屁股坐在了地上,兔子都半身不遂了。我们将剩下的脚数除以 2 就可以得到兔子数,鸡就是总数减兔子数了。


    第三种方法:枚举法

    解析:

    #include <iostream>
    using namespace std;
    int main()
    {
        int head,foot;
        cin >> head >> foot;
        for (int i = 1;i <= head;i++)
        {
            if((2 * i) + 4 * (head - i) == foot)
            {
                cout << i << " " << (head - i);
            }
        }
    
        return 0;
    }
    

    解析:

    用 for 循环将鸡的数量,(循环变量是 i ) 挨个从1设置到head,那么兔子的数量就是 (head - i)。然后判断 { ( 2 * i ) + [ 4 * ( head - i ) ] } 的和是否等于 foot ,是就输出。

    重要的事情说三遍:点赞!👍 点赞!👍 点赞!👍

    一定要点赞啊,写题解chiao慢~的啊!(T^T)
    • @ 2023-7-16 15:59:07

      hhh看你的题解我忍不住笑出了声,题解写得好,解释的很清楚,点赞啦,我们一起加油鸭

  • 6
    @ 2023-7-11 21:18:12

    解法三(我家哥哥脚无法):

    假设鸡和兔都沙雕两只脚,那么地上还剩foot-(head×2)=X(只)脚,全是兔子的脚,每只兔子还有两只脚幸存。X÷2=10(只)就是兔子的数量,head-raddit=Y(只)就是鸡的数量。

    此法对我家哥哥有点残忍,不建议使用👀️ UP主 羊了个强

    #include <iostream>
    using namespace std;
    int main()
    {
        int head,foot,check,rabbit;
        cin>>head>>foot;
        rabbit=(foot-(head*2))/2;
        check=head-rabbit;
        cout<<check<<" "<<rabbit;
        return 0;
    }
    
  • 4
    @ 2024-5-12 12:03:43

    根本不用循环好吧!

    设兔的数量为x,鸡的数量为y 定义总头数为h,总脚数为f 由题意得,x+y=h ① 4x+2y=f ② ①乘2得,2x+2y=2h ③ ②-③,得2x=f-2h,所以x=(f-2h)/2 将x=(f-2h)/2代入①得,y=h-(f-2h)/2. 所以代码如下: (要记得在2和h之间加*哦)

    #include<iostream>
    using namespace std;
    int main()
    {
        int h,f;
        cin>>h>>f;
        cout<<h-(f-2*h)/2<<' '<<(f-2*h)/2;
        return 0;
    }
    

    so easy!

    点个赞吧,球球惹

    • 3
      @ 2024-4-14 17:23:35
      #include<iostream>
      using namespace std;
      int main()
      {
          long long head,foot,ji,tu;
          cin >> head >> foot;
          tu=4*head;
          ji=(tu-foot)/2;
          tu=head-ji;
          cout << ji << " " << tu;
          return 0;
      }
      假设法
      
      • 2
        @ 2023-11-4 13:44:00

        我这里选择的是枚举法

        #include <iostream>
        using namespace std;
        int main()
        {
            int head,foot;
            cin >> head >> foot;
            for (int i = 1;i <= head;i++)
            {
                if((2 * i) + 4 * (head - i) == foot)
                {
                    cout << i << " " << (head - i);
                }
            }
        
            return 0;
        }
        
        
        • 1
          @ 2023-11-19 21:28:07

          #include <iostream> using namespace std; int main() { int head , foot , j , t; cin >> head >> foot; t = (foot - head * 2) / 2; j = head - t; cout << j << " " << t; return 0; }

          • 1
            @ 2023-8-5 14:28:52

            这道题灰常灰常经典,由于我在学校卷子上一般都给表格所以把我坑懵了,如今看到这道题(虽然标签是循环结构)我就不想用枚举法了(要用吐了),咱们还是用代码和算式写起来都很方便的假设法吧~ 假设法,在我们班又称“土豪算法”。有两种,一种是全🐔法,一种是全🐰法,道理都差不多。比如说全🐔法,先把🐰的两条腿砍了做红烧兔腿(开玩笑,兔兔这么可爱怎么可以吃兔兔),现在所有的小animal都只有两条腿了,和🐔一样。这时,缺的腿就是🐰的。(全🐰法的话就是多出来的腿是🐔的)每只🐰多出来两条腿,所以腿除以二就是🐰的数量,剩下的就是🐔的数量,代码如下(估计应该是全题解最简代码):

            #include <bits/stdc++.h>
            int h,f;
            int main()
            {
                std::cin>>h>>f;
                std::cout<<h-((f-h*2)/2)<<" "<<(f-h*2)/2;
                return 0;
            }
            

            可能看起来有点抽象,那咱们就还原一下算式(样例):

            //今有雉兔同笼,上有三十头,下有九十足,问雉兔各有几何?
            30×2=60(只)//假设全是🐔, 一共有60只足;
            90-60=30(只)//少的脚就是🐰的
            兔: 30÷2=15(只)//每只🐰少了两只脚, 脚的数量除以二就是🐰的数量
            鸡: 30-15=15(只)//剩下的就是🐔
            

            讲解完毕~ 本人刚刚学到L9, 代码有什么不足的地方请各位老师们以及同学们指出. 最后感谢大家的点赞支持❤️

            • @ 2024-2-3 13:22:08

              最简代码?非也非也。return 0;在主函数的结尾是可以去掉的。bits/stdc++.h换成iostream,字符更少。“ ”可以换成‘ ’,少按两次Shift。#include <>换成#include<>,少按一个空格。h-((f-h2)/2)外面的括号没用,换成h-(f-h2)/2。最后全不换行,少按几次Enter(这样在<>后要加个空格)。 不过代码整体看起来对懒人略有点不友好还是挺好的

            • @ 2024-2-3 13:25:24

              不对,那个<>的换行不删,忘了

          • 1
            @ 2023-7-14 21:10:56
            #include <iostream>
            using namespace std;
            int main()
            {
                int a,b,c,d;//a是头,b是脚。c和d不好形容。
                cin>>a>>b;
                c=a*4-b;//假设全是兔。
                d=c/2; //d=c/(4/2)
                cout<<d<<" "<<a-d;
            }
            

            至于为什么不用枚举法嘛。懒得写!

            • 1
              @ 2023-7-13 11:22:12
              #include <iostream>
              using namespace std;
              int main ()
              { 
                  int h,f;
                  cin>>h>>f;
                  for (int x=0;x<=h;x++)//设有x只坤
                  {
                      if (2*x+4*(h-x)==f)//如果坤和兔的总腿数=f,就输出
                      {
                          cout<<x<<" "<<h-x;
                      }
                      else//不等于则跳过此次循环
                      {
                          continue;
                      }
                  }
                  return 0;
              }
              
              • 1
                @ 2023-7-12 17:54:05
                #include <iostream>
                using namespace std;
                
                int main()
                {
                    int n, m, js, ts = 0;
                    cin >> n >> m;
                    for (int i = 1; i <= n; i++)
                    {
                        for (int zy = 1; zy <= i; zy++)
                        {
                            for (int tz = 1; tz <= i; tz++)
                            {
                                if (zy + tz == n && tz * 4 + zy * 2 == m)//如果坤头加兔头等于题目头数,并且鸡脚加兔脚等于题目脚数
                                {
                                    js = zy;
                                    ts = tz;
                                    break;
                                }
                            }
                        }
                    }
                    cout << js << " " << ts;
                    return 0;
                }
                
                • 1
                  @ 2023-7-11 23:25:33

                  看了一圈发现没有用一层循环的,那我发个一层循环的吧

                  首先,由题目可知,鸡的数量+兔的数量=head

                  所以我们只需要枚举鸡或者兔的数量就好了

                  因为题目说了:保证头和脚的个数符合鸡、兔的规律。所以我们不需要考虑无解的情况

                  最后在for循环里写个判断:如果 鸡的数量乘2(鸡的腿的数量)+ 兔的数量乘4(兔的腿的数量)=foot,直接输出鸡和兔的数量,然后return 0;

                  #include <iostream>
                  using namespace std;
                  
                  int main()
                  {
                      int heads,feet;
                      cin >> heads >> feet;
                      for(int i = 0;i <= heads;i++)
                      {
                          int chicken = i;
                          int rabits = heads - chicken;
                          if(chicken * 2 + rabits * 4 == feet)
                          {
                              cout << chicken << ' ' << rabits;
                              return 0;
                          }
                      }
                  }
                  
                  • 1
                    @ 2023-7-11 21:51:26
                    #include <iostream>
                    using namespace std;
                    int main()
                    {
                        long long h,f;
                        cin>>h>>f;
                        cout<<(h*4-f)/2<<" "<<h-(h*4-f)/2;
                        return 0;
                    }//应该是最短代码了(return 0不算)
                    
                  • 1
                    @ 2023-7-11 11:36:04

                    解析

                    问题大意:存在一群鸡和兔子,它们头加起来head个,脚加起来foot个。

                    从头加起来head个可知,一共有head个动物。

                    以此可以枚举鸡和兔子的所有情况,也就是外循环i枚举鸡的数量,内循环j枚举兔子的数量。

                    那么当i * 2 + j * 4等于foot,代表一种可能情况。

                    题解

                    #include <iostream>
                    using namespace std;
                    int main()
                    {
                        int h, f;
                        cin >> h >> f;
                        for (int i = 1; i <= h; i++)
                        {
                            for (int j = 1; j <= h; j++)
                            {
                                if (i + j == h && 2 * i + 4 * j == f)
                                {
                                    cout << i << " " << j << endl;
                                }
                            }
                        }
                        return 0;
                    }
                    
                    
                    • 0
                      @ 2024-6-9 17:53:16

                      题解 c++

                      #include <bits/stdc++.h> //hetao4040809
                      using namespace std;
                      int h,f,j,t;
                      int main()
                      {
                          cin>>h>>f;
                          t=(f-2*h)/2;
                          j=h-t;
                          cout<<j<<" "<<t;
                          return 0;
                      }
                      
                      • 0
                        @ 2023-7-11 20:53:58
                        #include<iostream>
                        using namespace std;
                        int main()
                        {
                            int head,foot,ji,tu;
                            cin>>head>>foot;
                            tu=foot/2-head;
                            ji=head-tu;
                            //一步到位
                            cout<<ji<<' '<<tu;
                        }
                        

                        偷工减料,但是对 真•轻量版 人脑更好编译:)

                        • -1
                          @ 2023-7-25 15:37:51
                          #include <iostream>
                          using namespace std;
                          int main()
                          {
                              int a,b;//a头,b脚
                              cin >> a >> b;
                              int c = b - 2 * a;//设全部为鸡,则多出的脚,多出的脚再乘2(因为兔是4只脚)就是鸡的脚
                              int d = b - 2 * c;//全部的脚减去鸡的脚就是兔子的脚 
                              cout << d/2 << " " << (c/2);
                              return 0;
                          }
                          
                          • -1
                            @ 2023-7-12 13:41:40
                            #include <iostream>
                            using namespace std;
                            int main()
                            {
                                int a,s;
                                cin>>a>>s;
                                for (int i=1;i<=a;i++)
                                {
                                    if(i*2+(a-i)*4==s)
                                    {
                                        cout<<i<<" "<<a-i;
                                        break;
                                    }
                                }
                                return 0;
                            }
                            
                            • -1
                              @ 2023-7-11 18:32:05
                              #include "iostream"
                              using namespace std;
                              int main()
                              {
                                  int n,m;
                                  cin >> n >> m;
                                  cout <<(4*n-m)/2 << ' ' << n-(4*n-m)/2;   // 直接运用公式输出简单又快速
                                  return 0;
                              }
                              

                              运用公式法求解,效率和人脑编译快的很

                              • @ 2023-7-11 21:53:37

                                ?嗯……的…… 内啥…… 我真的是发完了题解才看到你的方法的! 相信我! 我真不是抄你的!

                            • -2
                              @ 2024-6-7 22:36:16
                              #include<iostream>
                              using namespace std;
                              int main()
                              {
                                  int h,f;
                                  cin >> h >> f;
                                  int ifChicken = h * 2;
                                  int rabbit = (f - ifChicken) / 2;
                                  int chicken = h - rabbit;
                                  cout << chicken << " " << rabbit;
                                  return 0;
                              }
                              
                              • -2
                                @ 2024-5-24 21:22:54
                                #include <iostream>
                                using namespace std;
                                int main()
                                {
                                    int head,foot;
                                    cin>>head>>foot;
                                    int a=0,b=0;
                                    a=head*4-foot;
                                    a=a/2.0;
                                    b=head-a;
                                    cout<<a<<" "<<b;
                                    return 0;
                                }
                                

                                信息

                                ID
                                283
                                时间
                                1000ms
                                内存
                                256MiB
                                难度
                                6
                                标签
                                递交数
                                7546
                                已通过
                                2363
                                上传者