最小正整数

有一个整数,除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;
}

转载于:https://www.cnblogs.com/wc1903036673/p/3870559.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值