标签:style blog http color strong for 2014 div
穷举法(for循环嵌套)
eg:
1 //100购物券,香皂2元,牙刷5元,洗发水15元,购物券不找零,每样至少买一个,哪个组合能吧100元正好花完 2 int count = 0; 3 for (int i = 1; i <= 40; i++)//香皂穷举 4 { 5 for (int j = 1; j <= 17; j++)//牙刷穷举 6 { 7 for (int k = 1; k <= 7; k++)//洗发水穷举 8 { 9 if (2 * i + 5 * j + 15 * k == 100)//判断总和是否正好等于100 10 { 11 Console.WriteLine("香皂{0}块,牙刷{1}只,洗发水{2}瓶", i, j, k); 12 count = count + 1;//所有种类的总数 13 } 14 } 15 } 16 } 17 Console.WriteLine(count);
输出结果为
eg:
标签:style blog http color strong for 2014 div
原文地址:http://www.cnblogs.com/zsmj001/p/3921533.html