zoj 1086 Octal Fractions

//小数的高精度计算!利用辗转相除就可以!
#include "iostream"
#include "cstdio"
#include "string.h"
using namespace std;

const int maxsize = 100;

int main()
{
	char input[maxsize], ans[maxsize];//输入和结果的储存!
	int len, i, j, index, num, temp;
	while (scanf("%s", input) != EOF)
	{
		len = strlen(input);
		memset(ans, 0, sizeof(ans));
		index = 0;//结果的长度!
		for (i = len - 1; i > 1; i--)
		{
			num = input[i] - 48;
			for (j = 0; j < index || num; j++)
			{
				temp = num * 10 + (j < index ? ans[j] - 48 : 0);
				ans[j] = temp / 8 + 48;
				num = temp % 8;
			}
			index = j;
		}
		ans[j] = '\0';
		printf("%s [8] = 0.%s [10]\n", input, ans);
	}
}

你可能感兴趣的:(zoj 1086 Octal Fractions)