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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章