找数字

直接上代码:

import java.math.BigInteger;
import java.util.Scanner;

/*
 * 找数字
从键盘输入一个奇数p,其个位数字不是5,求一个整数S,使p*s=11....11。要求在屏幕上依次输出以下结果:
(1)S。
(2)乘积的数字位数。
样例输入:p=123

          S=903342366757
          15位
样例输入:127
样例输入:p=127

          S=874890638670166229221347331583552055993
          42位
 */
public class Main {
    public static final int MAX=100;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
//          String p = scanner.next();
//          findNum(p);
            int p=scanner.nextInt();
            findNum(p);
        }
    }
    /*
     * 使用bigInteger和StringBuilder进行处理
     */
    public static void findNum(String p){

        StringBuffer sBuffer=new StringBuffer();
        for(int i=0;i<=p.length();i++){
            sBuffer.append("1");
        }
        BigInteger divisor=new BigInteger(p);
        BigInteger bigInteger2=new BigInteger(sBuffer.toString());
        while(!bigInteger2.remainder(divisor).toString().equals("0")){
            sBuffer.append("1");
            bigInteger2=new BigInteger(sBuffer.toString());
        }
        System.out.println(bigInteger2.toString()+"\n"+bigInteger2.toString().length());


    }
    /*
     * 传统的方式做
     * 实践证明,字符串与数字的转化是很费时间的。
     */
    public static void findNum(int p){
        int temp=1, k=1,j=0;  
        while(temp<p){  
            temp=temp*10+1;  
            k++;  
        }  

        do{  
            System.out.printf("%d",temp/p);  //不断地输出最左边,尾数的增加,不影响最左边的输出
            k++;  
            j=temp%p;  
            temp=j*10+1;  
        }while(j!=0);  

        System.out.printf("\n%d\n",k-1);  
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值