最小正整数
有一個整數,除5余3、除3余2、除2余1, 求滿足條件的最小正整數。
那么while(1)
其中1代表一個常量表達式,他永遠不會等于0。
所以,循環會一直執行下去。
除非你設置break等類似的跳出循環語句循環 才會中止
#include <iostream>
using namespace std;
int main( )
{ int x=1;
while(1)
{
if(x%5==3 && x%3==2 && x%2==1 )
{ x++; break;}
}
cout<<x<<endl;
system("pause");
return 1;
}
#include <iostream>
using namespace std;
int main( )
{ int x=1;
while(1)
{if(x%5==3 && x%3==2 && x%2==1 )
{ break;}
x++;
}
cout<<x<<endl;
system("pause");return 1;
}
最小正整數(續) 加速
#include <iostream>
using namespace std;
int main( )
{
int x=3;
while(1)
{
if(x%3==2 && x%2==1) break;
x+=5;
}
cout<<x<<endl;
system("pause");
return 0;
}
總結
- 上一篇: 菲亚特车怎么样 评测菲亚特车的性能和口碑
- 下一篇: Google alphabet是什么意思