一个简单的模拟题目,不过要小心括号的位置,做一点提示,用一个数组来记录每个余数出现的位置,如果出现了相同的余数那就发现了循环,上一次出现的地方就是左括号的位置,因为是模拟题,不建议看我的代码,所以就不写注释了
Code
/**//*
ID: sdjllyh1
PROG: fracdec
LANG: JAVA
complete date: 2008/12/25
author: LiuYongHui From GuiZhou University Of China
more article: www.cnblogs.com/sdjls
*/
import java.io.*;
import java.util.*;
public class fracdec
{
private static int n, d;
private static int quotient, remainder;
private static int[] decimals = new int[100000];
private static int decimalLength = 0;
private static int[] firstAppearance;
private static int bracketPos = -1;
public static void main(String[] args) throws IOException
{
init();
run();
output();
System.exit(0);
}
private static void run()
{
quotient = n / d;
remainder = n % d;
if (remainder != 0)
{
firstAppearance[remainder] = 0;
while ((remainder != 0) && (bracketPos == -1))
{
remainder *= 10;
if (firstAppearance[remainder % d] == -1)
{
decimals[decimalLength] = remainder / d;
decimalLength++;
firstAppearance[remainder % d] = decimalLength;
remainder %= d;
}
else
{
decimals[decimalLength] = remainder / d;
decimalLength++;
bracketPos = firstAppearance[remainder % d];
}
}
}
else
{
decimalLength = 1;
}
}
private static void init() throws IOException
{
BufferedReader f = new BufferedReader(new FileReader("fracdec.in"));
StringTokenizer st = new StringTokenizer(f.readLine());
n = Integer.parseInt(st.nextToken());
d = Integer.parseInt(st.nextToken());
firstAppearance = new int[d];
Arrays.fill(firstAppearance, -1);
f.close();
}
private static void output() throws IOException
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("fracdec.out")));
out.print(quotient + ".");
int length = String.valueOf(quotient).length() + 1;
for (int i = 0; i < decimalLength; i++)
{
if (i == bracketPos)
{
out.print('(');
length++;
if ((length + i) % 76 == 0)
{
out.println();
}
}
if (((length + i) % 76 == 0) && (i != decimalLength - 1))
{
out.println();
}
out.print(decimals[i]);
}
if (bracketPos != -1)
{
out.println(')');
}
else
{
out.println();
}
out.close();
}
}
/**//*
ID: sdjllyh1
PROG: fracdec
LANG: JAVA
complete date: 2008/12/25
author: LiuYongHui From GuiZhou University Of China
more article: www.cnblogs.com/sdjls
*/
import java.io.*;
import java.util.*;
public class fracdec
{
private static int n, d;
private static int quotient, remainder;
private static int[] decimals = new int[100000];
private static int decimalLength = 0;
private static int[] firstAppearance;
private static int bracketPos = -1;
public static void main(String[] args) throws IOException
{
init();
run();
output();
System.exit(0);
}
private static void run()
{
quotient = n / d;
remainder = n % d;
if (remainder != 0)
{
firstAppearance[remainder] = 0;
while ((remainder != 0) && (bracketPos == -1))
{
remainder *= 10;
if (firstAppearance[remainder % d] == -1)
{
decimals[decimalLength] = remainder / d;
decimalLength++;
firstAppearance[remainder % d] = decimalLength;
remainder %= d;
}
else
{
decimals[decimalLength] = remainder / d;
decimalLength++;
bracketPos = firstAppearance[remainder % d];
}
}
}
else
{
decimalLength = 1;
}
}
private static void init() throws IOException
{
BufferedReader f = new BufferedReader(new FileReader("fracdec.in"));
StringTokenizer st = new StringTokenizer(f.readLine());
n = Integer.parseInt(st.nextToken());
d = Integer.parseInt(st.nextToken());
firstAppearance = new int[d];
Arrays.fill(firstAppearance, -1);
f.close();
}
private static void output() throws IOException
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("fracdec.out")));
out.print(quotient + ".");
int length = String.valueOf(quotient).length() + 1;
for (int i = 0; i < decimalLength; i++)
{
if (i == bracketPos)
{
out.print('(');
length++;
if ((length + i) % 76 == 0)
{
out.println();
}
}
if (((length + i) % 76 == 0) && (i != decimalLength - 1))
{
out.println();
}
out.print(decimals[i]);
}
if (bracketPos != -1)
{
out.println(')');
}
else
{
out.println();
}
out.close();
}
}