按照给定顺序重写数组。

huangapple go评论63阅读模式
英文:

Rewriting array in given order

问题

编写一个void shuffle函数(int* we, int count, int* wy),它会按照指定的顺序将we数组的元素(其中count参数指定了we数组的大小)重写到wy数组中。

我尝试使用for循环将数组分成i < 5i > 5的部分,但一直遇到问题。目前唯一有效的方法是重写元素[0]。有什么帮助吗?

#include <iostream>

using namespace std;

void zadanie1(void)
{
    int count = 11;
    int* we = new int[count];
    int* wy = new int[count];

    cout << "开始:" << endl;

    for (int i = 0; i < count; i++) {
        we[i] = rand() % 10;
        cout << we[i] << " ";
    }
    cout << endl;

    cout << "结束:" << endl;

    for (int i = 0; i < count; i++) {
        if (i == 0) {
            wy[i] = we[i];
            cout << wy[i] << " ";
            i++;
        };
    }
}

这是您提供的代码和描述的翻译。如果需要进一步的帮助,请随时告诉我。

英文:

I need to rewrite an array in given order below:

> Write a void shuffle function (int* we, int count, int* wy) that rewrites the the elements of the we array (where the count parameter specifies the size of the we array) to the array wy according.
>

I tried with for loop to divide the array for i < 5 and i > 5 but all the time got some problems. The only one which work for now is rewriting element[0]. Any help?

#include &lt;iostream&gt;

using namespace std; 

void zadanie1(void)
{

	int count = 11;
	int* we = new int[count];
	int* wy = new int[count];


	cout &lt;&lt; &quot;Begin: &quot; &lt;&lt; endl;

	for (int i = 0; i &lt; count; i++) {
		we[i] = rand() % 10;
		cout &lt;&lt; we[i] &lt;&lt; &quot; &quot;;
	}
	cout &lt;&lt; endl;

	cout &lt;&lt; &quot;End: &quot; &lt;&lt; endl;

	for (int i = 0; i &lt; count; i++) {
		if (i == 0) {
			wy[i] = we[i];
			cout &lt;&lt; wy[i] &lt;&lt; &quot; &quot;;
			i++;
		};
		
		
	};
}

答案1

得分: 0

从图像中,您可以看到对于i=0...5,您将元素映射为i->2*i,对于i=6..10,您将元素映射为i->2*i-11。因此,所需的循环如下:

for (int i = 0; i <= 5; i++)
  wy[2 * i] = we[i];

for (int i = 6; i <= 10; i++)
  wy[2 * i - 11] = we[i];

对于一般的大小count,循环如下:

for (int i = 0; i <= (count / 2); i++)
  wy[2 * i] = we[i];

for (int i = (count / 2) + 1; i < count; i++)
  wy[2 * i - count] = we[i];
英文:

From the image, you see that you map the element i-&gt;2*i for i=0...5, and i-&gt;2*i-11 for i=6..10. Thus the required loop is

for (int i = 0; i &lt;= 5; i++)
  wy[2 * i] = we[i];

for (int i = 6; i &lt; 11; i++)
  wy[2 * i - 11] = we[i];

For a general size count the loop looks like

for (int i = 0; i &lt;= (count / 2); i++)
  wy[2 * i] = we[i];

for (int i = (count / 2) + 1; i &lt; count; i++)
  wy[2 * i - count] = we[i];

答案2

得分: 0

以下是代码部分的中文翻译:

对于开始者,函数应该以以下方式声明:

    void shuffle( const int *src, size_t n, int *dsn );

这里第一个参数应该使用`const`修饰符进行声明,因为在函数内部不会更改源数组,而第二个参数应该使用无符号整数类型`size_t`来代替有符号整数类型`int`。

在函数内部,只需要编写一个for循环

这是一个演示程序。在第一个复合语句中,使用了具有偶数个元素的数组,在第二个复合语句中,使用了具有奇数个元素的数组。

