anonymous No title
No License PHP
2019年07月13日
Copy
function make_ary($x){
	if($x==1){
		return [2];	
	}
	$cur_list = make_ary($x-1);
	$x_last = end($cur_list);
	$add_list = array_map(function($x) use($x_last){
		return $x + $x_last;
	},$cur_list);
	$result = array_pad([0],count($cur_list)*2,0);
	
	$j = 0;
	foreach($result as $i => $int){
		if($i%2==0){
			$result[$i] = $cur_list[$j];
		}else{
			$result[$i] = $add_list[$j];
			$j++;
		}
	}
	return $result;
}

print_r( make_ary(5) );
// Array ( [0] => 2 [1] => 18 [2] => 10 [3] => 26 [4] => 6 [5] => 22 [6] => 14 [7] => 30 [8] => 4 [9] => 20 [10] => 12 [11] => 28 [12] => 8 [13] => 24 [14] => 16 [15] => 32 )
function make_ary($x){
	if($x==1){
		return [2];	
	}
	$cur_list = make_ary($x-1);
	$x_last = end($cur_list);
	$add_list = array_map(function($x) use($x_last){
		return $x + $x_last;
	},$cur_list);
	$result = array_pad([0],count($cur_list)*2,0);
	
	$j = 0;
	foreach($result as $i => $int){
		if($i%2==0){
			$result[$i] = $cur_list[$j];
		}else{
			$result[$i] = $add_list[$j];
			$j++;
		}
	}
	return $result;
}

print_r( make_ary(5) );
// Array ( [0] => 2 [1] => 18 [2] => 10 [3] => 26 [4] => 6 [5] => 22 [6] => 14 [7] => 30 [8] => 4 [9] => 20 [10] => 12 [11] => 28 [12] => 8 [13] => 24 [14] => 16 [15] => 32 )

Output

Run
年末年始は機械学習・深層学習を勉強しませんか?
No one still commented. Please first comment.
年末年始は機械学習・深層学習を勉強しませんか?
広告
未経験から最短でエンジニアへの転職を目指すなら