← 返回 google 的题目列表Generate All Strings of Ones and Twos That Sum to N
类型:online_judge
Given a positive integer N, output every string consisting only of characters '1' and '2' such that the sum of its character values equals exactly N.
For example, for N = 3, the valid strings are 111, 12, and 21.
Print results in lexicographic order ('1' before '2') and analyze time and space complexity.
Input
N
Output
Print one valid string per line.
Constraints
1 <= N <= 30
The number of outputs may be exponential; the complexity analysis must include output size.
Example
Input
1
Output
1