求助

P1304 哥德巴赫猜想

$j$ 为啥 $\leq \sqrt a$。。。$12=5+7$。
by Subset_Sum @ 2024-03-17 16:46:16


?
by youranzide @ 2024-03-17 16:47:21


@[youranzide](/user/1269604) 改成这样。。。 ``` #include<bits/stdc++.h> using namespace std; bool n(int num){ for(int i=2;i<=sqrt(num);i++){ if (num%i == 0) return false; } return true; } int main(){ int a; cin >> a; for(int i=4;i<=a;i+=2){ for(int j=2;j<=i-2;j++){ if (n(j)&&n(i-j)){ cout << i <<"=" << j << "+" << i-j << endl; break; } } } return 0; } ```
by Subset_Sum @ 2024-03-17 16:49:11


好的谢谢,不过12=5+7是什么意思啊
by youranzide @ 2024-03-17 16:52:49


@[youranzide](/user/1269604) 因为 $\sqrt{12}\le7$ 所以 $j$ 不应该只枚举到 $\sqrt{a}$
by kevinZ99 @ 2024-03-17 17:03:59


@[youranzide](/user/1269604) 根号a改成a/2 ```cpp #include<bits/stdc++.h> using namespace std; bool n(int num){ for(int i=2;i<=sqrt(num);i++){ if (num%i == 0) return false; } return true; } int main(){ int a; cin >> a; for(int i=4;i<=a;i+=2){ for(int j=2;j<=a / 2;j++){ if (n(j)&&n(i-j)){ cout << i <<"=" << j << "+" << i-j << endl; break; } } } return 0; } ```
by youcaiyoujuan @ 2024-03-17 20:31:52


|