找到给定输入的模式

时间:2018-05-12 17:26:07

标签: algorithm

我正在尝试从hackerrank.com中的给定输入输出数据中找到一个模式

input data :
1
2
3
4
5
10
11
15
20

output data :
1
2
3
4
5
11
22
66
111

问题没有任何描述,我必须找到处理输入成为输出的算法。

i thought the algorithm was to reverse the number position then add the numbers, examples :
10 +01 =11
15 +51 =66
11 + 11 =22

but it doesn't work for 20, 
20+02 =22?

任何想法?

2 个答案:

答案 0 :(得分:0)

我发现了一个可以满足输入>输出关系的逻辑。我个人讨厌这些问题,因为即使你的逻辑有效,它也可能与问题设计者不同。但无论如何,这是我的方法:

1-calculate (number of digits-1) for each number
2-add least significant digit of number to it
3-repeat result as much as (other digits of numbers+1)

然后就是这样:

1 -> add 1 to 0 (number of digits-1), repeat it 1 time (other digits = 0) -> result is 1
15 -> add 5 to 1 (number of digits-1), repeat it 2 times (other digits = 1) -> result is 66
20 -> add 0 to 1 (number of digits-1), repeat it 3 times (other digits = 2) -> result is 111

此方法适用于所有输入/输出对。但我不知道这是否是预期的方法。

答案 1 :(得分:0)

对于给定数字n,输出n - 回文数。前二十个是[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111]因此,例如n = 20 -> 111