欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UVA - 202 Repeating Decimals

程序员文章站 2022-03-14 19:40:02
...

Repeating Decimals

 UVA - 202 

题目传送门

解决方法:模拟一下除法,及时记录余数,当一个余数第二次出现时证明开始循环

AC代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int yushu[maxn];
int shang[maxn];
int wei[maxn];
int main() 
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    //freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m;
    int cnt=0;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
    	int k=n;
    	ms(yushu);ms(shang);ms(wei);
    	int count=0;
    	shang[count++]=n/m;
    	n=n%m;
    	while(!wei[n]&&n)
    	{
    		wei[n]=count;
    		yushu[count]=n;
    		shang[count++]=n*10/m;
    		n=n*10%m;
    	}
    	printf("%d/%d = %d.",k,m,shang[0]);
    	int i;
    	for(i=1;i<count&&i<=50;i++)
    	{
    		if(yushu[i]==n&&n)
    			printf("(");
    		printf("%d",shang[i]);
    	}
    	if(n==0)
    		printf("(0");
    	if(count>50)
    		printf("...");
    	printf(")\n");
    	printf("   %d = number of digits in repeating cycle\n\n",!n?1:count-wei[n]);
    }
    return 0;
}

 

相关标签: 模拟