Solution of 913 - Joana and the Odd Numbers

Problem Description
source:https://uva.onlinejudge.org/external/9/913.html

Joana loves playing with odd numbers. In the other day, she started writing, in each line, an odd number of odd numbers. It looked as follows:


3 5 7 
9 11 13 15 17 
19 21 23 25 27 29 31
......

    On a certain line Joana wrote 55 odd numbers. Can you discover the sum of the last three numbers written in that line? Can you do this more generally for a given quantity of odd numbers? 
    Given the number N of odd numbers in a certain line, your task is to determine the sum of the last three numbers of that line. 

Input 

The input is a sequence of lines, one odd number N (1 < N < 1000000000) per line 

Output 

For each input line write the sum of the last three odd numbers written by Joana in that line with N numbers. This sum is guaranteed to be less than 263 .

Sample Input 





Sample Output 

1
45 
87

Solution:
#include<stdio.h>
int main()
{
    long int n,lineno, value,result;
    while(scanf("%ld",&n)==1)
    {
        lineno=(n+1)/2;
        value=lineno*lineno*2-1;
        result=value+(value-2)+(value-4);
        printf("%ld\n",result);
    }
    return 0;
}

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience