913 - Joana and the Odd Numbers

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:

 1
 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?

Problem

 

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.

Joana喜欢玩关于奇数的游戏。有一天,她开始写,每列都是奇数,如下表。
 
1 3 5 7 9 11 13 15 1719 21 23 25 27 29 31...在某一列Joana写下了55个奇数数字,你可以看出该列最后3个数字的和吗?给你一个数字N,代表某一列有N个奇数数字,你的任务是把该列最后三个数加起来。
Input

输入含有多组测试资料。

每组测试资料一列,有一个数字N,表示某一列有N 个奇数数字(1 < N < 1000000000)。
Output

对每组测试资料,输出该列的最后三个数字的和。本问题中保证三个数字的和一定小于263。

Sample Input

 

3
5
7

 

Sample Output

 

15
45
87

解题思路:先算行数找到下一行的头一个数字,再倒回去计算要求的三个数的和
#include<stdio.h>
int main()
{long long n,h,a,s[3]={0},sum;
while(scanf("%lld",&n)!=EOF){sum=0;
                            h=(n+1)/2;
                            a=(h+h*(h-1))*2+1;
                            s[0]=a-2;
                            s[1]=a-4;
                            s[2]=a-6;
                            sum=s[0]+s[1]+s[2];
                            printf("%lld\n",sum);
                            }
return 0;
}

posted on 2013-02-05 10:22  喂喂还债啦  阅读(479)  评论(0编辑  收藏  举报