Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 数学习题中的复现_C++_Recurrence - Fatal编程技术网

C++ 数学习题中的复现

C++ 数学习题中的复现,c++,recurrence,C++,Recurrence,我还在努力学习算法,我有一个家庭作业。我必须做出一个输出 Sum of : 1/2 + 1/4 + 1/6 - 1/8 + 1/10 + 1/12 Result : 0.975 我意识到我的程序有太多可以避免的操作,你能帮我提高效率吗 cout << p << "/" << q; cout如果您希望使用递归,正如您在主题中所指出的那样,那么以下是您也可以参考的内容 static void recurse(int i, int limit){

我还在努力学习算法,我有一个家庭作业。我必须做出一个输出

Sum of : 1/2 + 1/4 + 1/6 - 1/8 + 1/10 + 1/12 Result : 0.975 我意识到我的程序有太多可以避免的操作,你能帮我提高效率吗

cout << p << "/" << q; 

cout如果您希望使用递归,正如您在主题中所指出的那样,那么以下是您也可以参考的内容

    static void recurse(int i, int limit){
            int sign = 0, p, q, n;
            double x, S;
            S =0;
            if (i == 1) // to prevent 1st number not show " + " symbol
            {
                    sign = 1;
                    cout << "Sum of : ";
            }
            else if (i< 1 || i> limit){
                    return ;
            }
            else {
                    sign = (i % 4 == 0) ? -1 : 1;
                    if (sign > 0){
                      cout << " + ";
                    }
                    else {
                      cout << " - ";
                    }
            }


            p = 1;
            q = ( 2 * ( i - 1 ) ) + 2;
            cout << p << "/" << q;
            x = ( 1.0 * p / q );
            S = S + x;

            recurse(i+1, limit);
    }
静态无效递归(int i,int limit){
int符号=0,p,q,n;
双x,S;
S=0;
如果(i==1)//防止第一个数字不显示“+”符号
{
符号=1;
cout(极限){
返回;
}
否则{
符号=(i%4==0)?-1:1;
如果(符号>0){

如果你有功能代码,也许这更适合?你可以使数字始终为正,然后独立输出它的“符号”。然后“-”将像“+”一样工作谢谢你@EdChum给你的建议,对不起,我仍然不知道在哪里问我的程序。
cout << p << "/" << q; 
if(p < 0) {
    cout << " - " << p*-1 << "/" << q;
} else {
    cout << p << "/" << q;
}
    static void recurse(int i, int limit){
            int sign = 0, p, q, n;
            double x, S;
            S =0;
            if (i == 1) // to prevent 1st number not show " + " symbol
            {
                    sign = 1;
                    cout << "Sum of : ";
            }
            else if (i< 1 || i> limit){
                    return ;
            }
            else {
                    sign = (i % 4 == 0) ? -1 : 1;
                    if (sign > 0){
                      cout << " + ";
                    }
                    else {
                      cout << " - ";
                    }
            }


            p = 1;
            q = ( 2 * ( i - 1 ) ) + 2;
            cout << p << "/" << q;
            x = ( 1.0 * p / q );
            S = S + x;

            recurse(i+1, limit);
    }
int main ()
{
  recurse(1, 6);
  cout << "\n";
}