Problem 1017 Playing with Calculator

来源:http://acm.fzu.edu.cn/problem.php?pid=1017

Problem Description
Pete is playing a game. He types on a calculator a natural number K and then presses the ‘+’ key. The display still shows K. Pete wants to have on the display a number that is formed using only one figure. In order to get that he presses, if it is necessary, the ‘=’ key several times (maybe 0). The result after the first pressing is K+K. After each pressing the number K is added to the sum. Find out whether Pete will achieve his goal or not, and what number, consisting all of identical figures, he will get first. Consider the capacity of the calculator to be unlimited.

K < 1000

Input
The input file contains the number K.

Process to the end of file.

Output
The output file must contain two numbers: first the figure that forms the result and then the number of digits of the result. If multiple solution exists, first minimize the figure then minimize the number of digits. If finding the desired result is impossible, the output file must contain only one number -1 (minus 1). The numbers in the output file must be separated by one space.
Sample Input
37
Sample Output
1 3
题意:输入N 如果N的倍数里右全位某个数值的数 比如 111 那么输出这个数的组成部分 即1 和他的个数 这里有 3个1 所以 37 +37 +37 =111 输出 1 3
思路:这题没什么思路,参看了大神的代码 把所有情况都列举出来,看看是不是 N的倍数。。
但是有一点我没有弄清楚。就是代码中 关于 结果的位数 为什么只搜索到N 就停止搜索了。
代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
using namespace std;

int main()
{
int n, i, j;
while (cin>>n)
{
int f[1000];
f[0] = 0;
if (n/10==0)
{
printf("%d 1\n", n);
continue;
}
for (i = 1; i<10; i++)
{
for (j = 1; j<n; j++) //就是这为什么J小于N 我不是很理解
{ //我的猜测是当j大于N 与J 小于N的情况重复了 所以直接排除
f[j] = (f[j - 1] * 10 + i) % n;
if (!f[j])
{
printf("%d %d\n", i, j);
break;
}
}
if (j != n)
break;
}
if (i == 10)
printf("-1\n");
}
return 0;
}