方法一: 自己想的,虽然很笨,但毕竟是自己想的
先贴代码,再补充笔记和说明。
具体思路会画到纸上,照片贴出来,不过很有必要学习下网上别人的思路。
我的逻辑见图:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 * 写在前面的话,虽然AC了,但是觉得这个逻辑还是有不严谨的地方,应该是数据不够强大。 3 * 具体是在第134行,算上注释,其实那里判断不应该吧 (beDividedNum/startNum <=100)算进去 4 * 而是应该写到if{......}中,那里判断 beDividedNum/startNum 是否>100 5 * 如果是,则吧beDividedNum/startNum传入canBeDivided 6 * 7 * 但是我后来改成了我认为对的逻辑,OJ上却WA了。 8 * 9 ******************************************************************************************************* 10 * !!!!!!!!!!!!!!网上的AC 代码貌似比我这个要简单的多,有必要去看下。!!!!!!!!!!!!!!!!! 11 */ 12 #include <stdio.h> 13 #include <math.h> 14 15 #define kStateDivideTrue 1 16 #define kStateDivideFalse 2 17 #define kStateDivideNeedMore 3 18 19 void resetKeyNumArray(int keyNum[]); 20 int canBeDivided(int beDividedNum, int startNum, int keyNum[]); 21 22 int main() 23 { 24 int aInput,bInput; 25 int bigger,smaller; 26 int challengeResult; 27 int keyNum[100] ={0}; 28 int state = 0; 29 30 //----------------------Input----------------------------- 31 while (scanf("%d %d", &aInput, &bInput) != EOF) 32 { 33 //--------------------Find-Winner------------------------- 34 bigger = (aInput>bInput) ? aInput : bInput; 35 smaller = (aInput<bInput) ? aInput : bInput; 36 challengeResult = bigger; 37 38 if (bigger<=100 && smaller<=100) 39 { 40 challengeResult = bigger; 41 } 42 else if (bigger>100 && smaller<=100) 43 { 44 if (0 != bigger%smaller) 45 { 46 keyNum[0] = smaller; 47 state = canBeDivided(bigger, 2, keyNum); 48 if (kStateDivideTrue == state) 49 challengeResult = bigger; 50 else 51 challengeResult = smaller; 52 } 53 else 54 { 55 resetKeyNumArray(keyNum); 56 keyNum[0] = bigger/smaller; 57 state = canBeDivided(smaller, 2, keyNum); 58 if (kStateDivideTrue == state) 59 { 60 challengeResult = bigger; 61 } 62 else 63 { 64 resetKeyNumArray(keyNum); 65 keyNum[0] = smaller; 66 keyNum[1] = bigger/smaller; 67 state = canBeDivided(bigger, 2, keyNum); 68 if (kStateDivideTrue == state) 69 challengeResult = bigger; 70 else 71 challengeResult = smaller; 72 } 73 } 74 } 75 else if (bigger>100 && smaller>100) 76 { 77 resetKeyNumArray(keyNum); 78 state = canBeDivided(smaller, 2, keyNum); 79 if (kStateDivideTrue == state) 80 { 81 state = canBeDivided(bigger, 2, keyNum); 82 if (kStateDivideTrue == state) 83 { 84 challengeResult = bigger; 85 } 86 else if (kStateDivideFalse == state) 87 { 88 challengeResult = smaller; 89 } 90 else 91 { 92 state = canBeDivided(smaller, 2, keyNum); 93 if (kStateDivideTrue == state) 94 { 95 challengeResult = bigger; 96 } 97 else 98 challengeResult = smaller; 99 } 100 } 101 else 102 challengeResult = bigger; 103 } 104 else 105 { 106 challengeResult = bigger; 107 } 108 109 //----------------------Output---------------------------- 110 printf("%d\n", challengeResult); 111 resetKeyNumArray(keyNum); 112 state = 0; 113 } 114 return 0; 115 } 116 117 void resetKeyNumArray(int keyNum[]) 118 { 119 int i = 0; 120 for (i=0; i<100 && keyNum[i]!=0; i++) 121 { 122 keyNum[i] = 0; 123 } 124 } 125 126 int canBeDivided(int beDividedNum, int startNum, int keyNum[]) 127 { 128 int state = 0; 129 int i = 0; 130 int conflict = 0; 131 if(startNum >= beDividedNum || startNum >= 100) 132 return kStateDivideFalse; 133 else if (1 == beDividedNum) 134 { 135 return kStateDivideFalse; 136 } 137 else 138 { 139 if (0 == beDividedNum%startNum && beDividedNum/startNum <=100) 140 { 141 // if (0 == beDividedNum%startNum) 142 // { 143 // if (beDividedNum/startNum > 100) 144 // { 145 // state = canBeDivided(beDividedNum/startNum, startNum, keyNum); 146 // if (kStateDivideTrue != state) 147 // return canBeDivided(beDividedNum, startNum+1, keyNum); 148 // } 149 if(NULL == keyNum) 150 return kStateDivideTrue; 151 else if (beDividedNum/startNum == startNum) 152 { 153 return canBeDivided(beDividedNum, startNum+1,keyNum); 154 } 155 else 156 { 157 for (i=0; i<100 && keyNum[i] !=0; i++) 158 { 159 // 0:no conflict 1:has conflict 160 conflict = (keyNum[i] == startNum || keyNum[i] == beDividedNum/startNum) ? 1 : 0; 161 if (1 == conflict) 162 break; 163 } 164 if (0 == conflict) 165 { 166 i = 0; 167 while (keyNum[i] !=0) 168 i++; 169 keyNum[i] = startNum; 170 keyNum[i+1] = beDividedNum/startNum; 171 return kStateDivideTrue; 172 } 173 else if (1 == conflict) 174 { 175 state = canBeDivided(beDividedNum, startNum+1, keyNum); 176 if (kStateDivideTrue == state) 177 { 178 return kStateDivideTrue; 179 } 180 else 181 { 182 return kStateDivideNeedMore; 183 } 184 } 185 else 186 return kStateDivideTrue; 187 } 188 } 189 else 190 { 191 return canBeDivided(beDividedNum, startNum+1, keyNum); 192 } 193 } 194 }