C ++练习中的复发 [英] Recurrence in C++ exercise

查看:70
本文介绍了C ++练习中的复发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在学习算法,我有一个功课。我必须输出



I am still learn about algorithms, i have a homework. I must make an output

Sum of : 1/2 + 1/4 + 1/6 - 1/8 + 1/10 + 1/12
Result : 0.975





但我的程序输出



But output of my program

Sum of : 1/2 + 1/4 + 1/6-1/8 + 1/10 + 1/12
Result : 0.975



我不知道如何制作空间负号,如果我使用cout会出现两次负号。



我的节目




I dont know how to make space `negative sign`, if i use cout there will show twice negative sign.

my program

#include <iostream>
    #include <math.h>
    using namespace std;
    int main ()
    {
        int i ,sign, p, q, n;
        double x , S;
        S=0;
        cout << "Sum of :";
        for (i=1; i <= 6; i++)
        {
            if ( (i % 4 == 0) && ( i > 1 ) ) // to make condition where the number become negative
            {
                sign = -1;
    
    
            }
            if ( ( i % 4 != 0 ) && ( i > 1 ) )  // to make condition where the number become positive
            {
                sign = 1;
                cout << " + ";
            }
            if ( i == 1 ) // to prevent 1st number not show " + " symbol
            {
                sign =1;
            }
    
            p = sign*1;
            q = ( 2 * ( i - 1 ) ) + 2;
            cout << p << "/" << q;
            x = ( 1.0 * p / q );
            S = S + x;
    
        }
    
            cout << "\n" << S;
    }



我意识到我的程序太多了不需要的操作,你能帮我把它变得更有效吗?



我尝试过:



我的计划输出


I realize my program too many operation that not needed, could u help me make it more effecient ?

What I have tried:

Output of my program

Sum of : 1/2 + 1/4 + 1/6-1/8 + 1/10 + 1/12
Result : 0.975

推荐答案

引用:

我不知道如何制作空间`负号',如果我使用cout会显示两次负号。

I dont know how to make space `negative sign`, if i use cout there will show twice negative sign.

你的错误是你试图添加一个负数而不是减去一个正数。

Your mistake is that you try to add a negative number instead of substract a positive number.

#include <iostream>
#include <math.h>
using namespace std;
int main ()
{
	int i;
	double x , S;
	cout << "Sum of :";
	
	// first 
	i = 2;
	s= ( 1.0 / i );
	cout << "1/" << i;
	
	for (i=4; i <= 12; i+= 2)
	{
		x = ( 1.0 / i );
		if ( (i % 8 == 0)) { // to make condition where substration
			cout << " - ";
			S = S - x;
		} else {
			cout << " + ";
			S = S + x;
		}
		cout << "1/" << i;
	}
	cout << "\nResult : " << S;
}


请不要太认真对待这个答案。它可能适合也可能不合适。



而不是正1/4减1/8,跳过1/4并使用正1/8。结果值相同。



总和:1/2 + 1/6 + 1/8 + 1/10 + 1/12

结果:0.975



否则,jeron的回答对我来说很好。



如果你*必须*拥有所有六个条款,这是另一个条款:



Please don't take this answer too seriously. It may or may not be appropriate.

Instead of a positive 1/4 minus 1/8, skip the 1/4 and use a positive 1/8. The resulting value is the same.

Sum of : 1/2 + 1/6 + 1/8 + 1/10 + 1/12
Result : 0.975

Otherwise, jeron's answer looks good to me.

If you *must* have all six terms, here's another take:

#include <iostream>
#include <math.h>

#define countof(arg) ( (sizeof arg) / (sizeof arg[0]) )

int main ()
{
    static const int denominators[] = {2, 4, 6, -8, 10, 12};
    double S = 0.f;
    std::cout << "Sum of :";
    for (int i = 0; i < countof(denominators); i++)
    {
        int denom = denominators[i];
        if (denom < 0)
            std::cout << " - ";
        else if (i > 0)
            std::cout << " + ";
        std::cout << 1 << '/' << abs(denom);
        double term = 1.f / denom;
        S += term;
    }

    std::cout << std::endl << S;
    return 0;
}


快速尝试。

A quick attempt.
#include <iostream>
    #include <math.h>
    using namespace std;
    int main ()
    {
        int i, q;
        double x , S;
        S=0;
        cout << "Sum of :";
        for (i=1; i <= 6; i++)
        {
            if ( (i % 4 == 0) && ( i > 1 ) ) // to make condition where the number become negative
            {
                cout << " - ";
            }
            if ( ( i % 4 != 0 ) && ( i > 1 ) )  // to make condition where the number become positive
            {
                cout << " + ";
            }
             q = ( 2 * ( i - 1 ) ) + 2;
            cout << 1 << "/" << q;
            x = ( 1.0 / q );
            S = S + x;
    
        }
    
            cout << "\n" << "Result = " <<S;
    }


这篇关于C ++练习中的复发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