最小正整数
有一個(gè)整數(shù),除5余3、除3余2、除2余1, 求滿足條件的最小正整數(shù)。
那么while(1)
其中1代表一個(gè)常量表達(dá)式,他永遠(yuǎn)不會(huì)等于0。
所以,循環(huán)會(huì)一直執(zhí)行下去。
除非你設(shè)置break等類似的跳出循環(huán)語(yǔ)句循環(huán) 才會(huì)中止
#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é)
- 上一篇: 菲亚特车怎么样 评测菲亚特车的性能和口碑
- 下一篇: 怎么创建具有真实纹理的CG场景岩石?