Cpp class exercise 3:修订间差异
外观
| 第158行: | 第158行: | ||
=== break、continue语句 === | === break、continue语句 === | ||
【例3.4】质数的判断 | |||
<syntaxhighlight lang="C++" line> | |||
#include <iostream> | |||
using namespace std; | |||
int main() | |||
{ | |||
int n =0; | |||
int count=0;// | |||
int flag = 1; //1: 是质数 0:不是质数 | |||
cout << "请输入一个大于2的整数:" <<endl; | |||
cin >> n; | |||
for(int i=2;i<n;i++) | |||
{ | |||
count++; | |||
if(n % i == 0) | |||
{ | |||
flag = 0; | |||
break; | |||
} | |||
} | |||
cout << "运行了" << count << "次判断" <<endl; | |||
if(flag == 1) | |||
cout << n << "是质数" <<endl; | |||
else | |||
cout << n << "不是质数" <<endl; | |||
return 0; | |||
} | |||
</syntaxhighlight> | |||