2895. 循环小数

Saitama
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    int a,b;
    while(scanf("%d/%d",&a,&b)!=EOF)
    {
        int yushu[10000],ans[10000];
        int j=0,k=0;
        int jdg=0;
        while(a%b)
        {
            yushu[k++]=a%b;
            ans[j++]=a/b;
            a=a%b*10;
            for(int i=0;i<k-1;i++)
            {
                if(yushu[i]==yushu[k-1])
                {
                    for(int p=i+1;p<=k-1;p++)
                        cout<<ans[p];
                    cout<<endl;
                    jdg=1;
                    break;
                }
            }
            if(jdg==1)
                break;
        }
        if(a%b==0)
            cout<<0<<endl;
    }
    return 0;
}
10175102211

多谢成耀大佬指点

10165101122

用循环会超时,所以改用了标记

include

int main()
{
int a,b;
while(scanf(“%d/%d”,&a,&b)!=EOF)
{ if(a<0||b<0)break;
int i=0,k,flag=0;
int s[100000]={0};
int yu[100000]={0};
for(;a>=b;a=a%b);
for(;a<b;++i)
{ if(a==0)
{
printf(“0\n”);
break;
}
if(yu[a]==1)
{
for(k=0;k<i;++k)
{
if(s[k]==(a10)/b)break;
}
for(;k<i;++k)printf(“%d”,s[k]);
printf(“\n”); flag=1;
break;
}
yu[a]=1;
a=a
10;
s[i]=a/b;
a=a%b;
if(flag==1)break;
}

}

}

Money4

判断余数是否出现过
用map标记会超时

#include<bits/stdc++.h>
using namespace std;
int main() {
    char c;
    int m, n, i;
    while (scanf("%d/%d", &m, &n) != EOF) {
        int nums[100001] = {0};
        bool flag = false;
        string res = {}, ans;
        i = 1;
        nums[m] = i++;
        int left;
        while (m != 0) {
            m *= 10;
            left = m / n;
            res += to_string(left);
            m = m % n;
            if (nums[m]) {
                for (int j = nums[m]-1; j < i-1; j++)
                    printf("%c",res[j]);
                printf("\n");
                break;
            }
            else {
                nums[m] = i++;
            }
        }
        if (m==0) printf("0\n");
    }
}
Money4

用unordered_map可以

296804129

求助,帮忙看看哪里错了

include

include

int main()
{
int m,n;
int a[1000],b[1000],i,j;
while(scanf(“%d/%d”,&m,&n)==2)
{
if(m<=0||n<=0)break;
m=m10;
for(i=0; m%n!=0; i++)
{
a[i]=m/n;
b[i]=m%n;
m=b[i]
10;
for(j=0; j<i; j++)
{
if(b[i]==b[j]&&a[j]==a[i])break;
}
if(i!=0&&a[j]==a[i]&&j!=i)break;
}
if(m%n==0)printf(“0\n”);
else
{
for(; j<i; j++)printf(“%d”,a[j]);
printf(“\n”);
}
}
return 0;
}

一剑无痕雪满山

烦烦烦 总是不过
[em:15]我也……而且就是按照老师的方法。。总是tle 。。。
_(:зゝ∠)_哭了

include

include

include

int main()
{
int n,m;int i;int flag;int yushu;int shang;
while (scanf(“%d/%d”,&m,&n))
{
if (m<=0||n<=0) {break;}
yushu=(int)malloc(nsizeof(int));
shang=(int)malloc(nsizeof(int));
for (i=0;i<n;i++)
{
yushu[i]=-1;shang[i]=-1;
}
for (i=0;i<n;i++)
{
yushu[m]=i;
m=m*10;
shang[i]=m/n;
m=m%n;

     if (m==0)   {printf("0\n");break;}
     if (yushu[m]!=-1)
     {
         for(flag=yushu[m];flag<=i;flag++)
         {
                printf("%d",shang[flag]);

         }
          printf("\n");break;
     }
 }

free(yushu);
free(shang);
}
return 0;
}

10175101159

TLE的话,先判断余数相等再判断商相等,保证过
分母很大时,余数相等的概率相比商相等的概率大很多
&&运算前面不满足自动停止

Master X

既然你都知道EOJ巨快。
那么……
懂了吧。

Suzuki_Yuuta

坑点就是寻找当前的余数是否与之前余数相同的操作时需要优化,否则会超时。

void solve(int a, int b) {
    int quotient[100001], i_q = 1; \\ 记录商以及个数
    int remain[100001] = {0}, i_r = 1; \\ 用类似哈希表的方式存储余数及其对应的次序
    int p = a; bool find = false;
    while (!find) {
        quotient[i_q++] = p / b;
        if (p % b == 0) { 
            cout << 0 << endl; find = true;
        } else {
            if (remain[p % b]) {
                for (int j = remain[p % b] + 1; j < i_q; j++) {
                    cout << quotient[j];
                }
                cout << endl; find = true;
            }
            remain[p % b] = i_r++; p = (p % b) * 10;
        }
    }
}
LzQuarter

汗,建议确认过了样例以后试试
https://blog.csdn.net/panhe1992/article/details/7471320
上面这个地址提到的样例

#include<iostream>
#include<queue>
using namespace std;
int main(){
    int a, b;
    char t;
    while(cin >> a >> t >> b){
        int vis[100001] = {0};
        queue<int> left;
        queue<int> res;
        int zero = 0;
        do{
            int resnum = a / b;
            int leftnum = a % b;
            //cout << resnum << " " << leftnum << endl;
            a %= b;
            if(leftnum == 0){
                zero = 1;
                break;
            }
            if(vis[leftnum]){
                int firstpoped = 0;
                while(left.front() != leftnum){
                    firstpoped = 1;
                    left.pop();
                    res.pop();
                }
                if(res.front() != resnum){
                    left.pop();
                    res.pop();
                    res.push(resnum);
                }
                else if(!firstpoped && res.front() == 0){
                    left.pop();
                    res.pop();
                    res.push(resnum);
                }
                break;
            }
            else{
                vis[leftnum] = 1;
                left.push(leftnum);
                res.push(resnum);
            }
            a *= 10;
        }while(1);
        if(zero){
            cout << "0" << endl;
        }
        else{
            while(!res.empty()){
                cout << res.front();
                res.pop();
            }
            cout << endl;
        }
    }
    return 0;
}
LzQuarter

这个大概是能AC的

你当前正在回复 博客/题目
存在问题!