將分數之整數部分輸出,並將之減掉整數部分後做分母分子反轉再重複以上步驟,直到分子的數值變為零即是答案。
C++(0.000)
/*******************************************************/
/* UVa 834 Continued Fractions */
/* Author: Maplewing [at] knightzone.studio */
/* Version: 2015/12/02 */
/*******************************************************/
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int numerator, denominator;
while( scanf("%d%d", &numerator, &denominator) != EOF ){
printf("[%d;", numerator / denominator);
numerator -= numerator / denominator * denominator;
bool isPrint = false;
while( numerator > 0 ){
swap(numerator, denominator);
if( isPrint ){
printf(",");
}
printf("%d", numerator / denominator);
isPrint = true;
numerator -= numerator / denominator * denominator;
}
printf("]\n");
}
return 0;
}