为什么超时

elk 2023-07-06 11:45:12 22
#include<iostream>
using namespace std;
int step(int x){
	if(x==1) return 1;
	if(x==2) return 2;
	if(x==3) return 4;
	return step(x-1)+step(x-2)+step(x-3);
} 
int main(){
	int a;
	while(cin>>a){
		if(a==0){
			break;
		}
		cout<<step(a)<<endl;
	}
	return 0;
}
{{ vote && vote.total.up }}

共 4 条回复

tctm229

#include <bits/stdc++.h> using namespace std; long long n, a[101] = { 1, 2, 4 }; int main() { for (int i = 3; i < 71; i++) { a[i] = a[i - 1] + a[i - 2] + a[i - 3]; } while (cin >> n && n != 0) { cout << a[n - 1] << endl; } return 0; }

root 站长

用递推。

Even226 逗比

QWQ

Even226 逗比

我也超时了