PDA

View Full Version : các bạn xem thử dùm mình



kokono_py
18-03-2008, 09:32
đề bài c là tinh biểu thức sau:
s=(x^2)/1!-(x^4)/3!-(x^6)/5!+(x^8)/7!........
ban nhập vào số x va n cần tính
lưu ý là cứ một dấum+ thì tiếp theo là 2 dấu -
bạn nào gợi ý cho mình giải quyết mấy cái dấu +,- này đi.mình tạm thời chưa nghĩ ra
thanks nhiều

VuongChieuQuan
18-03-2008, 09:54
Cái này bạn dùng thêm hai cái biến tạm nữa đi, hoặc chỉ cần một thôi cũng được. Mình dùng 2 biến dem và dau cho nó sáng sủa nhé:


int i,dem,dau;
float s = 0;
for(i=1;i<=2*n;i++)
{
if(dem > 3)dem = 0;
dem++;
if(dem == 1) dau = 1;
if(dem > 1) dau = -1;
s = s + dau*(cái này bạn tính nhé);
}


À, cái tính tổng bạn cũng cần nghĩ thuật toán cho nó ổn nhé.

The Second Life
18-03-2008, 17:06
//s=(x^2)/1!-(x^4)/3!-(x^6)/5!+(x^8)/7!........
#include <stdio.h>
#include <conio.h>
#include <math.h>

float factorial(int n){
if (n<=1){
return 1;
} else{
return n*factorial(n-1);
}
}

float exp(int x,int n){
float s=0;
int i=2,sw=0;

for (;i<=n;i+=2,sw=1-sw){
if (sw){
s-=(pow(x,i)/factorial(i-1));
} else{
s+=(pow(x,i)/factorial(i-1));
}
}
return s;
}

void main(){
int x,n;
clrscr();
printf("Nhap x = ");fflush(stdin);scanf("%d",&x);
printf("Nhap n = ");fflush(stdin);scanf("%d",&n);
printf("s = %.2f",exp(x,n)) ;
getch();
}