uva 202 小数循环节

http://acm.bnu.edu.cn/v3/contest_show.php?cid=5772#problem/G

模仿竖式除法,当某一个余数出现第二次的时候就循环了,用数组保存出现过的余数和余数出现的位置。

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <bitset>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define clr0(x) memset(x,0,sizeof(x))
#define clr1(x) memset(x,-1,sizeof(x))
typedef long long LL;
const int maxn = 3005;

int r[maxn],u[maxn],s[maxn];

int main()
{
	int n,m,t;
	while (~RD2(n,m)) {
		t = n;
		clr0(r),clr0(u);
		int cnt = 0;
		r[cnt ++] = n/m;
		n = n%m;
		while (!u[n] && n) {
			u[n] = cnt;
			s[cnt] = n;
			r[cnt ++] = 10*n/m;
			n = 10*n%m;
		}
		printf("%d/%d = %d.",t,m,r[0]);
		for (int i = 1;i < cnt && i <= 50;++i) {
			if (n && s[i] == n) printf("(");
			printf("%d",r[i]);
		}
		if (!n) printf("(0");
		if (cnt > 50) printf("...");
		printf(")\n");
		printf("   %d = number of digits in repeating cycle\n\n",n?(cnt-u[n]):1);
	}
	return 0;
}


你可能感兴趣的:(uva 202 小数循环节)