usaco Fractions to Decimals

分数转小数,循环小数的循环节要放在括号内

方法是模拟触发操作,然后记录下余数,每次生成的余数检查以前是否出现过,出现过说明出现了循环节,则找到循环节开始的地方。

这题一点都不好写,我反正是打了好多补丁的感觉。

/*
ID: modengd1
PROG: fracdec
LANG: C++
*/
#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <vector>
#include <math.h>
using namespace std;
void output(int ans1,vector<int> ans2,int cystart,bool iscycle)
{
    int lenofNotfloat =0;
    for(int i=0;pow(10,i-1)<=ans1;i++)
        lenofNotfloat=i;
    cout<<ans1<<‘.‘;
    if(ans1==0)
        lenofNotfloat++;
    lenofNotfloat++;
    if(ans2.size()==0)
        cout<<0;
    int i;
    for( i=0;i<cystart;i++)
    {
        cout<<ans2[i];
        if((i+lenofNotfloat)%76==75)
            cout<<endl;
    }
    if(iscycle)
    {
        cout<<‘(‘;
        lenofNotfloat++;
        if((i+lenofNotfloat)%76==75)
            cout<<endl;
    }
    for(;i<ans2.size();i++)
    {
        cout<<ans2[i];
        if((i+lenofNotfloat)%76==75)
            cout<<endl;
    }
    if(iscycle)
    {
        cout<<‘)‘;
        lenofNotfloat++;
        if((i+lenofNotfloat)%76==75)
            cout<<endl;
    }
    cout<<endl;
}
int main()
{
    freopen("fracdec.in","r",stdin);
    freopen("fracdec.out","w",stdout);
    bool vis[1000000];
    memset(vis,false,sizeof(vis));
    vector<int> mods;//记录每次除得的余数
    int N,D;
    int ans1;//整数部分
    vector<int> ans2;//小数部分
    bool iscycle=false;;//是否循环小数
    int cystart=-1;//循环节开始的第几位小数
    scanf("%d%d",&N,&D);
    ans1=N/D;//求出整数部分
    N%=D;//得到第一个余数,这个余数若不等于0,则从它后面的计算开始产生小数
    N*=10;//给余数乘10,从此这个数产生第一位小数
    mods.push_back(N);
    vis[N]=true;
    while(N!=0&&!iscycle)
    {

        if(N<D)//新的被除数小于除数,给被除数乘10,给商末尾写上0
        {
            ans2.push_back(0);
            N*=10;
        }
        else
        {
            ans2.push_back(N/D);//写入新求得的商
            N=N%D;//产生新的余数
            N*=10;//乘10,生成新的被除数
        }
        if(vis[N])
        for(int i=0;i<mods.size();i++)//检查此余数(其实是余数乘10后得到的新的被除数)是否已经出现过
        {
            if(mods[i]==N)
            {
                iscycle=true;
                cystart=i;
                break;
            }
        }
        if(iscycle)
            break;
        mods.push_back(N);//记录余数(其实是余数乘10后得到的新的被除数)
        vis[N]=true;
    }
    output(ans1,ans2,cystart,iscycle);
    return 0;
}

  

时间: 2024-11-15 17:55:08

usaco Fractions to Decimals的相关文章

USACO training 2.4.5 Fractions to Decimals题解

嗯...用到一个定理,对于一个最简分数n/d,d=(2^x)*(5^y)*m,m≠1,那么其循环节长度为使10^L mod m==1的最小的L,不循环长度为max(x,y) 然后这题就没什么了.. 76个字符一换行比较坑,我用了stringstream... 1 #include<iostream> 2 #include<sstream> 3 #include<cstdio> 4 #include<iomanip> 5 #include<algorit

USACO 2.4

USACO 2.4.1 题解: 模拟. 用一个6维数组储存农夫与奶牛当前状态是否出现过,若出现过则表明出现循环,直接输出0,f[农夫x][农夫y][农夫方向][奶牛x][奶牛y][奶牛方向]. 最后注意转弯要算一步. 代码: /* ID:m1599491 PROB:ttwo LANG:C++ */ #include<cmath> #include<cstdio> #include<cstring> #include<iostream> using names

分数化小数

Fractions to Decimals Write a program that will accept a fraction of the form N/D, where N is the numerator and D is the denominator and print the decimal representation.If the decimal representation has a repeating sequence of digits, indicate the s

What Is Mathematics?

What Is Mathematics? The National Council of Teachers of Mathematics (NCTM), the world's largest organization devoted to improving mathematics education, is developing a set of mathematics concepts, or standards, that are important for teaching and l

USACO 2.1 Ordered Fractions

Ordered Fractions Consider the set of all reduced fractions between 0 and 1 inclusive with denominators less than or equal to N. Here is the set when N = 5: 0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1 Write a program that, given an integer N between

USACO Section 2.1 Ordered Fractions

/* ID: lucien23 PROG: frac1 LANG: C++ */ #include <iostream> #include <fstream> #include <vector> #include <algorithm> using namespace std; typedef struct Fraction { int numerator; int denominator; Fraction(){} Fraction(int x, int

【USACO 2.1】Ordered Fractions

/* TASK: frac1 LANG: C++ URL: http://train.usaco.org/usacoprob2?S=frac1&a=dbgwn5v2WLr SOLVE: 直接枚举,约分,排序,去重 */ #include<cstdio> #include<algorithm> using namespace std; struct node{ int nu,deno; double v; }a[40000]; int n,cnt; int cmp(node

USACO Ordered Fractions

题目大意:求一个n的farey序列 思路:懒得麻烦的推导和公式了,暴力压倒一切 /*{ ID:a4298442 PROB:frac1 LANG:C++ } */ #include<cstdio> #include<iostream> #include<algorithm> #include<fstream> #define maxn 160*160+10 using namespace std; ifstream fin("frac1.in&quo

USACO Section2.1 Ordered Fractions 解题报告

frac1解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------------------------------------------------------------------------------------------[题目] 给你N,对于所有的既约分数i/j(1<=j<=N,0<=i<=j),升序排列输出(重复则只留j最小的).[数据范围]