前些日子一个同事让我跟他做个东西,主要实现是这样,他有一个数组,数组里面的都是整形,数组里面的个数都是四的整数倍,就是可以被四整除,他想把他们四个拿出来做一组,分别拿出来,然后计算分组后的第一位的和,第二位的和,第三位的和和第四位的和.下面给出原形:
long[] tuirate ={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 530, 5, 5, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
1-4为一组,5-8为一组,依次类推.拿到问题我想肯定要用个二维来解决,不过是我自己想的,并非标准答案.下面是具体实现
也许我接触二维不多,所以放着做个记号,以后方便查阅.
long[] tuirate ={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 530, 5, 5, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
1-4为一组,5-8为一组,依次类推.拿到问题我想肯定要用个二维来解决,不过是我自己想的,并非标准答案.下面是具体实现
long[] tuirate ={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 530, 5, 5, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
long[,] tempA = new long[tuirate.Length/ 4, 4];
long[] tempB = new long[4];
for (int i = 0; i < tuirate.Length/4; i++)
{
for (int j = 0; j < 4; j++)
{
tempA[i,j] = tuirate[i * 4 + j];
}
}
long result=0;
int m = 0;
for (int k = 0; k < 4; k++)
{
for (int l = 0; l < tuirate.Length / 4; l++)
{
result+=tempA[l,k];
}
tempB[m++] = result;
}
long[,] tempA = new long[tuirate.Length/ 4, 4];
long[] tempB = new long[4];
for (int i = 0; i < tuirate.Length/4; i++)
{
for (int j = 0; j < 4; j++)
{
tempA[i,j] = tuirate[i * 4 + j];
}
}
long result=0;
int m = 0;
for (int k = 0; k < 4; k++)
{
for (int l = 0; l < tuirate.Length / 4; l++)
{
result+=tempA[l,k];
}
tempB[m++] = result;
}
也许我接触二维不多,所以放着做个记号,以后方便查阅.