-4
我想調用這個遞歸方法,但我不明白爲什麼它沒有達到else語句返回mul。我在主函數中傳遞值0,它小於n,並且在x變得大於n時,它應該移動到else函數。控制到達非空函數結束
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n,mul=0;
vector <int> a;
int calc(int x)
{ int mul,chk;
int l=n;
if(x<n)
{
chk=a[x]*(l-1);
l--;
if(mul<chk)
{
mul=chk;
}
calc(x+1);
}
else
{ return mul;}
}
int main() {
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
sort(a.begin(),a.end());
int z=calc(0);
cout<<z;
return 0;
}
「它沒有達到else語句」。示例代碼中的任何地方都沒有「else」關鍵字。 –
正確地格式化您的代碼,將使診斷此問題的源更加容易。 –