UVA - 202——Repeating Decimals(暴力取余)

题目传送门:Repeating Decimals

题意:

输入a,b,求a/b的小数表示,并求出循环节长度。

分析:

不断用a整除b,得出的数为小数点后的数,记录下来,然后再用a = a%b*10再去整除b,直到出现相同的a位置。

AC代码:

(注意格式空行,在下pe了N遍。。。。。)

//#include<bits/stdc++.h>
#include  <stdio.h>
#include <iostream>
#include<algorithm>
//#include      <map>
//#include      <set>
//#include   <vector>
//#include    <queue>
//#include    <stack>
#include <stdlib.h>
#include  <cstring>
#include <string.h>
#include   <string>
#include   <math.h>
using namespace std;
typedef long long ll;
#define MAXN 10005
#define INF 0x3f3f3f3f//将近ll类型最大数的一半,而且乘2不会爆ll
const ll mod = 1000000007;
const ll inv = (mod + 1) / 2;

int up[MAXN], down[MAXN], ans[MAXN];

int iff(int *arr, int x, int y) {
	for (int i = 2; i <= x; ++i)
		if (arr[i] == y) return i;
	return 0;
}

int main()
{
	int a, b;
	while (cin >> a >> b) {
		memset(up, 0, sizeof(up));
		memset(down, 0, sizeof(down));
		memset(ans, 0, sizeof(ans));
		int i = 1, j = 1, m=1;
		up[i] = a, down[j] = a / b, ans[m] = a%b;
		while (!iff(up, i, ans[i]*10)) {
			i++; j++; m++;
			up[i] = ans[m - 1] * 10;
			down[j] = up[i] / b;
			ans[m] = up[i] % b;
		}
		int yy = iff(up, i, ans[i] * 10);
		printf("%d/%d = ", a, b);
		for (int e = 1; e <= i; ++e) {
			if (e == yy) cout << '(';
			cout << down[e];
			if (e == 1) cout << '.';
			if (e > 50) { cout << "..."; break;}
		}
		cout << ')' << '\n';
		cout << "   " << i - yy + 1 << " = number of digits in repeating cycle" << '\n' << '\n';
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值