結果

問題 No.123 カードシャッフル
ユーザー gogoteagogotea
提出日時 2015-04-27 19:44:49
言語 Go1.4
(1.4.2)
結果
AC  
実行時間 29 ms / 5,000 ms
コード長 1,130 bytes
コンパイル時間 858 ms
コンパイル使用メモリ 33,164 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-16 11:48:50
合計ジャッジ時間 1,586 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

package main

import (
	"bufio"
	"container/list"
	"fmt"
	"io"
	"os"
	"strconv"
)

func main() {
	sc := NewScanner(os.Stdin)
	N, _ := sc.NextInt()
	M, _ := sc.NextInt()

	l := list.New()
	for i := 1; i <= N; i++ {
		l.PushBack(i)
	}

	for i := 1; i <= M; i++ {
		A, _ := sc.NextInt()
		e := getElement(l, A)
		l.MoveToFront(e)
	}
	fmt.Println(l.Front().Value)
}

func getElement(l *list.List, n int) (elem *list.Element) {
	if n <= 0 || n > l.Len() {
		return nil
	}
	var i int = 1
	for elem = l.Front(); elem != nil; elem = elem.Next() {
		if i == n {
			break
		}
		i++
	}
	return elem
}

type Scanner struct {
	*bufio.Scanner
}

func NewScanner(r io.Reader) *Scanner {
	return &Scanner{
		bufio.NewScanner(r),
	}
}

func (s *Scanner) Next() (string, error) {
	s.Scanner.Split(bufio.ScanWords)
	return s.nextToken()
}

func (s *Scanner) nextToken() (string, error) {
	sc := s.Scanner
	if sc.Scan() {
		return sc.Text(), nil
	}
	if sc.Err() != nil {
		return "", sc.Err()
	}
	return "", io.EOF
}

func (s *Scanner) NextInt() (int, error) {
	token, err := s.Next()
	if err != nil {
		return 0, err
	}
	return strconv.Atoi(token)
}
0