Ones
Description
Given any integer 0 ≤ n ≤ 10000 not divisibleby 2 or 5, some multiple of n is a number whichin decimal notation is a sequence of 1’s. Howmany digits are in the smallest such a multipleof n?
Input
A file of integers at one integer per line.
Output
Each output line gives the smallest integer x > 0such that p =∑x−1i=0 1×10i = a×b, where a is thecorresponding input integer, and b is an integergreater than zero.Sample Input379901Sample Output
Sample Input
3
7
9901
Sample Output
3
6
12
原题链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19740
题意:输入一个定不能被2和整除的非负整数n,求它的最小倍数,全由一组成(十进制),即求至少需要多少个1(十进制)将其整除.
例如,n=3时,111 % 3 == 0,所以输出3
n=7是,111 111 % 3 == 0,所以输出6
解题思路:
原来打算"暴力求解",即:x=1,11,111...依次试下去,但不对!样例第三个就无法输出(数字太大了...),
后来发现,如果还没有找到,只要上次的余数*10+1就可以了,大大简化了计算.
AC代码:
#include <iostream>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
int ans=1,time=1;
while(ans%n)
{
ans=ans%n*10+1;
//ans=ans*10+1;
time++;
}
cout<<time<<endl;
}
return 0;
}