(UVA 202) Repeating Decimals(除法模拟+哈希映射)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/Africa_South/article/details/102246439

原题:https://vjudge.net/problem/UVA-202

题目大意
在这里插入图片描述
题目分析
手动模拟除法运算:

  • 先输出整数部分,然后a变成余数*10,用数组ans记录下小数的商,建立哈希表,初始值为-1
    将此时的余数a的位置用哈希表记录下来(位置从0开始,0表示整数位值,从1开始便是小数位的商)
  • 然后继续计算a / b,将商压入ans,如果余数出现过,即哈希表的值不为-1,表示开始出现循环,则开始输出小数部分和循环部分

此题的难点之一应该是如何判断小数部分是否开始循环——即商是否出现过,我采用的方法是将所有出现过的商与其位置映射起来。还有一个注意的是,每个样例的输出后都需要输出一个"\n"。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<iostream>
using namespace std;

int a, b;
int hash_table[30010]; // 用来统计余数出现的位置,若余数出现过,则表示开始循环了
vector<int> ans; // 拿来保存小数部分
int main() {
	while (scanf("%d %d", &a, &b) != EOF) {
		ans.clear();
		memset(hash_table, -1, sizeof(hash_table));
		int num = a / b; // 整数
		int re = a % b; // 余数
		hash_table[re] = 0;
		int cnt = 0; // 小数部分的个数
		re *= 10;
		printf("%d/%d = %d.", a, b, num);
		while (1) {
			a = re / b; // 新的商
			ans.push_back(a);
			re = re % b; // 新的余数
			//printf("商=%d,余数=%d\n", a, re);
			cnt++;

			if (hash_table[re] != -1) {
				// 重复了,开始输出,只输出50位小数
				int point = 0;
				for (int i = 0; i < hash_table[re]; point++,i++) printf("%d", ans[i]);
				printf("(");
				for(int i=hash_table[re];i<cnt && point < 50;point++,i++) printf("%d", ans[i]);
				if (point == 50) printf("...");
				printf(")\n");
				printf("   %d = number of digits in repeating cycle\n\n", cnt - hash_table[re]);
				break;
			}
			else {
				hash_table[re] = cnt;
				re *= 10; // 继续计算
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Africa_South/article/details/102246439