hdoj 1717 小數化分數2【水】

/*
循環小數化分數:
小數形式:循環部分&循環部分  【&】表示連接 
分數形式: (非循環部分&循環部分)/( (10^(循環節位數)-1)^(非循環節位數) )
例:
0.32(692307)
32692307 / 99999900 = 17/52 
注意的輸入:0.00(1) 
*/
#include<stdio.h>
#include <math.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#define PI 3.1415926
#define STOP system("pause")
using namespace::std;
int gcd(int a, int b)
{
	while(b^=a^=b^=a%=b);
	return a;
}
int bits(int x)
{
	int ans = 0;
	while(x)
	{
		ans ++;
		x /= 10;
	}
	return ans;
}
int main()
{
	int a, b, t, n, b1, b2, x, y, i;
	char temp;
	scanf("%d", &n);
	while(n--)
	{
		a = b = 0;
		scanf(" 0.");
		i = 0;
		while(temp = getchar(), temp >= '0' && temp <= '9')
		{
			a = a * 10 + (temp -'0');
			i ++;
		}
		b1 = i;
		if(temp == '\n')
		{
			y = 1;
			for(i=0;  i<b1;  i++)
				y *= 10;
			x = a;
			t = gcd(x, y);
			printf("%d/%d\n", x/t, y/t);
			continue;
		}
		i = 0;
		while(temp = getchar(), temp >= '0' && temp <= '9')
		{
			b = b * 10 + temp -  '0';
			i++;
		}
		b2 = i;
		x = a;
		for(i=0; i<b2; i++)
			x *= 10;
		x += b;
		x -= a;
		y = 1;
		for(i=0; i<b2; i++)
			y *= 10;
		y -= 1;
		for(i=0; i<b1; i++)
			y *= 10;
		t = gcd(x, y);
		printf("%d/%d\n", x / t,  y / t);
	}
	return 0;
}


發佈了164 篇原創文章 · 獲贊 1 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章