uva 202-Repeating Decimals

Posted 033000-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了uva 202-Repeating Decimals相关的知识,希望对你有一定的参考价值。

题意:给你两个数a和b,计算a/b的结果,结果参照题意

首先必然会出现循环,因为对b取余的结果最有b种,当我们计算的次数增多后必然出现余数想等的情况,此时就出现了循环

知道这一点后就可以开始编程了,写一个模拟除法的程序,另外仔细读题,要按照它的要求输出

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<assert.h>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<sstream>
#include<stack>
#include<queue>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
#define me(s)  memset(s,0,sizeof(s))
#define _for(i,a,b) for(int i=(a);i<(b);++i)
#define _rep(i,a,b) for(int i=(a);i<=(b);++i)
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define Di(x) int x;scanf("%d",&x)
#define in(x) inp(x)
#define in2(x,y) inp(x),inp(y)
#define in3(x,y,z) inp(x),inp(y),inp(z)
#define ins(x) scanf("%s",x)
#define ind(x) scanf("%lf",&x)
#define IO ios_base::sync_with_stdio(0);cin.tie(0)
#define READ freopen("C:/Users/ASUS/Desktop/in.txt","r",stdin)
#define WRITE freopen("C:/Users/ASUS/Desktop/out.txt","w",stdout)
template<class T> void inp(T &x) {//读入优化
	char c = getchar(); x = 0;
	for (; (c < 48 || c>57); c = getchar());
	for (; c > 47 && c < 58; c = getchar()) { x = (x << 1) + (x << 3) + c - 48; }
}
typedef pair <int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-15;

const int maxn =3000 + 5;
int vis[maxn], ans[maxn], res[maxn];
int a, b;

int main() {
	//READ;
	//WRITE;
	int a, b;
	while (scanf("%d%d", &a, &b) == 2) {
		printf("%d/%d = ", a, b);
		me(vis);
		int k = 0;
		ans[k] = a / b;
		a = a % b; res[k++] = a;
		while (a && !vis[a]) {
			vis[a] = 1;
			a *= 10;
			ans[k] = a / b;
			a %= b; res[k++] = a;
		}
		//_for(i, 0, k) printf("%d----ans=%d,res=%d
", i, ans[i], res[i]);
		printf("%d.", ans[0]);
		if (!a) {
			for (int i = 1; i < k; i++)
				printf("%d", ans[i]);
			printf("(0)
");
			printf("   %d = number of digits in repeating cycle

", 1);
		}
		else {
			int st = 0;
			while (res[st] != a) st++;
			int cir = k - st - 1;
			for (int i = 1; i <= st; i++) printf("%d", ans[i]);
			printf("(");
			for (int i = st + 1; i <= min(st + cir, 50); i++) printf("%d", ans[i]);
			if (cir <= 50) printf(")
");
			else printf("...)
");
			printf("   %d = number of digits in repeating cycle

", cir);
		}
	}
	
		

}

  

以上是关于uva 202-Repeating Decimals的主要内容,如果未能解决你的问题,请参考以下文章

uva 202-Repeating Decimals

UVA202-Repeating Decimals

202 - Repeating Decimals

ACM中java中BigInteger和Decimal用到的主要函数

decimal.JS 快速入门

c#如何将null值附值decimal类型变量