最小正整数
有一個整數(shù),除5余3、除3余2、除2余1, 求滿足條件的最小正整數(shù)。
那么while(1)
其中1代表一個常量表達式,他永遠不會等于0。
所以,循環(huán)會一直執(zhí)行下去。
除非你設置break等類似的跳出循環(huán)語句循環(huán) 才會中止
#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;
}
最小正整數(shù)(續(xù)) 加速
#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;
}
總結(jié)
- 上一篇: 菲亚特车怎么样 评测菲亚特车的性能和口碑
- 下一篇: 前进轮胎怎么样 评测前进轮胎的性能和质量