Advertisement
nguyenhappy92

Tính tổng.

Oct 21st, 2015
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. //S(x,n) = -x + (x^2)/(1+2) - (x^3)/(1+2+3) + (x^4)/(1+2+3+4) + ... + (-1)n (x^n)/(1+2+3+⋯+n)
  2. // tinh S= -x + (x^2)/(1+2) + ...+ (-1)^n * x^n/ (1+2+3+...+n)
  3. // Khai bao cac ham thu vien neu co
  4. #include<stdio.h>
  5. #include<conio.h>
  6. void main()
  7. {
  8. int x,n;
  9. scanf("%d%d",&x,&n);
  10. long double T=1;
  11. long double S=0;
  12. long double tong=0;
  13. for(int i=1;i<=n;i++)
  14. {
  15. if(i%2!=0)
  16. {
  17. T=T*x;
  18. S=S+i;
  19. tong=tong -(long double) T/S;
  20. }
  21. else
  22. {
  23. T=T*x;
  24. S=S+i;
  25. tong=tong +(long double) T/S;
  26. }
  27. }
  28. printf("%.2lf",tong);// lay 2 chu so thap phan.
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement