#include using namespace std; int gcd(int x, int y); int main(){ int n,student; while(cin >> n){ int ans[n],answer=0; int people[n]; for(int i=0;i<n;i++){ //把人數輸入陣列 cin >> student; people[i] = student; } for(int i=0;i<n;i++){ if (i != n-1){ int r = gcd(people[i], people[i+1]); ans[i] = r; } for(int i=0;i<n;i++){ //檢驗是否為最大公因數 if (i != n-1){ answer = ans[0]; while(ans[i+1] < answer){ answer = ans[i+1]; } } } } cout << answer << endl; } } int gcd(int x,int y){ if (y == 0){ return x; } else { return gcd(y , x % y); //即使後大於前仍會調換 } }
#include<iostream>
using namespace std;
int gcd(int x, int y);
int main(){
int n,student;
while(cin >> n){
int ans[n],answer=0;
int people[n];
for(int i=0;i<n;i++){ //把人數輸入陣列
cin >> student;
people[i] = student;
}
for(int i=0;i<n;i++){
if (i != n-1){
int r = gcd(people[i], people[i+1]);
ans[i] = r;
}
for(int i=0;i<n;i++){ //檢驗是否為最大公因數
if (i != n-1){
answer = ans[0];
while(ans[i+1] < answer){
answer = ans[i+1];
}
}
}
}
cout << answer << endl;
}
}
int gcd(int x,int y){
if (y == 0){
return x;
} else {
return gcd(y , x % y); //即使後大於前仍會調換
}
}
#include
using namespace std;
int gcd(int x, int y);
int main(){
int n,student;
while(cin >> n){
int ans[n],answer=0;
int people[n];
for(int i=0;i<n;i++){ //把人數輸入陣列
cin >> student;
people[i] = student;
}
for(int i=0;i<n;i++){
if (i != n-1){
int r = gcd(people[i], people[i+1]);
ans[i] = r;
}
for(int i=0;i<n;i++){ //檢驗是否為最大公因數
if (i != n-1){
answer = ans[0];
while(ans[i+1] < answer){
answer = ans[i+1];
}
}
}
}
cout << answer << endl;
}
}
int gcd(int x,int y){
if (y == 0){
return x;
} else {
return gcd(y , x % y); //即使後大於前仍會調換
}
}
for(int i=0;i<n;i++){ //檢驗是否為最大公因數
if (i != n-1){
answer = ans[0];
while(ans[i+1] < answer){
answer = ans[i+1];
}
}
}
n 等於 4 的時候 i 會跑0 1 2
你的 ans[i+1] 會跑到 ans[3]
你的ans[3]是 0
你的檢驗方法應該也有錯
3
494
1482
741
用你方法答案出來會是494
不過正確答案是247