您的位置:首页 > 其它

UVa202 Repeating Decimals

2015-06-22 16:49 501 查看
#include <stdio.h>
#include <map>
using namespace std;

int main()
{
int a, b, c, q, r, places;
map<int, int> rmap;
pair<map<int, int>::iterator, bool> pr;
int qarr[50];
while (scanf("%d %d", &a, &b) == 2)
{
rmap.clear();
places = 0;
r = a % b;

for (;;)
{
c = r * 10;
pr = rmap.insert(make_pair(c, places));
if (!pr.second)
break;
q = c / b;
r = c % b;
if (places < 50)
qarr[places] = q;
++places;
}

printf("%d/%d = %d.", a, b, a/b);
r = places > 50 ? 50 : places;
for (c = 0; c < r; ++c)
{
if (c == (pr.first)->second)
putchar('(');
printf("%d", qarr[c]);
}
if (places > 50)
printf("...");
printf(")\n %d = number of digits in repeating cycle\n\n", places - (pr.first)->second);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: