#12649: 為什麼會溢位?


acer12340306 (charleschen)

學校 : 國立臺中第一高級中學
編號 : 58352
來源 : [111.252.176.177]
最後登入時間 :
2020-01-09 22:43:34
a740. 质因数之和 -- 海豚原创 | From: [1.170.234.244] | 發表日期 : 2017-09-02 20:32

#include<iostream>
#include<vector>
using namespace std;
bool prime[20000000];
int main(){
int x , total = 0;
vector<int> array;
for(int i = 0;i < 20000000; i++){
prime[i] = true;
}
for(int i = 2; i < 20000000; i++){
if(prime[i]){
for(int j = i * 2; j < 20000000; j += i){
prime[j] = false;
}
array.push_back(i);
}
}
prime[0] = false;
prime[1] = false;

while(cin >> x){
for(int i = 0 ;array[i] <= x ; i++){        //條件如果改成 i < array.size() 就AC
while(x % array[i] == 0){
total += array[i];
x /= array[i];
if(x == 1){
break;
}
}
}
if(total == 0){
cout << x << endl;
}
else{
cout << total << endl;
total = 0;
}
}
return 0;
}

 
#12650: Re:為什麼會溢位?


justinO__o (夜貓)

學校 : 臺北市立成功高級中學
編號 : 51052
來源 : [61.216.80.115]
最後登入時間 :
2023-12-25 15:06:44
a740. 质因数之和 -- 海豚原创 | From: [112.105.246.121] | 發表日期 : 2017-09-02 21:19

#include
#include
using namespace std;
bool prime[20000000];
int main(){
int x , total = 0;
vector array;
for(int i = 0;i < 20000000; i++){
prime[i] = true;
}
for(int i = 2; i < 20000000; i++){
if(prime[i]){
for(int j = i * 2; j < 20000000; j += i){
prime[j] = false;
}
array.push_back(i);
}
}
prime[0] = false;
prime[1] = false;

while(cin >> x){
for(int i = 0 ;array[i] <= x ; i++){        //條件如果改成 i < array.size() 就AC
while(x % array[i] == 0){
total += array[i];
x /= array[i];
if(x == 1){
break;
}
}
}
if(total == 0){
cout << x << endl;
}
else{
cout << total << endl;
total = 0;
}
}
return 0;
}


質數建到 sqrt(20000000) 就可以了吧 ?

 
ZeroJudge Forum