英文:
Rewriting array in given order
问题
编写一个
void shuffle
函数(int* we, int count, int* wy),它会按照指定的顺序将we
数组的元素(其中count
参数指定了we
数组的大小)重写到wy
数组中。
我尝试使用
for
循环将数组分成i < 5
和i > 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 <iostream>
using namespace std;
void zadanie1(void)
{
int count = 11;
int* we = new int[count];
int* wy = new int[count];
cout << "Begin: " << endl;
for (int i = 0; i < count; i++) {
we[i] = rand() % 10;
cout << we[i] << " ";
}
cout << endl;
cout << "End: " << endl;
for (int i = 0; i < count; i++) {
if (i == 0) {
wy[i] = we[i];
cout << wy[i] << " ";
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->2*i
for i=0...5
, and i->2*i-11
for i=6..10
. Thus the required loop is
for (int i = 0; i <= 5; i++)
wy[2 * i] = we[i];
for (int i = 6; i < 11; i++)
wy[2 * i - 11] = we[i];
For a general size count
the loop looks like
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];
答案2
得分: 0
以下是代码部分的中文翻译:
对于开始者,函数应该以以下方式声明:
void shuffle( const int *src, size_t n, int *dsn );
这里第一个参数应该使用`const`修饰符进行声明,因为在函数内部不会更改源数组,而第二个参数应该使用无符号整数类型`size_t`来代替有符号整数类型`int`。
在函数内部,只需要编写一个for循环。
这是一个演示程序。在第一个复合语句中,使用了具有偶数个元素的数组,在第二个复合语句中,使用了具有奇数个元素的数组。
```cpp
#include <iostream>
void shuffle( const int *src, size_t n, int *dsn )
{
for ( size_t i = 0, middle = ( n + 1 ) / 2; i < n; i++ )
{
if (i < 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 &item : a)
{
std::cout << item << ' ';
}
std::cout << '\n';
shuffle( a, N, b );
for (const auto &item : b)
{
std::cout << item << ' ';
}
std::cout << '\n';
}
std::cout << '\n';
{
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 &item : a)
{
std::cout << item << ' ';
}
std::cout << '\n';
shuffle( a, N, b );
for (const auto &item : b)
{
std::cout << item << ' ';
}
std::cout << '\n';
}
}
程序的输出结果为:
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 <iostream>
void shuffle( const int *src, size_t n, int *dsn )
{
for ( size_t i = 0, middle = ( n + 1 ) / 2; i < n; i++ )
{
if (i < 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 &item : a)
{
std::cout << item << ' ';
}
std::cout << '\n';
shuffle( a, N, b );
for (const auto &item : b)
{
std::cout << item << ' ';
}
std::cout << '\n';
}
std::cout << '\n';
{
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 &item : a)
{
std::cout << item << ' ';
}
std::cout << '\n';
shuffle( a, N, b );
for (const auto &item : b)
{
std::cout << item << ' ';
}
std::cout << '\n';
}
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论