Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 4480 | Accepted: 2454 | Special Judge |
Description
Input
Output
Sample Input
5 7 12 15 77 0
Sample Output
1 2 4 5 1 2 4 6 7 1 2 4 8 12 1 2 4 5 10 15 1 2 4 8 9 17 34 68 77
题目大意:输入一个数字n,输出一个数列,要求数列的第一个为1,后面的由前面的数字是前面数字中人选两个想家的和,可以选两个相同的数字,亦即对于任意i和j (0<=i, j<=k-1) 有 ak=ai+aj,输出长度最短的一个序列。
#include <stdio.h> #include <iostream> #include <string.h> using namespace std; int MinLen; int n; int result[10005] = {1, 2}; int temp[10005] = {1, 2}; void DFS(int nLen, int num) { if (nLen >= MinLen || num > n) { return; } if (num == n) { if (MinLen > nLen) { MinLen = nLen; for (int i = 2; i < nLen; i++) { result[i] = temp[i]; } } return; } for (int i = nLen - 1; i >= 0; i--) { temp[nLen] = temp[i] + temp[nLen - 1]; DFS(nLen + 1, temp[nLen]); } } int main() { while(scanf("%d", &n) != EOF && n != 0) { if (n == 1) { printf("1\n"); continue; } MinLen = 0x7fffffff; DFS(2, 2); printf("%d", result[0]); for (int i = 1; i < MinLen; i++) { printf(" %d", result[i]); } printf("\n"); } return 0; }