2972. 分数的精确值

#include <cstdint>
#include <iostream>
#include <unordered_map>

int main() {
using u32 = uint32_t;
u32 t;
std::cin >> t;
for (u32 i = 0; i != t; ++i) {
u32 n, m;
std::cin >> n >> m;
std::cout << "case #" << i << ":\n" << n / m;
n = n % m * 10;
if (!n) {
std::cout << '\n';
continue;
}
std::cout << '.';
using BitPos = u32;
using Dividend = u32;
u32 bit_cnt = 1;
std::unordered_map<Dividend, BitPos> table{{n, bit_cnt}};
do {
std::cout << n / m;
n = n % m * 10;
if (auto [it, ok] = table.insert({n, bit_cnt += 1}); !ok) {
std::cout << '\n' << it->second << '-' << bit_cnt - 1;
break;
}
} while (n);
std::cout << '\n';
}
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int T;
cin >> T;
for (int cse = 0; cse < T; cse++)
{
cout << "case #" << cse << ":" << endl;
int a, b;
cin >> a >> b;
vector<int> R; // 余数
vector<int> Q; // 商
int z = a / b; // 整数部分
cout << z << ".";
a %= b;
bool inf = false;
int begini,endi; //循环节开始和结束位
for(;;)
{
a *= 10;
int q = a / b;
int r = a % b;
a = r;
if (r == 0)
{
Q.push_back(q);
inf = false;
break;
}
else
{
vector<int>::iterator it = find(R.begin(), R.end(), r);
if (it == R.end()) // 余数不重复
{
Q.push_back(q);
R.push_back(r);
}
else // 余数重复
{
begini = it - R.begin(); // 这里是算的索引值，后面输出要 +1
inf = true;
if (q != Q[begini]) // 起初就是因为少了这个判断和 +1 的逻辑，试了很多次，样例都过不了
{
begini++;
Q.push_back(q); // 前面不相等的商不是循环节，因此要把这个商放进去
}
endi = Q.size();
break;
}

}

}
for (int i = 0; i < Q.size(); i++)
{
cout << Q[i];
}
cout << endl;
if (inf)
{
cout << (begini + 1) << '-' << endi << endl;
}
}
return 0;
}


include

using namespace std;
int T,up,down;
int occurred[1000];

int main()
{
cin>>T;
for(int step=0;step>up>>down;
memset(occurred,0,sizeof(occurred));
int yu=up%down;
printf(“case #%d:\n0.”,step);
int pos=1;
while(yu!=0 && !occurred[yu])
{
occurred[yu]=pos++;
cout<<(yu10)/down;
yu=(yu
10)%down;
}
if(yu==0) cout<<endl;
else
{
cout<<endl<<occurred[yu]<<’-‘<<pos-1<<endl;
}
}
return 0;
}

#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n,m;
cin>>n>>m;
int ans[10000],rmdr[10000];
int k=0,j=0,flag=0;
int intpart=n/m;
rmdr[j++]=n%m;
n%=m;
cout<<intpart<<'.';
while(n%m!=0)
{
n*=10;
ans[k]=n/m;
rmdr[j++]=n%m;
n%=m;
cout<<ans[k++];
for(int i=0;i<j-1;i++)
{
if(rmdr[i]==rmdr[j-1])
{
flag=1;
if(i==j-2)
{
cout<<endl<<i+1<<'-'<<i+1<<endl;
break;
}
else
{
cout<<endl<<i+1<<'-'<<j-1<<endl;
break;
}
}
}
if(flag==1)
break;
}
if(flag==0)
cout<<endl;

}
int main()
{
int T;
cin>>T;
for(int i=0;i<T;i++)
{
printf("case #%d:\n",i);
solve();
}
return 0;
}


include

using namespace std;
void solve()
{
int n,m;
cin>>n>>m;
int s[10000];
int t;
int i=0;
while(n!=0)
{
n=n10;
s[i]=n/m,n=n%m;
int flu=0;
for(t=0;t<=i;t++)
{
if(s[t]==s[i]&&t!=i)
{
int k=i-t;
int t1=t;
int n1=n;
int i1=i+1;
int flag=0;
t1+=1;
while(k–)
{
n1=n1
10;
s[i1]=n1/m,n1=n1%m;
if(s[i1]!=s[t1]) {flag=1;break;}
else t1++,i1++;
}
if(flag==1) break;
else {
k-=1;
if(k==1) i=t;
else i-=1;
flu=1;
}
}
if(flu==1) break;
}
i++;
if(flu==1) break;
}
t+=1;
cout<<”0.”;
for(int t=0;t<i;t++) cout<<s[t];
cout<<endl;
if(t<=i) cout<<t<<”-“<<i<>n;
for(int i=0;i<n;i++)
{
printf(“case #%d:\n”,i);
solve();
}
}

import re
from decimal import *

getcontext().prec = 2077
for _ in range(int(input())):
print(f'case #{_}:')
a, b = map(Decimal, input().split())
s = str(a / b)
m = re.search(r'(.+?)(?=\1{10,})', s)
if m:
print(s[:m.end()])
print(f'{m.start() - 1}-{m.end() - 2}')
else:
print(s)