1/6=0.16
你知道循环节是16,还是6啊?
写成1/6=0.1(6),就不会产生误解.
1/28=0.03(571428)
讲个思路来听下.
用java 编一个吧
基本思想:这个思想有点像十进制数求二进制数的过程,每商一个数都会得到一个相应的余数,当余数重复出现是,这时后面商的数肯定会与前面的一样,也就是开始循环了。
import java.io.*;
public class Output_1_DIV_n {
private int dividend; //被除数 开始为1
private int divesor; //除数 n ,一直不变
private int yushu; // 余数
private int[] existYuShu; //用来记录所有出现过的余数
private int existYuShuCount; //已经存在的余数的个数
private int MaxYuShuCount; //存储余数的最大个数
private StringBuffer quotient; //存储商的最后结果
public Output_1_DIV_n(int n){
this.dividend = 1;
this.divesor = n;
this.yushu = -1; //初始化余数,不能为`1
this.quotient = new StringBuffer();
this.MaxYuShuCount = 50;//存储余数的最大个数默认为50 个
this.existYuShu = new int[this.MaxYuShuCount];
this.existYuShuCount = 0;
}
public static void main(String[] args) {
int n = 1 ;
while(n!=0){
System.out.println("请输入一个整数,求它的1\\n,且不输出小数点后面循环的部分,输入0 退出");
try{
String num = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
num = br.readLine();
n = Integer.valueOf(num);
if(n<=0){
throw new NumberFormatException();
}
}catch(NumberFormatException e){
System.out.println("请输入一个大于0的整数");
continue;
}
catch(IOException ei){
}
Output_1_DIV_n o = new Output_1_DIV_n(n);
System.out.println(o.getResult());
}
}
public StringBuffer getResult(){
int i = 1; // i [1,9]; 商数
if (this.divesor == 1) {
this.quotient.append("1.0");
return this.quotient;
}
this.existYuShu[this.existYuShuCount++] = 1;
do {
if (this.dividend < this.divesor) {
this.quotient.append("0");
this.dividend *= 10;
} else {
for (i = 1; this.dividend - this.divesor*i>=this.divesor;i++){}
this.quotient.append(Integer.toString(i));
this.yushu = this.dividend-this.divesor*i;
this.dividend = this.yushu;
this.dividend *= 10;
if(this.yushu == 0){
this.quotient.insert(1,".");
return this.quotient;
}
}
}while(this.yushu!=0&&this.existYuShuCount<this.MaxYuShuCount&&!exist(this.yushu));
this.quotient.insert(1,".");
return this.quotient;
}
private boolean exist(int count){
for (int i = 0; i < this.existYuShuCount; i++) {
if (this.existYuShu[i] == count) {
return true;
}
}
if(this.yushu!=-1)
this.existYuShu[this.existYuShuCount++] = this.yushu;
return false;
}
}
//闲来无事,编一小程序,各位大哥大姐多多指点指点
麻烦“我不是郭靖”讲一下变成思路吧,看不懂啊。