思路:
&&的短路特性:A&&B中,假如A爲假,那麼B就不會被運算。
因此我們只要將 遞歸放在B中,而將終止條件放在A中即可解決該問題。
代碼:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long int LL;
LL add(LL a,LL &sum)
{
a&&add(a-1,sum);
return sum+=a;
}
int main()
{
LL n;
while(~scanf("%lld",&n))
{
LL ans=0;
add(n,ans);
printf("%lld\n",ans);
}
return 0;
}