class Solution {
public:
int nthTermOfAP(int A1, int A2, int N) {
int d=A2-A1。
int current=A1;
int arr[10] 。
for (int i=0; i<N; i )
{
arr[i]=current;
current d;
}
cout<< arr[N-1]。
}
};
// { Driver Code Starts.
int main() {
int t;
cingt;> t。
while (t--) {
int A1, A2, N;
cingt;> A1 >> A2 >> N。
解決方案ob。
cout << ob.nthTermOfAP(A1, A2, N) << "
"。
}
}
// } 驅動代碼結束
我得到的輸出是26295968。請解釋一下為什么我得到這么大的數字?我是否必須將任何數字慣化為0?
uj5u.com熱心網友回復:
你的nthTermOfAP函式沒有回傳任何值。因此,當你試圖喊出沒有回傳任何值的函式時,它將給你一個像26295968的輸出。
所以你可以做兩件事:
- 在你的 nthTermOfAP 函式中添加一個回傳值。
- 簡單地洗掉
cout中的cout << ob.nthTermOfAP(A1, A2, N) << " ";在你的int main()中。并使函式的資料型別變成void而不是int。使用void時,你不應該給出一個回傳值。
uj5u.com熱心網友回復:
你的函式的回傳型別應該是void而不是int。然后你可以洗掉cout命令。 取而代之的是 current d。 你應該使用 current = current d;
for (int i=0; i<N;i )
{
arr[i]=current;
current = current d;
}
cout<<arr[N-1] 。
你也可以使用Ap= a (n-1)*d的第n項方程
。轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/319964.html
標籤:
