Java, 669 byte
tidak akan berbohong, saya cukup bangga pada diri saya untuk yang satu ini: hal
import java.lang.reflect.Array;enum S{D;<A>A s(A a){int l=Array.getLength(a),x=0;Class t=a.getClass();Class c=t.getComponentType();A r=(A)Array.newInstance(c,l+1);System.arraycopy(a,0,r,0,l);if(t==int[].class)for(;x<l;)((int[])r)[l]=((int[])r)[l]+((int[])r)[x++];else{for(;x<l;)Array.set(r,x,S.this.s(Array.get(r,x++)));Object o=Array.get(r,0);for(;--x>0;)o=s(o,Array.get(r,x));Array.set(r,l,o);}return r;}<A>A s(A a,A b){int l=Array.getLength(a),x=0;Class t=a.getClass();A r=(A)Array.newInstance(t.getComponentType(),l);if(int[].class==t)for(;x<l;)((int[])r)[x]=((int[])a)[x]+((int[])b)[x++];else for(;x<l;)Array.set(r,x,s(Array.get(a,x),Array.get(b,x++)));return r;}}
diperluas dengan pengujian:
import java.lang.reflect.Array;
import java.util.Arrays;
public enum SumOf{
Dimensions;
<A>A sum(A array){ //call this method to solve the challenge
int length=Array.getLength(array),x=0;
Class arrayType=array.getClass();
Class componentType=arrayType.getComponentType();
//grow the array to include the sum element
A result=(A)Array.newInstance(componentType,length+1);
System.arraycopy(array,0,result,0,length);
if(arrayType==int[].class) //one-dimensional array needs to be handled separately
for(;x<length;) //find the sum
((int[])result)[length]=((int[])result)[length]+((int[])result)[x++];
else{ //multi-dimensional array
for(;x<length;) //find the sum for each element in this dimension's array
Array.set(result,x,sum(Array.get(result,x++)));
//find the total sum for this dimension's array
Object s=Array.get(result,0);
for(;--x>0;)
s=_sum(s,Array.get(result,x)); //add the 2 elements together
Array.set(result,length,s);
}
return result;
}
<A>A _sum(A arrayA,A arrayB){ //this method is used by the previous method
int length=Array.getLength(arrayA),x=0;
Class arrayType=arrayA.getClass();
A result=(A)Array.newInstance(arrayType.getComponentType(),length);
if(int[].class==arrayType) //one-dimensional array needs to be handled separately
for(;x<length;) //find the sum of both arrays
((int[])result)[x]=((int[])arrayA)[x]+((int[])arrayB)[x++];
else
for(;x<length;) //find the sum of both arrays
Array.set(result,x,sum(Array.get(arrayA,x),Array.get(arrayB,x++)));
return result;
}
static int[] intArray( int firstElement, int...array ) {
if( array == null ) array = new int[0];
array = Arrays.copyOf( array, array.length + 1 );
System.arraycopy( array, 0, array, 1, array.length - 1 );
array[0] = firstElement;
return array;
}
static <E> E[] arrayArray( E firstElement, E...array ) {
if( array == null ) array = (E[]) Array.newInstance( firstElement.getClass(), 0 );
array = Arrays.copyOf( array, array.length + 1 );
System.arraycopy( array, 0, array, 1, array.length - 1 );
array[0] = firstElement;
return array;
}
static void printIntArray( int[]array ){
System.out.print("[ ");
for( int x = 0; x < array.length; x++ )
System.out.print( array[x] + " " );
System.out.print("] ");
}
static < A > void printArray( A array ) {
if( array.getClass() == int[].class ){
printIntArray( (int[]) array );
}
else {
System.out.print("[ ");
int length = Array.getLength( array );
for( int x = 0; x < length; x++ )
printArray( Array.get( array, x ) );
System.out.print("] ");
}
}
public static void main(String[]s){
int[] test01 = intArray( 5, 2, 3 );
System.out.print("Input: ");
printArray( test01 );
System.out.print("\nOutput: ");
printArray( SumOf.Dimensions.sum( test01 ) );
System.out.println();
int[][] test02 = arrayArray( intArray( 1, 2, 3 ), intArray( 4, 5, 6 ) );
System.out.print("\nInput: ");
printArray( test02 );
System.out.print("\nOutput: ");
printArray( SumOf.Dimensions.sum( test02 ) );
System.out.println();
int[][][] test03 = arrayArray( arrayArray( intArray( 1 ), intArray( 1 ), intArray( 1 ), intArray( 0 ) ) );
System.out.print("\nInput: ");
printArray( test03 );
System.out.print("\nOutput: ");
printArray( SumOf.Dimensions.sum( test03 ) );
System.out.println();
int[][][][] test04 = arrayArray( arrayArray( arrayArray( intArray( -1 ) ) ) );
System.out.print("\nInput: ");
printArray( test04 );
System.out.print("\nOutput: ");
printArray( SumOf.Dimensions.sum( test04 ) );
System.out.println();
int[][][] test05 = arrayArray( arrayArray( intArray( 1, 2, 3 ), intArray( 4, 5, 6 ), intArray( 7, 8, 9 ) ), arrayArray( intArray( 11, 12, 13 ), intArray( 14, 15, 16 ), intArray( 17, 18, 19 ) ), arrayArray( intArray( 21, 22, 23 ), intArray( 24, 25, 26 ), intArray( 27, 28, 29 ) ) );
System.out.print("\nInput: ");
printArray( test05 );
System.out.print("\nOutput: ");
printArray( SumOf.Dimensions.sum( test05 ) );
System.out.println();
}
}
menjalankan versi pengujian yang diperluas mencetak ini:
Input: [ 5 2 3 ]
Output: [ 5 2 3 10 ]
Input: [ [ 1 2 3 ] [ 4 5 6 ] ]
Output: [ [ 1 2 3 6 ] [ 4 5 6 15 ] [ 5 7 9 21 ] ]
Input: [ [ [ 1 ] [ 1 ] [ 1 ] [ 0 ] ] ]
Output: [ [ [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 0 0 ] [ 3 3 ] ] [ [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 0 0 ] [ 3 3 ] ] ]
Input: [ [ [ [ -1 ] ] ] ]
Output: [ [ [ [ -1 -1 ] [ -1 -1 ] ] [ [ -1 -1 ] [ -1 -1 ] ] ] [ [ [ -1 -1 ] [ -1 -1 ] ] [ [ -1 -1 ] [ -1 -1 ] ] ] ]
Input: [ [ [ 1 2 3 ] [ 4 5 6 ] [ 7 8 9 ] ] [ [ 11 12 13 ] [ 14 15 16 ] [ 17 18 19 ] ] [ [ 21 22 23 ] [ 24 25 26 ] [ 27 28 29 ] ] ]
Output: [ [ [ 1 2 3 6 ] [ 4 5 6 15 ] [ 7 8 9 24 ] [ 12 15 18 45 ] ] [ [ 11 12 13 36 ] [ 14 15 16 45 ] [ 17 18 19 54 ] [ 42 45 48 135 ] ] [ [ 21 22 23 66 ] [ 24 25 26 75 ] [ 27 28 29 84 ] [ 72 75 78 225 ] ] [ [ 33 36 39 108 ] [ 42 45 48 135 ] [ 51 54 57 162 ] [ 126 135 144 405 ] ] ]