Skip to content

Instantly share code, notes, and snippets.

@WindAzure
Last active September 16, 2018 16:25
Show Gist options
  • Select an option

  • Save WindAzure/187c466953b2219d1db53477bf2a9665 to your computer and use it in GitHub Desktop.

Select an option

Save WindAzure/187c466953b2219d1db53477bf2a9665 to your computer and use it in GitHub Desktop.
UVa 202
#include <stdio.h>
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
auto a = 0, b = 0;
while (~scanf("%d%d", &a, &b))
{
printf("%d/%d = %d.", a, b, a / b);
auto remainder = a % b;
auto cycle = string{};
auto remainderHistory = map<int, int>();
while (true)
{
if (remainderHistory.find(remainder) != remainderHistory.end())
{
cycle.insert(remainderHistory[remainder], "(");
cycle.append(")");
break;
}
remainderHistory[remainder] = cycle.size();
remainder *= 10;
cycle.append(to_string(remainder / b));
remainder = remainder % b;
}
auto cycleLength = cycle.size()- remainderHistory[remainder] - 2;
if (cycleLength >= 51)
{
cycle = cycle.substr(remainderHistory[remainder], 51) + "...)";
}
cout << cycle << endl;
printf(" %d = number of digits in repeating cycle\n\n", cycleLength);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment