这道题改用数组做的话,效率会更高。我用的是向量。解题思路很简单,就是按照顺序将f(n)列出来,输出你要的那个值。 // Self-describing Sequence.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <vector> #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { long n; vector<long> f; while(cin>>n){ if(n==0)break; f.push_back(1); f.push_back(2); f.push_back(2); long i=2; long z=3; while(z<=n){ //if(z==n)break; long x=1; while(x<=f[i]){ //if(z==n)break; f.push_back(i+1); ++z; ++x; } ++i; } cout<<f[n-1]<<"/n"; f.clear(); } vector<long>().swap(f); system("pause"); return 0; }