C++ 中查詢 3,5,33,35,53… 數列的第 N 項的程式
本教程將討論一個查詢 3,5,33,35,53… 數列的第 N 項的程式。
為此,我們將獲得一個數字。我們的任務是在特定位置找到該數列的項。
示例
#include <bits/stdc++.h>
using namespace std;
//finding the nth term in the series
int printNthElement(int n){
int arr[n + 1];
arr[1] = 3;
arr[2] = 5;
for (int i = 3; i <= n; i++) {
if (i % 2 != 0)
arr[i] = arr[i / 2] * 10 + 3;
else
arr[i] = arr[(i / 2) - 1] * 10 + 5;
}
return arr[n];
}
int main(){
int n = 6;
cout << printNthElement(n);
return 0;
}輸出
55
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP