历届试题 小数第n位

问题描述

  我们知道,整数做除法时,有时得到有限小数,有时得到无限循环小数。
  如果我们把有限小数的末尾加上无限多个0,它们就有了统一的形式。


  本题的任务是:在上面的约定下,求整数除法小数点后的第n位开始的3位数。

输入格式

  一行三个整数:a b n,用空格分开。a是被除数,b是除数,n是所求的小数后位置(0<a,b,n<1000000000)

输出格式

  一行3位数字,表示:a除以b,小数后第n位开始的3位数字。

样例输入

1 8 1

样例输出

125

样例输入

1 8 3

样例输出

500

样例输入

282866 999000 6

样例输出

914

注意:x~y为小数的循环节的条件是  在x,y两个位置作除法运算时得到的余数相等,而不是除数相等

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

	static int s[];
	static int vis[];
	static int l;
	static Map<Integer,Integer> ma;
	public static Integer div(int a , int b) {
		l = 1;
		while(true) {
			a = a%b;
			
			if(ma.containsKey(new Integer(a%b))) {
				return ma.get(new Integer(a%b));
			}else {
				ma.put(new Integer(a%b), new Integer(l)); 
			}
			a *= 10;
			int temp = (int)a/(int)b;
			s[l++] = temp;
//			System.out.println(l);
//			System.out.println(temp);
			a = a % b; 
		}
	}
	public static void out(int n , int up , int cir) {
		if(n < up) System.out.print(s[n]);
		else System.out.print(s[(n-up)%cir+up]);
	}
	 
	public static void main(String args[]) {
		Scanner scan = new Scanner(System.in);
		s = new int[1111111];
		vis = new int[111111];
		ma = new HashMap<Integer,Integer>(); 
		int a = scan.nextInt();
		int b = scan.nextInt();
		int n = scan.nextInt();
		  
		int up = div(a,b) ;
		
// 		System.out.println(cir);
 		int cir = l-up;
//		System.out.println(cir);
 		out(n,up,cir);out(n+1,up,cir);out(n+2,up,cir);
		
		
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值