Tuesday, December 2, 2014

Bài 14

Bài 14: Tính S(x,n) = x + x^3 + x^5 + ... + x^(2n+1)

//========================================
//Cách 1

#include <stdio.h>
#include <conio.h>

void main()
{
    float x;
    scanf("%f", &x);
    int n;
    scanf("%d", &n);
    float S = x;
    float T = x;
    int i = 3;
    while (i <= 2*n + 1)
    {
        T = T * x * x;
        S = S + T;
        i = i + 2;
    }
    printf("%f", S);
}

//========================================
//Cách 2
/*
#include <stdio.h>
#include <conio.h>

float Tong(float,int);
void main()
{
    float x;
    scanf("%f", &x);
    int n;
    scanf("%d", &n);
    float kq = Tong(x,n);
    printf("%f", kq);
}

float Tong(float y, int k)
{
    float S = y;
    float T = y;
    for (int i=3; i<=2*k+1; i=i+2)
    {
        T = T * y * y;
        S = S + T;
    }
    return S;
}
*/

No comments:

Post a Comment