Description
有一個分數序列:2/1,3/2,5/3,8/5,13/8,21/13... 求出這個數列的前n項之和。
Input
多測試用例,每個測試用例一行,每行是一個正整數n
Output
為每個測試用例單獨輸出一行:該數列的前n項之和。結果均保留小數點後10位。
Sample Input
1
2
3
Sample Output
2.0000000000
3.5000000000
#include
int main(void){
double i, n, j, x, sum, tem, x1, x2, y1, y2, y;
while(scanf("%lf", &n)!=EOF&&n>=0){
x1=2; x2=3; y1=1; y2=2; sum=0;
if(n==0) sum=-3.5;
else if(n==1) sum=-1.5;
else if(n==2) sum=0;
else
for(i=1; i<=n-2; i++){
x=x1+x2; x2=x1; x1=x; y=y1+y2; y2=y1; y1=y;
sum=sum+x/y;
}
printf("%.10f\n", sum+3.5);
}
return 0;
}
OJ反饋信息:wrong answer
不知道你寫的為啥不對,我寫了一個,c語言的
struct Node
{
double number;
Node* pNext;
Node()
{
number = 0;
pNext = NULL;
}
};
int main()
{
int n = -1;
Node* pNode = new Node;
Node* pHead = pNode;
Node* pPre = pNode;
while (scanf_s("%d", &n) != EOF)
{
pNode->number = n;
Node* pNext = new Node;
pNode->pNext = pNext;
pPre = pNode;
pNode = pNext;
}
delete pNode;
pPre->pNext = NULL;
pNode = pHead;
double sum = 0.0;
while (pNode->pNext != NULL)
{
double d = pNode->pNext->number / pNode->number;
sum += d;
printf("%.10f\n", sum);
pNode = pNode->pNext;
}
return 0;
}