Sha 10555-dead fraction) (number theory)

Source: Internet
Author: User

Link: Ultraviolet A 10555-dead Fraction

A decimal number can be any number starting from..., but it must be an infinite repeating decimal number. The decimal number should be expressed in fraction form and the denominator should be as big as possible.

Solution: How can I convert an infinite loop decimal point to a component type:

  • There are decimal digits 0. abcdeee, Uncyclic part length 4, cyclic section is E, and e length is I (Suppose)
  • ABCD + e999... (I bit 9) 10i
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 105;const ll INF = 0x3f3f3f3f3f3f3f;char s[maxn];ll gcd (ll a, ll b) {    return b ? gcd(b, a%b) : a;}int main () {    while (scanf("%s", s) == 1 && strcmp(s, "0")) {        int len = strlen(s)-5;        ll ansu, ansd = INF;        for (int i = 0; i < len; i++)            s[i] = s[i+2];        for (int i = 0; i < len; i++) {            ll d = 1, u = 0;            for (int j = 0; j < i; j++) {                d = d * 10;                u = u * 10 + s[j] - ‘0‘;            }            ll x = 0, y = 0;            for (int j = i; j < len; j++) {                x = x * 10 + s[j] - ‘0‘;                y = y * 10 + 9;            }            d = d * y;            u = u * y + x;            ll g = gcd(d, u);            u /= g;            d /= g;            if (d < ansd) {                ansd = d;                ansu = u;            }        }        printf("%lld/%lld\n", ansu, ansd);    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.