C语言趣味编程100题

1,百钱买百鸡问题

复制代码
 1 #include <iostream> 
 2 using namespace std;
 3 int main()
 4 {
 5     int x,y,z;
 6     for(int x=0;x<=20;x++)
 7     {
 8         for(int y=0;y<=33;y++)
 9         {
10             for(int z=0;z<=100;z++)
11             {
12                 if(x+y+z==100 && 5*x+3*y+(1.0/3)*z==100)
13                 cout<<x<<" "<<y<<" "<<z<<endl;
14             }
15         }
16         
17     }    
18 }
复制代码

进一步提高代码效率,确定公鸡和母鸡后,边确定了小鸡,所以只用两层循环便可解决问题。

复制代码
 1 #include <iostream> 
 2 using namespace std;
 3 int main()
 4 {
 5     int x,y,z;
 6     for(int x=0;x<=20;x++)
 7     {
 8         for(int y=0;y<=33;y++)
 9         {
10             z=100-x-y;
11             if(5*x+y*3+(1.0/3)*z==100)
12             {
13                 cout<<x<<" "<<y<<" "<<z<<endl;
14             }
15         }
16         
17     }    
18 }
复制代码

借书方案知多少

复制代码
 1 #include <iostream> 
 2 using namespace std;
 3 int main()
 4 {
 5     int x,y,z,count=0;
 6     for(int x=1;x<=5;x++)
 7     {
 8         for(int y=1;y<=5;y++)
 9         {
10             for(int z=1;z<=5&&x!=y;z++)
11             {
12                 if(y!=z&&x!=z)
13                 {
14                     count++;
15                 }
16             }
17            
18         }
19     }
20     cout<<count<<endl;
21     return 0;
22 }
复制代码

 

posted @   新晋软工小白  阅读(456)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤
点击右上角即可分享
微信分享提示