标题:求助!一道编程计算题--数学决定编程的上限!
取消只看楼主
Eahonxu
Rank: 1
等 级:新手上路
帖 子:113
专家分:0
注 册:2016-7-13
结帖率:96.15%
已结贴  问题点数:20 回复次数:2 
求助!一道编程计算题--数学决定编程的上限!

原题如下:考虑两个无线序列:
1.0+1.0/2.0+1.0/3.0+1.0/4.0+……
1.0-1.0/2.0+1.0/3.0-1.0/4.0+……
编写一个程序来计算这两个序列不断变化的总和,直到达到某个次数。让用户交互地输入这个次数。
我自己编的程序如下所示:
#include <stdio.h>
int main(void)
{
    unsigned n, x, i;
    float sum1_n, sum2_n;
   
    while(1)
    {
        printf("请输入x的值:\n");
        i = scanf("%d", &x);
        if(i != 1)break;
        while(getchar() != '\n');
        sum1_n = 0;
        sum2_n = 0;
        
        if(x == 1)
        {
            sum1_n = 1;
            sum2_n = 1;
            printf("%f %f", sum1_n, sum2_n);
        }
        if(x == 2 * n)
        {
            for(n = 1; n <= x; n++)
            {
                sum2_n += 1 / (n-1) - 1 / n;
                sum1_n += 1 / n;
            }
            
            printf("%f %f", sum1_n, sum2_n);
        }
        else
        {
            for(n = 1; n <= x; n++)
            {
                sum2_n += 1 / n - 1 / (n - 1) + 1; // Thread1:EXC_ARITHMETIC(code=EXC_I386_DIV,subcode=0x0)[color=#FF0000]
                sum1_n += 1 / n;
            }
            printf("%f %f", sum1_n, sum2_n);
        }
        return 0;
    }
}

结果则如下所示
[local]1[/local]
输入任意结果都是显示错误,上方红字被提示有误,请示大神该如何修改呢?
搜索更多相关主题的帖子: include 数学 无线 用户 
2016-08-19 20:30
Eahonxu
Rank: 1
等 级:新手上路
帖 子:113
专家分:0
注 册:2016-7-13
得分:0 
回复 2楼 ehszt
#include <stdio.h>
int main(void)
{
    unsigned n, x, i;
    float sum1_n, sum2_n;
   
   
    while(1)
    {
        printf("请输入x的值:\n");
        i = scanf("%d", &x);
        if(i != 1)break;
        while(getchar() != '\n');
        sum1_n = 0;
        sum2_n = 0;
        
        if(x == 1)
        {
            sum1_n = 1;
            sum2_n = 1;
            printf("%f %f\n", sum1_n, sum2_n);
        }
        if(x % 2 == 0)
        {
            for(n = 2; n <= x; n++)
            {
                sum2_n += 1 / (n-1) - 1 / n;
                sum1_n += 1 / n;
            }
            
            printf("%f %f\n", sum1_n, sum2_n);
        }
        if((x % 2 == 1)&& (x != 1))
        {
            for(n = 2; n <= x; n++)
            {
                sum2_n += 1 / n - 1 / (n - 1) + 1;
                sum1_n += 1 / n;
            }
            printf("%f %f\n", sum1_n, sum2_n);
        }
        
    }
    return 0;
}
修改了下程序,然并软。
2016-08-19 21:14
Eahonxu
Rank: 1
等 级:新手上路
帖 子:113
专家分:0
注 册:2016-7-13
得分:0 
回复 4楼 ehszt
怎么算的--我高数荒废了两年了哎
2016-08-19 22:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-468037-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.141763 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved