pesquisa

URI PROBLEMA 1158 - Soma de Ímpares Consecutivos III SOLUÇÃO EM C

URI Online Judge | 1158

Soma de Ímpares Consecutivos III

Adaptado por Neilor Tonin, URI Brasil
Timelimit: 1
Leia um valor inteiro N que é a quantidade de casos de teste que vem a seguir. Cada caso de teste consiste de dois inteiros X e Y. Você deve apresentar a soma de Y ímpares consecutivos a partir de X inclusive o próprio X se ele for ímpar. Por exemplo:
para a entrada 4 5, a saída deve ser 45, que é equivalente à: 5 + 7 + 9 + 11 + 13
para a entrada 7 4, a saída deve ser 40, que é equivalente à: 7 + 9 + 11 + 13

Entrada

A primeira linha de entrada é um inteiro N que é a quantidade de casos de teste que vem a seguir. Cada caso de teste consiste em uma linha contendo dois inteiros X e Y.

Saída

Imprima a soma dos consecutivos números ímpares a partir do valor X.



URI Online Judge | 1158

Sum of Consecutive Odd Numbers III

Adapted by Neilor Tonin, URI  Brazil
Timelimit: 1
Read an integer N that is the number of test cases that follows. Each test case contains two integers X and Y. Print one output line for each test case that is the sum of odd numbers from including it if is the case. For example:
for the input 4 5, the output must be 45, that is: 5 + 7 + 9 + 11 + 13
for the input 7 4, the output must be 40, that is: 7 + 9 + 11 + 13

Input

The first line of the input is an integer that is the number of test cases that follow. Each test case is a line containing two integer and Y.

Output

Print the sum of all consecutive odd numbers from X.

#include <stdio.h>
int main()
{
    int N, X, Y, a, b, c, d, e;
    scanf("%d", &N);
    for(a=1; a<=N; a++)
    {
        scanf("%d %d", &X, &Y);
        if(X%2==1)
        {
            c=0;
            for(b=1; b<=Y; b++)
            {
                c+=X;
                X+=2;
            }
            printf("%d\n", c);
        }
        else
        {
            X++;
            c=0;
            for(b=1; b<=Y; b++)
            {
                c+=X;
                X+=2;
            }
            printf("%d\n", c);
        }
    }
    return 0;
}

Postar um comentário

0 Comentários