#include <iostream>
using namespace std;
int main()
{
int p, e, i, d;
int nCase = 0;
while( cin >> p >> e >> i >> d )
{
if ( p == -1 && e == -1 && i == -1 && d == -1 )
{
break;
}
p %= 23;
e %= 28;
i %= 33;
while ( i <= d )
{
i += 33;
}
while ( true )
{
if ( ( (i - p) % 23 == 0 ) && ( (i - e) % 28 == 0 ) )
{
break;
}
i += 33;
}
++nCase;
cout << "Case " << nCase << ": the next triple peak occurs in " << i - d << " days." << endl;
}
return 0;
}
参考文章:http://blog.sina.com.cn/s/blog_7c9b29d50100t7st.html
在问题的数学模型中有多个条件需要满足时,可以采用逐步减小搜索空间的方法提高计算的效率。一次按照条件一、条件二、······、进行搜索。在最初的搜索空间上按条件一进行判定。除最后一次外,每次搜索都找到符合当前条件的全部答案,将他们作为下一个条件判定的搜索空间。
#include <iostream>
using namespace std;
int main()
{
int p, e, i, d;
int nCase = 0;
while( cin >> p >> e >> i >> d )
{
if ( p == -1 && e == -1 && i == -1 && d == -1 )
{
break;
}
int k = d + 1;
for ( ; k <= 21252; ++k ) if ( ( k - p ) % 23 == 0 ) break;
for ( ; k <= 21252; k += 23 ) if ( ( k - e ) % 28 == 0 ) break;
for( ; k <= 21252; k += 23*28 ) if ( ( k - i ) % 33 == 0 ) break;
++nCase;
cout << "Case " << nCase << ": the next triple peak occurs in " << k - d << " days." << endl;
}
return 0;
}
作者:山丘儿
转载请标明出处,谢谢。原文地址:http://blog.csdn.net/s634772208/article/details/46560511