```cpp
#include &lt;iostream&gt;

void shuffle( const int *src, size_t n, int *dsn )
{
    for ( size_t i = 0, middle = ( n + 1 ) / 2; i &lt; n; i++ )
    {
        if (i &lt; middle)
        {
            dsn[2 * i] = src[i];
        }
        else
        {
            dsn[2 * ( i - middle ) + 1] = src[i];
        }
    }
}

int main()
{
    {
        const size_t N = 10;
        int a[N] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        int b[N];

        for (const auto &amp;item : a)
        {
            std::cout &lt;&lt; item &lt;&lt; &#39; &#39;;
        }
        std::cout &lt;&lt; &#39;\n&#39;;

        shuffle( a, N, b );

        for (const auto &amp;item : b)
        {
            std::cout &lt;&lt; item &lt;&lt; &#39; &#39;;
        }
        std::cout &lt;&lt; &#39;\n&#39;;
    }

    std::cout &lt;&lt; &#39;\n&#39;;

    {
        const size_t N = 11;
        int a[N] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 , 11 };
        int b[N];

        for (const auto &amp;item : a)
        {
            std::cout &lt;&lt; item &lt;&lt; &#39; &#39;;
        }
        std::cout &lt;&lt; &#39;\n&#39;;

        shuffle( a, N, b );

        for (const auto &amp;item : b)
        {
            std::cout &lt;&lt; item &lt;&lt; &#39; &#39;;
        }
        std::cout &lt;&lt; &#39;\n&#39;;
    }
}

程序的输出结果为:

1 2 3 4 5 6 7 8 9 10
1 6 2 7 3 8 4 9 5 10

1 2 3 4 5 6 7 8 9 10 11
1 7 2 8 3 9 4 10 5 11 6
英文:

For starters the function should be declared the following way

void shuffle( const int *src, size_t n, int *dsn );

That is the first parameter should be declared with the qualifier const because the source array is not changed within the function and the second parameter should have the unsigned integer type size_t instead of the signed integer type int.

Within the function it is enough to write one for loop.

Here is a demonstration program. In the first compound statement there are used arrays with an even number of elements and in the second compound statement there are used arrays with an odd number of elements.

#include &lt;iostream&gt;
void shuffle( const int *src, size_t n, int *dsn )
{
for ( size_t i = 0, middle = ( n + 1 ) / 2; i &lt; n; i++ )
{
if (i &lt; middle)
{
dsn[2 * i] = src[i];
}
else
{
dsn[2 * ( i - middle ) + 1] = src[i];
}
}
}
int main()
{
{
const size_t N = 10;
int a[N] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int b[N];
for (const auto &amp;item : a)
{
std::cout &lt;&lt; item &lt;&lt; &#39; &#39;;
}
std::cout &lt;&lt; &#39;\n&#39;;
shuffle( a, N, b );
for (const auto &amp;item : b)
{
std::cout &lt;&lt; item &lt;&lt; &#39; &#39;;
}
std::cout &lt;&lt; &#39;\n&#39;;
}
std::cout &lt;&lt; &#39;\n&#39;;
{
const size_t N = 11;
int a[N] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 , 11 };
int b[N];
for (const auto &amp;item : a)
{
std::cout &lt;&lt; item &lt;&lt; &#39; &#39;;
}
std::cout &lt;&lt; &#39;\n&#39;;
shuffle( a, N, b );
for (const auto &amp;item : b)
{
std::cout &lt;&lt; item &lt;&lt; &#39; &#39;;
}
std::cout &lt;&lt; &#39;\n&#39;;
}
}

The program output is

1 2 3 4 5 6 7 8 9 10
1 6 2 7 3 8 4 9 5 10
1 2 3 4 5 6 7 8 9 10 11
1 7 2 8 3 9 4 10 5 11 6

huangapple
  • 本文由 发表于 2023年2月24日 01:25:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548263.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定