本文介绍了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;
}


#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;
}


#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 ++练习中的复发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 06:34