3 条题解
-
119230202 尹庆杰 (YQJMonster) LV 9 @ 2023-09-05 20:54:31
#include<bits\stdc++.h>//万能文件头可能报错,慎用
using namespace std;
void Div(int d,int f)
{
int a[1000000],b[1000],c[1000];//a数组存余数是否出现的状态,b数组存循环节数,c数组存循环节数的位置
int tmp,i=0;
//初始化
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
while(1)
{
c[i] = d % f;//保留余数
d = d % f;
if(a[d])//判断余数是否出现
{
tmp = i;
break;
}
a[d] = 1;//标记余数已出现
d = d * 10;
b[i]=d / f;//保留商
i++;
}
for (int i = 0; i < 1000; i++)
{
if(c[tmp] == c[i] && tmp != i)//判断左括号输出的时机
cout<<"(";
if(b[i] == 0 && b[i+1] == 0 && b[i+2] == 0)
break;
cout<<b[i];
}
}int main()
{
int n,x,p,q;
cin>>x>>n>>p>>q;
cout<<"0.";
Div(x,n);
printf(")\n");
cout<<"0.";
Div(p,q);
printf(")\n");
return 0;
} -
02023-10-29 17:31:43@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>#define MAXLEN 1000
void Div(int x, int y, char* result) {
int remainder[MAXLEN];
int quotient[MAXLEN];
int index = 0;
int i;// 初始化余数数组为-1
memset(remainder, -1, sizeof(int) * MAXLEN);
x %= y;
// 计算小数部分的循环节
while (x != 0 && remainder[x] == -1) {
remainder[x] = index;
x *= 10;
quotient[index] = x / y;
x %= y;
index++;
}// 将循环节转换为字符串形式
memset(result, 0, sizeof(char) * MAXLEN);
if (x == 0) {
for (i = 0; i < index; i++) {
result[i] = quotient[i] + '0';
}
} else {
for (i = 0; i <remainder[x]; i++) {
result[i] = quotient[i] + '0';
}
result[i] = '(';
i++;
for (; i < index+1; i++) {
result[i] = quotient[i-1] + '0';
}
result[i] = ')';
}
}int main() {
int x1, y1, x2, y2;
char result1[MAXLEN];
char result2[MAXLEN];scanf("%d %d", &x1, &y1);
scanf("%d %d", &x2, &y2);
Div(x1, y1, result1);
Div(x2, y2, result2);printf("0.%s\n", result1);
printf("0.%s\n", result2);return 0;
} -
02023-09-05 22:28:02@
//依照上一个题解思路做的C语言简化版(也没简化多少)
//感谢上一个题解的思考#include <stdio.h>
void div(int x, int y)
{
int a[100000] = { 0 };//保存小数
int b[1000] = { 0 };//保存余数
int c[1000] = { 0 };//保存余数的位置
int i = 0;
int j = 0;
int t = 0;
int n = 0;while (1)
{
x = x % y;
for (j = 0; j <= i; j++)
{
if (x == b[j])
{
n = c[j];
t = 1;
break;
}
}
if (t == 1)
{
break;
}
b[i] = x;
c[i] = i;
x = x * 10;
a[i] = x / y;
i++;}
printf("0.");
for (j = 0; j < i; j++)
{
if (j == n)
{
printf("(");
}
printf("%d", a[j]);
}
printf(")");
}int main()
{
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
div(a, b);
printf("\n");
div(c, d);
}
- 1
信息
- 难度
- 7
- 分类
- (无)
- 标签
- 递交数
- 468
- 已通过
- 73
- 通过率
- 16%
- 被复制
- 11
- 上传者