趣味算法入门
1.百钱百鸡问题
| #include<iostream> |
| using namespace std; |
| int main() |
| { |
| int x, y, z; |
| for ( x = 0; x <= 20; x++) |
| { |
| for (y = 0; y <= 33; y++) |
| { |
| z = 100 - x - y; |
| if ((5 * x + 3 * y + z / 3 == 100) && (x + y + z == 100)) |
| { |
| cout << "公鸡:" << x ; |
| cout << "母鸡:" << y; |
| cout << "小鸡:" << z << endl; |
| } |
| |
| } |
| } |
| } |
2.借书方案
| #include<iostream> |
| using namespace std; |
| int main() |
| { |
| int a, b, c,i=0; |
| for (a = 1; a <= 5; a++) |
| { |
| for (b = 1; b <= 5 && a != b; b++) |
| { |
| for (c = 1; c <= 5; c++) |
| { |
| if (a != c && b != c) |
| { |
| cout << "A: " << a << " B;" << b << " C: " << c << " | "; |
| i++; |
| } |
| if (i % 4 == 0) |
| { |
| cout << endl; |
| } |
| } |
| } |
| } |
| cout << "一共有" << i << "种" << endl; |
| } |
3.打鱼还是晒网
| #include<iostream> |
| using namespace std; |
| struct Date |
| { |
| int year; |
| int month; |
| int day; |
| }; |
| bool runyear(int year) |
| { |
| if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) |
| { |
| return true; |
| } |
| return false; |
| } |
| int countdat(Date& d) |
| { |
| int Month[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; |
| int totalday = 0; |
| |
| |
| |
| for (int year = 1990; year < d.year; year++) |
| { |
| if (runyear(year)) |
| { |
| totalday += 366; |
| } |
| else |
| { |
| totalday += 365; |
| } |
| } |
| |
| |
| if (runyear(d.year)) { Month[2]++; } |
| for (int i = 1; i < d.month; i++) |
| { |
| totalday += Month[i]; |
| } |
| |
| totalday += d.day; |
| |
| |
| return totalday; |
| } |
| |
| int main() |
| { |
| Date d; |
| int totalday, result; |
| cout << "请输入年月日:"; |
| cin >> d.year >> d.month >> d.day; |
| totalday = countdat(d); |
| result = totalday % 5; |
| if (result > 0 && result < 4) |
| { |
| cout << "今天打渔" << endl; |
| } |
| else |
| { |
| cout << "今天晒网" << endl; |
| } |
| } |
抓交通肇事犯
一脸卡车撞人逃跑,三人目击,但没有记住车牌号,之记下了车号的特征。甲:牌照前两位数是相同的 乙:牌照的后两位是也是相同的,但是与前两位不同 丙:他是数学家,四位的车号刚好是一个整数的平方。
请求出车牌号
| #include<iostream> |
| using namespace std; |
| |
| int main() |
| { |
| int i, j,k, temp; |
| int flag = 0; |
| for (i = 1; i <= 9; i++) |
| { |
| if (flag) { break; } |
| for (j = 1; j <= 9; j++) |
| { |
| if (flag) { break; } |
| if (i != j) |
| { |
| k = 1000 * i + 100 * i + 10 * j + j; |
| for (temp = 31; temp <= 99; temp++) |
| { |
| if (k == temp * temp) |
| { |
| cout << "车牌号为:" << k << endl; |
| flag = 1; |
| break; |
| } |
| } |
| } |
| |
| } |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?