这里所谓的“光棍”,并不是指单身汪啦~ 说的是全部由1组成的数字,比如1、11、111、1111等。传说任何一个光棍都能被一个不以5结尾的奇数整除。比如,111111就可以被13整除。 现在,你的程序要读入一个整数x,这个整数一定是奇数并且不以5结尾。然后,经过计算,输出两个数字:第一个数字s,表示x乘以s是一个光棍,第二个数字n是这个光棍的位数。这样的解当然不是唯一的,题目要求你输出最小的解。
提示:一个显然的办法是逐渐增加光棍的位数,直到可以整除x为止。但难点在于,s可能是个非常大的数 —— 比如,程序输入31,那么就输出3584229390681和15,因为31乘以3584229390681的结果是111111111111111,一共15个1。
输入格式:
输入在一行中给出一个不以5结尾的正奇数x(<1000)。
输出格式:
在一行中输出相应的最小的s和n,其间以1个空格分隔。
输入样例:
31
输出样例:
3584229390681 15
解答:感谢BigInteger!BigInteger!BigInteger!感谢StringBuilder!StringBuilder!StringBuilder!
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int userInput = sc.nextInt();
BigInteger x = new BigInteger(userInput+"");
StringBuilder sb = new StringBuilder("1");
BigInteger bachelor = null;
BigInteger s = null;
BigInteger remaider = null;
while(true) {
bachelor = new BigInteger(sb.toString());
remaider = bachelor.remainder(x);
if(remaider.intValue()==0) {
s = bachelor.divide(x);
break;
}
sb.append("1");
}
System.out.println(s.toString()+" "+bachelor.toString().length());
sc.close();
}
}