参考博客:
题目链接:https://leetcode.com/problems/smallest-integer-divisible-by-k/
解题思路:博客2的解题思路容易理解,但是暴力法的上限为100000,在博客1中解释了,但是没怎么看懂,先这样.另外,在计算v值的时,不用把真实的值计算出来,因为t的最大值达到10100000,超过int表示范围,此处做一个trick处理:若a被k整除,那个10*a也一定能被k整除.
解题代码:
class Solution {
public:
int smallestRepunitDivByK(int K) {
int t=0;
for(int i=1;i<=100000;i++){
t=(t*10+1)%K;
if(t==0) return i;
}
return -1;
}
};