ในปัญหานี้ เราได้รับค่าจำนวนเต็ม N ภารกิจของเราคือค้นหาเทอมที่ n ของอนุกรม -
9, 45, 243, 1377, 8019, …
มาดูตัวอย่างเพื่อทำความเข้าใจปัญหากัน
Input : N = 4 Output : 1377
แนวทางการแก้ปัญหา
วิธีแก้ปัญหาง่ายๆ ก็คือการหาพจน์ที่ N โดยใช้เทคนิคการสังเกต ในการสังเกตอนุกรม เราสามารถกำหนดได้ดังนี้ −
(1 1 + 2 1 )*3 1 + (1 2 + 2 2 )*3 2 + (1 3 + 2 3 )*3 3 … + (1 n + 2 n )*3 n
ตัวอย่าง
โปรแกรมเพื่อแสดงการทำงานของโซลูชันของเรา
#include <iostream>
#include <math.h>
using namespace std;
long findNthTermSeries(int n){
return ( ( (pow(1, n) + pow(2, n)) )*pow(3, n) );
}
int main(){
int n = 4;
cout<<n<<"th term of the series is "<<findNthTermSeries(n);
return 0;
} ผลลัพธ์
4th term of the series is 1377