o1-mini-2024-09-12· OpenAI
WildChat-4.8M (AI2)
برنامهای بنویسید که مقدار e x e x را با کمک رابطهی زیر محاسبه کند(تا سه رقم اعشار).…
- User
برنامهای بنویسید که مقدار e x e x را با کمک رابطهی زیر محاسبه کند(تا سه رقم اعشار). این برنامه مقدار x x و تعداد جملات سری، n n، را از ورودی دریافت میکند.
e x
1 + x 1 ! + x 2 2 ! + x 3 3 ! + . . . e x =1+ 1! x + 2! x 2
+ 3! x 3
+...
ورودی در خط اول عدد x x و در خط بعدی عدد n n به شما داده میشود. 0 ≤ n , x ≤ 1
000 0≤n,x≤1 000
خروجی در تنها خط خروجی مقدار e x e x را با دقت سه رقم اعشار چاپ کنید.
مثال ورودی نمونه ۱ 5 10 Plain text خروجی نمونه ۱ 143.689 Plain text ورودی نمونه ۲ 3 15 Plain text خروجی نمونه ۲ 20.086 c++
- Assistantcpp
#include <bits/stdc++.h> #include <iomanip> using namespace std; int main(){ double x; int n; cin >> x >> n; double sum = 1.0; // Initialize with the 0th term double term = 1.0; for(int k=1; k<=n; ++k){ term *= x / k; sum += term; } // To handle cases where the series might not converge properly, // especially for large n and x, we can limit the maximum sum // However, based on the problem constraints, we'll proceed normally. // Print the result with three decimal places cout << fixed << setprecision(3) << sum; return 0; }