我刚开始这样写的,MLE了:
#include<bits/stdc++.h>
using namespace std;
int f(int a,int b)
{
return f(a-1,f(a-1,b));
}
int main()
{
int a,b,c,d;cin>>a>>b>>c>>d;
if(a==1)
{
cout<<(c*b+d)%10;
}
else
{
cout<<f(a-1,b);
}
return 0;
}
原因是我的的递归里没有写返回条件,无限循环了。我们应该在递归里加入如果a==1,那么应计算Cb+D,然后结束递归,返回结果。
这题考察对于递归的理解,套公式写就可以了
#include<bits/stdc++.h>
using namespace std;
int a,b,c,d;
int f(int a,int b,int c,int d)
{
if(a==1)
{
return (c*b+d)%10;
}
else
{
return f(a-1,f(a-1,b,c,d),c,d);
}
}
int main()
{
cin>>a>>b>>c>>d;
cout<<f(a,b,c,d);
return 0;
}