# 100~200之间求素数问题

• 147

``````#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
int x = 1, s  = 1;
for (int i = 101; i <= 200; i++){
s  = 1;
for (int j = 2; j < i; j++){
x = i%j;
s  = s *x;
if (s  = 0){
continue;
}
}
if (s ){
cout << i << " " ;
}
}
system("pause");
return 0;
}

``````

2 个回答
✓ 已被采纳
``s = s*x``

``s = x``

`if(s = 0)`错了吧。`if(s == 0)`,`break`才能结束内层循环。

``````#include <iostream>
#include <stdio.h>

using namespace std;

int main(){
int x = 1, s  = 1;
for (int i = 101; i <= 200; i++){
s  = 1;
for (int j = 2; j < i; j++){
s = 1;
x = i % j;
s  = s * x;
if (s  == 0){
break;
}
}
if (s){
cout << i << " " ;
}
}
//system("pause");
return 0;
}

``````

``````#include <iostream>
#include <stdio.h>
#include <bitset>

using namespace std;

int main(){
int x = 1, s  = 1;

//使用第一次内循环的s值做测试

//观察a = -2147483648乘上35的值的变化。
cout << "test\n";

int a = -2147483648;
cout << a << endl;
//观察a的二进制位
bitset<35> bit_a(a);
cout << bit_a << endl << endl;

//观察b的二进制位
long long b = a * 35;
cout << b << endl;
bitset<40> bit_b(b);
cout << bit_b << endl << endl;
//如果此时用int型保存b，会发生截断，这正是下面代码中s连乘过程中发生的

//观察int a = -2147483648 乘上34后发生的变化
long long c = a * 34;
cout << c << endl;
bitset<40> bit_c(c);
cout << bit_c << endl;//这时候截取为32位int型变量会变成0.

cout << "test\n";
//每次溢出后，都将截取32位作为结果保存在s中
//当s为0，if语句成立，跳出循环

for (int i = 101; i <= 200; i++){
s  = 1;
for (int j = 2; j < i; j++){
//s = 1;
x = i % j;
s  = s * x;
cout << x << ' ' << s << endl;
if (s  == 0){
break;
}
}
char q;
cin >> q;
if (s){
cout << i << " " ;
}
}
//system("pause");
return 0;
}
``````

###### 你尚未登录，登录后可以
• 和开发者交流问题的细节
• 关注并接收问题和回答的更新提醒
• 参与内容的编辑和改进，让解决方法与时俱进