一.枚举
例题一 百鸡问题
#include<bits/stdc++.h> using namespace std; int main(){ int n; while (cin>>n) { for (int i = 0; i <= 100; i++) { for (int j = 0; j <= 100 - i; j++) { int m = 100 - i - j; if (i * 5 + 3 * j + (double) 1 / 3 * m > n) break; else cout << "x=" << i << ",y=" << j << ",z=" << m << endl; } } } return 0; }
例题二 老比尔
N _XYZ_,N是火鸡的数量,现在不知道价格的第五位和第一位,求一只火鸡的价格
有多个满足条件的价格时选择价格最高的。
假设一只火鸡的价格为整数,总价为5位数
#include <bits/stdc++.h> using namespace std; int main(void){ int n, a, b, c, d, e, bcd, abcde; while(cin >>n >>a >>b >>c){ bool flag = false; bcd = a*1000 + b*100 +c*10; for(int a=9; a>=1; a--){//倒着搜索,保证是最大值 for(int e=9; e>=0; e--){ abcde = a*10000 + bcd + e; if(abcde%n==0){ flag = true; printf("%d %d %d\n", a, e, abcde/n); break; } } if(abcde%n==0) break; } if(!flag) cout <<0 <<endl; //未找到返回0 } return 0; }