Adobe Interview Question
Software Engineer / DevelopersAs only xxx7 times xxx3 gets xxx1, the last digit of the other number is 7. Take this information, and given xxx3, formulate like this: A = 10*X + 3, B = 10*Y + 7. A is the given number, X is known, Y is integer. Now it is good to test from Y = 0 to n, until A*B = 1xxxxx1. O(n).
given n , construct a number 'k' with 1's greater than n.
while(true)
{
if k%n == 0, then return k
else k=k*10+1
}
private static int cal(int num) {
int total = 3;
int model = 100 % num;
int begin = 11;
while ((model + begin) % num != 0) {
begin = (model + begin) % num;
model = (10 * model) % num;
total++;
}
return total;
}
<pre lang="" line="1" title="CodeMonkey83779" class="run-this">#include <iostream>
- Anonymous August 10, 2011using namespace std;
int cal (int num) {
int total = 3;
int model = 100 % num;
int begin = 11;
while ((model + begin) % num != 0) {
begin = (model + begin) % num;
model = (10 * model)%num;
total++;
}
return total;
}
int main() {
int num = 0;
while (num % 10 != 3) {
cout << "input num: ";
cin >> num;
}
int loc = cal(num);
for (int i = 0; i < loc; i++)
cout << 1 ;
cout << endl;
return 0;
}
</pre><pre title="CodeMonkey83779" input="yes">1113</pre>