我想要以以下形式打印数组A:F(A)= F(odd)+ F(even)如果n> 1////"+"表示合并F(A)= A如果n == 1 [英] I want print array A with this form: F(A)=F(odd)+F(even) if n>1 // “+” means merge F(A)=A if n==1

查看:121
本文介绍了我想要以以下形式打印数组A:F(A)= F(odd)+ F(even)如果n> 1////"+"表示合并F(A)= A如果n == 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使用以下形式打印数组A:
F(A)= F(奇数)+ F(偶数),如果n> 1////"+"表示合并
如果n == 1
,则F(A)= A 例如:
F([1,2,3,4])= F([1,3])+ F([2,4])
F([1,3])= F([1])+ F([3])= [1] + [3] = [1,3]
F([2,4])= F([2])+ F([4])= [2] + [4] = [2,4]
F([1,2,3,4])= F([1,3])+ F([2,4])= [1,3] + [2,4] = [1,3,2, 4]
因此,我编写了一个递归函数.但是我的代码对于最多4个元素是正确的.
但是如果输入5元素是错误的.
例如:
F([1,2,3,4,5] = [1,5,5,2,4]的结果,此错误.结果应为[1,5,3,2,4]
我的代码是:

I want print array A with this form:
F(A)=F(odd)+F(even) if n>1 // "+" means merge
F(A)=A if n==1
For example:
F([1,2,3,4])=F([1,3])+F([2,4])
F([1,3])=F([1])+F([3])=[1]+[3]=[1,3]
F([2,4])=F([2])+F([4])=[2]+[4]=[2,4]
F([1,2,3,4])=F([1,3])+F([2,4])=[1,3]+[2,4]=[1,3,2,4]
Thus I write a recursive function. But my code is correct for max 4 element.
But if enter 5 element is wrong.
For example:
Result of F([1,2,3,4,5]=[1,5,5,2,4] and this wrong . result should be [1,5,3,2,4]
My code is:

void EvenOdd::F(int *a,int n)  // F is in EvenOdd class
  {
	if (n==1)
	{
		cout <<a[n] << " ";
	       return   ;
	}
	else
	{
		if(n%2==0 )
		{
                        CreatOdd(a,odd,n);
			 F(odd,n/2);
			CreatEven(a,even,n);
			 F(even,n/2);
			
		}
		else
		{
			CreatEven(a,even,n);
			 F(even,(n/2)+1);
			CreatOdd(a,odd,n);
			 F(odd,n/2);
		}
	}
}
//*************************************************************************
 void EvenOdd::CreatOdd( int *Aptr,int *oddptr,int n)
{
	int j=1;
	for (int i=1 ;i<=n ; )
	{
		oddptr[j]=Aptr[i];
		j++;
		i=i+2;
	}
}
//***************************************************************************
  void EvenOdd::CreatEven(int *Aptr,int *evenptr,int n)
{
    int j=1;
	for (int i=2 ;i<=n ; )
	{
		evenptr[j]=Aptr[i];
		j++;
		i=i+2;
	}
}


谢谢


[edit]添加了代码块[/edit]


thanks


[edit]code block added[/edit]

推荐答案

您在这种情况下(n%2!= 0)的错误是因为您反转CreateOdd和CreateEven函数.

我认为您的代码应为:
Your mistake in the case (n%2!=0) is because you invert the CreateOdd and CreateEven function.

I think your code should be :
if(n%2==0 )
{
                CreatOdd(a,odd,n);
     F(odd,n/2);
    CreatEven(a,even,n);
     F(even,n/2);

}
else
{
    CreatOdd(a,odd,n);
     F(odd,(n/2)+1);
    CreatEven(a,even,n);
     F(even,(n/2));
}


因为当您有奇数个元素时,您将在奇数位置再有1个元素.
F [1,2,3,4,5] = F [1,3,5] + F [2,4]:奇数个3个元素,偶数个2个元素

您可以使用:
简化代码


because when you have an odd number of elements, you will have 1 more element at an odd position.
F[1,2,3,4,5] = F[1,3,5] + F[2,4] : 3 elements in odd and only 2 in even

You can simplify your code with :

CreateOdd(a,odd,n);
F(odd, (n/2)+n%2);
CreateEven(a,even,n);
F(even,(n/2));



第二个错误必须是在递归函数中使用全局"变量(偶数和奇数数组).我在您的代码示例中看不到偶数和奇数的任何声明.

我的最后一句话,将是在C ++中,数组通常从索引0开始:



The second mistake must be to use "global" variables (even and odd array) in a recursive function. I do not see in your code sample any declarations for even and odd.

And my last remark, will be in C++ the arrays usually start at index 0:

int a[1];
a[0]=1; //first element in a
a[1]=2; //element out of the array !


尝试一下:
Try this:
#include <iostream>
#include <vector>
using namespace std;

void oddeven(vector <int> & inv, vector <int> & outv)
{
    if (inv.size() > 1)
    {
        vector <int> odd;
        vector <int> even;
        auto it = inv.begin();
        while (it != inv.end())
        {
            odd.push_back(*it);
            it++;
            if ( it == inv.end()) break;
            even.push_back(*it);
            it++;
        }
        oddeven(odd, outv);
        oddeven(even, outv);
    }
    else
        outv.push_back(inv[0]);
}


void dump_vector(vector <int> v)
{
    cout << "{ ";
    for (auto a : v)
    {
        cout << a << " ";
    }
    cout << "}" << endl;
}

int main()
{
    vector <int> v,w;
    v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);v.push_back(5);
    dump_vector(v);
    oddeven(v,w);
    dump_vector(w);
}


这篇关于我想要以以下形式打印数组A:F(A)= F(odd)+ F(even)如果n&gt; 1////"+"表示合并F(A)= A如果n == 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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