直接上代码:
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);
}
}