想法:
這題可以自己舉幾個例子算出分數,找出規則,解法如下,由題目Example,從(43,19)逆推回去
- 首先2的話就是43/19=2,43%19=5,那麼現在[2;] & (5,19)
- 19/5=3,19%5=4,得到[2;3] & (5,4)
- 倒數的緣故,交換兩數,現在[2;3] & (4,5)
- 重複2~3步驟,直到第一個數為1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <algorithm> | |
using namespace std; | |
int main() | |
{ | |
int a,b; | |
while (scanf("%d%d",&a,&b)!=EOF){ // 43,19 | |
printf("[%d;",a/b); // [2; | |
a %= b; // 5,19 | |
while(a!=1){ | |
printf("%d,",b/a); // [2;3, / [2;3,1, | |
b %= a; // 5,4 / 4,1 | |
swap (a,b); // 4,5 / 1,4 | |
} | |
printf("%d]\n",b); // [2;3,4,1] | |
} | |
return 0; | |
} |
沒有留言:
張貼留言