ZOJ 1003 DFS 因子分解
View Code
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 #include<string.h> 5 6 int ascore, bscore; 7 8 int DFS(int m, int n, int p) 9 { 10 if(ascore == 1) return 0; 11 if(m==1 && n==1) 12 { 13 bscore = 1; 14 ascore = 1; 15 return 0; 16 } 17 18 if(n == 1) bscore = 1; 19 20 while (p > 1) 21 { 22 if(m%p == 0) DFS(m/p, n, p-1); 23 if(n%p == 0) DFS(m, n/p, p-1); 24 p--; 25 } 26 27 return 0; 28 } 29 30 int main() 31 { 32 int a, b, temp; 33 34 while(scanf("%d%d",&a,&b) != EOF) 35 { 36 37 if(a < b) 38 { 39 temp = a; 40 a = b; 41 b = temp; 42 } 43 ascore = 0; bscore = 0; 44 45 DFS(a, b, 100); 46 if(bscore && !ascore) 47 printf("%d\n", b); 48 else 49 printf("%d\n", a); 50 51 } 52 53 return 0; 54 } 55 56