各位高人帮个忙

bill614

新手上路
注册
2004-10-02
消息
53
荣誉分数
0
声望点数
0
有个东西不会做 我用的是c# 请各位高人谁指点一下
make these three arrays:
int [] odd={5,1,17,3,11,15,31};
int [] even={16,4,18,36,26,10,12};


int [] arr= new int [14];
use a loop to alternately copy from the first two arrays, odd and even, into the third one,arr.
then reverse arr, don't simply print in reverse, but actually physically reverse it without copying the data,in reverse, into another array. don't make a 4th array.
就这么多 跪求此题答案 请各位会c语言的大哥帮忙看一下 小弟感激不尽!
 
//copy:
for (int i = 0; i < 7; i++)
{
arr[2i] = odd;
arr[2i+1] = even;
}

//reverse:
int j = 0;
for (int i = 6; i >= 0; i--, j++)
{
arr[2j] = even;
arr[2j+1] = odd;
}

不一定对, 你编译试一哈。
 
// another way to reverse it:
// it could be better to satisfy your teacher

int temp;
for (int i = 0; i < 7; i++)
{
temp = arr;
arr = arr[13-i];
arr[13-i] = temp;
}
 
It works like this actually:

static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
//make these three arrays:
int [] odd={5,1,17,3,11,15,31};
int [] even={16,4,18,36,26,10,12};

int [] arr= new int [14];
//copy:
for (int i = 0; i < 7; i++)
{
arr[2*i] = odd;
arr[2*i+1] = even;
}

for (int i = 0; i < 14; i++)
{
Console.Write(arr.ToString() + "\t");
}
Console.Write("\n\n");

//reverse:
int temp;
for (int i = 0; i < 7; i++)
{
temp = arr;
arr = arr[13-i];
arr[13-i] = temp;
}
for (int i = 0; i < 14; i++)
{
Console.Write(arr.ToString() + "\t");
}
Console.Write("\n\n");
}
 
后退
顶部