結果

問題 No.39 桁の数字を入れ替え
ユーザー er-k-akier-k-aki
提出日時 2020-07-15 11:56:22
言語 Go
(1.22.1)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 1,547 bytes
コンパイル時間 10,603 ms
コンパイル使用メモリ 232,052 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-01 10:15:58
合計ジャッジ時間 11,296 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var sc = bufio.NewScanner(os.Stdin)

func nextLine() string {
	sc.Scan()
	return sc.Text()
}
func nextInt() int {
	a, _ := strconv.Atoi(nextLine())
	return a
}

func main() {
	a := nextLine()
	intArray := toIntArray(a, "")
	originalSize := ArrayToInt(intArray)
	max := originalSize
	//1回だけスワップをloop
	len := len(intArray)
	for i := 0; i < len; i++ {
		for j := i + 1; j < len; j++ {
			copied := arrayCopy(intArray)
			copied[i], copied[j] = intArray[j], intArray[i]
			copiedSize := ArrayToInt(copied)
			// スワップ済みが元より大きいとき、最大をスワップ済みで置換
			if copiedSize > max {
				max = copiedSize
			}
		}
	}
	fmt.Println(max)
}

/**
 * 文字列を分割して数値要素の配列に変換して返す
 */
func toIntArray(original string, delimiter string) (intArray []int) {
	splited := strings.Split(original, delimiter)
	len := len(splited)
	intArray = make([]int, len)
	for i := 0; i < len; i++ {
		intArray[i], _ = strconv.Atoi(splited[i])
	}
	return
}

/**
 * 数値配列を桁数に合わせた数値に変換して返す
 */
func ArrayToInt(original []int) (result int) {
	base := 1
	len := len(original)
	//末尾から足しては10倍していく
	for i := len - 1; i >= 0; i-- {
		result += base * original[i]
		base *= 10
	}
	return
}

/**
* 数値配列のコピーを返す
 */
func arrayCopy(original []int) (result []int) {
	result = make([]int, len(original))
	_ = copy(result, original)
	return
}
0