結果

問題 No.39 桁の数字を入れ替え
ユーザー 綾地寧々綾地寧々
提出日時 2015-07-25 13:41:27
言語 PHP
(8.2.11)
結果
WA  
実行時間 -
コード長 1,272 bytes
コンパイル時間 51 ms
コンパイル使用メモリ 18,852 KB
実行使用メモリ 18,960 KB
最終ジャッジ日時 2023-09-23 04:53:38
合計ジャッジ時間 1,494 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
18,788 KB
testcase_01 AC 15 ms
18,852 KB
testcase_02 AC 16 ms
18,704 KB
testcase_03 AC 15 ms
18,816 KB
testcase_04 AC 16 ms
18,824 KB
testcase_05 AC 15 ms
18,852 KB
testcase_06 AC 15 ms
18,756 KB
testcase_07 AC 15 ms
18,624 KB
testcase_08 AC 15 ms
18,732 KB
testcase_09 AC 16 ms
18,732 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 16 ms
18,736 KB
testcase_15 AC 15 ms
18,780 KB
testcase_16 AC 16 ms
18,756 KB
testcase_17 AC 15 ms
18,660 KB
testcase_18 AC 15 ms
18,728 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

<?php
$n = trim(fgets(STDIN));
$n_array = array();
// 最大値を探す(ついでに1桁ずつ分解する)
$max = FALSE;
for ( $i=0; $i<strlen($n); $i++ ) {
	$n_array[] = intval(substr($n,$i,1));
	$max = my_max($n_array[$i], $max);
}
// 最大値がどの桁にいるか探す
$max_index = array();
$min_index = FALSE;
foreach ( $n_array as $index => $value ) {
	if ( $value == $max ) {
		$max_index[] = $index;
	}
	else if ( $min_index === FALSE ) {
		$min_index = $index;
	}
}
// どこと入れ替えるか考える。
// 最大値ではない数が出てこない
if ( $min_index === FALSE ) {
	;// 何もしない
}
// 最大値が出てくる一番低い桁より、最大値ではない値が出てくる一番高い桁のほうが高い場合は、入れ替える。
else if ( $max_index[count($max_index)-1] > $min_index ) {
	swap($n_array[$min_index], $n_array[$max_index[count($max_index)-1]]);

}
echo implode("", $n_array).PHP_EOL;

function my_max ($a, $b) {
	// どっちも FALSE, または整数で同じ値
	if ( $a === $b ) {
		return $a;
	}
	// a だけが FALSE
	if ( $a === FALSE ) {
		return $b;
	}
	// b だけが FALSE
	if ( $b === FALSE ) {
		return $a;
	}
	return max($a, $b);
}

function swap (&$a, &$b) {
	$t = $a;
	$a = $b;
	$b = $t;
}
0