亞馬遜線上技術筆試題
阿新 • • 發佈:2018-12-25
public class chaif {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
p(10);
}
static void print(int[] r,int len){
for(int i=0;i<=len;i++){
System.out.print(r[i]+" ");
//cout<<r[i]<<" ";
}
System.out.println();
}
public static void p(int n) {
int sum = 0, len = 0;
while (sum < n) {
sum += len;
len++;
}
System.out.println(len);
len--;
int r[]=new int[len];
for (int i = 0; i < len; i++) {
r[i] = i + 1;
}
int k = len - 2;
sum-=r[len-1];
while (true) {
if (k == 0 && r[k] == n) {
break;
}
if (sum < n) {
r[k + 1] = r[k] + 1;
k++;
sum += r[k];
} else {
if (sum == n) {
print(r, k);
}
sum -= r[k];
r[--k]++;
sum++;
}
}
}
}