就是每两项相减,肯定能被模数整除。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 7 const int Maxn=1010; 8 int f[Maxn],n,Ans; 9 int Gcd(int a,int b) {if (b==0) return a; return Gcd(b,a%b);} 10 inline int Abs(int x) {return x>0?x:-x;} 11 int main() 12 { 13 while (true) 14 { 15 n=0; 16 scanf("%d",&f[++n]); 17 if (f[n]==0) break; 18 while (f[n]!=0) scanf("%d",&f[++n]); 19 n--; 20 for (int i=1;i<n;i++) 21 f[i]=f[i]-f[i+1]; 22 Ans=f[1]; 23 for (int i=2;i<n;i++) 24 Ans=Gcd(f[i]==0?Ans:f[i],Ans); 25 printf("%d ",Abs(Ans)); 26 } 27 return 0; 28 }