1
3 5 7
9 11 13 15 17
19 21 23 25 27 29 31
...
在某一列Joana寫下了55個奇數數字,你可以看出該列最後3個數字的和嗎?
給你一個數字N,代表某一列有N個奇數數字,你的任務是把該列最後三個數加起來。
Input 輸入含有多組測試資料。每組測試資料一列,有一個數字 N,表示某一列有 N 個奇數數字(1 < N < 1000000000)。
Input
對每組測試資料,輸出該列的最後三個數字的和。本問題中保證三個數字的和一定小於263。Sample Iutput
3
5
7
Sample Output
15
45
87
#include<stdio.h>
#include<stdlib.h>
int main()
{
int N,num = 0;
scanf("%d",&N);
for (int k = N;k >= 1; k -= 2)
{
num += k;
}
int *odd = NULL;
odd = (int*)malloc(num*sizeof(int)); /*動態產生一個size為num的陣列*/
for (int i = 1;i <= N; i += 2)
{
for (int j = 0 ;j < i; j++)
{
static int score = -1; /*奇數*/
static int x = 0;
score += 2;
*(odd+x) = score; /*將score寫入odd動態陣列*/
x++;
}
}
int sum = 0;
for (int i = 0;i < 3; i++)
{
sum += *(odd+(--num)); /*求最後三個數字的和*/
}
printf("%d\n",sum);
system("pause");
return 0;
}
沒有留言:
張貼留言