枚举法求解常见问题

完美立方:

//a的三次方等于a,b,c三个数的立方之和,这一组数称为完美立方

#includeusing namespace std;

int main()

{

int n;

cin >> n;

int count = 0;

for (int a = 2; a <= n; a++)

{

for (int b = 2; b < a; b++)

{

for (int c = b; c < a; c++)

{

for (int d = c; d < a; d++)

{

if (a*a*a == b*b*b + c*c*c + d*d*d)

{

printf("Cube=%d,Tripe=(%d,%d,%d)", a, b, c, d);

}

count++;

}

}

}

}

cout << "times:" << count;

}

求人体的生理高峰的日子:

#includeusing namespace std;

#define N 21252

int main()

{

//人体的体力智商情商高峰分别每23,28,33天出现一次,a,b,c分别是三次高峰出现的日子,求下一次高峰出现的日子

//int a, b, c;

//cin >> a >> b >> c;

//for (int i = 0; i < 21252; i++)          //最简单的枚举法

//{

// if ((i - a) % 23 == 0 && (i - b) % 28 == 0 && (i - c) % 33 == 0)

// {

// cout << i;

// }

//}

int p, e, i, d, caseNo = 0;

while (cin>>p>>e>>i>>d&&p!=-1)

{

++caseNo;

int k;

for (k = d + 1; (k - p) % 23; ++k);

for (; (k - e) % 28; k += 23);

for (; (k - i) % 33; k += 23 * 28);

cout << "case" << caseNo << "下一次高峰出现在" << k - d << "天后";

}

return 0;

}

分析出假币并判断出假币的重量:


#includeusing namespace std;

//若干硬币,有一枚假币,称量三次,找出假币

char Left[3][7];  //天平左边硬币

char Right[3][7];    //天平右边硬币

char Result[3][7];

bool IsFake(char c, bool light)  //light表示假币为轻,否则表示假币为重

{

for (int i = 0; i < 3; i++)

{

char *pLeft, *pRight;

if (light)

{

*pLeft = Left[0][0];

pRight = Right[i];

}

else {

pLeft = Left[i];

pRight = Right[i];

}

switch (Result[i][0])

{

case 'u':

if (strchr(pRight, c) == NULL)

{

return false;

}

break;

case 'e':

if (strchr(pLeft, c) || strchr(pRight, c))

{

return false;

}

break;

case 'd':

if (strchr(pLeft, c) == NULL)

{

return false;

}

break;

}

}

return true;

}

int main()

{

int t;

cin >> t;

while (t--)

{

for (int i = 0; i < 3; i++)

{

cin >> Left[i] >> Right[i] >> Result[i];

for (char c = 'A'; c <= 'L'; c++)

{

if (IsFake(c, true))

{

cout << c << "是假币并且比较轻" << endl;

}

else {

if (IsFake(c, false))

{

cout << c << "是假币并且比较重" << endl;

}

}

}

}

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容