結果

問題 No.2426 Select Plus or Minus
ユーザー kakel-san
提出日時 2023-08-18 21:27:03
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,634 bytes
コンパイル時間 4,137 ms
コンパイル使用メモリ 112,048 KB
実行使用メモリ 18,048 KB
最終ジャッジ日時 2024-11-28 05:40:15
合計ジャッジ時間 5,227 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;
class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[] LList(long n) => Enumerable.Repeat(0, (int)n).Select(_ => int.Parse(ReadLine())).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var n = long.Parse(ReadLine());
        var ans = Select(n);
        WriteLine(ans.Length);
        WriteLine(ans);
    }
    static string Select(long n)
    {
        var ans = new List<char>();
        while (n > 1)
        {
            if (n % 2 == 0)
            {
                n >>= 1;
                ans.Add('/');
            }
            else
            {
                var a = 3 * n + 1;
                var b = 3 * n - 1;
                var acnt = 0;
                var tmp = a;
                while (tmp % 2 == 0)
                {
                    ++acnt;
                    tmp >>= 1;
                }
                var bcnt = 0;
                tmp = b;
                while (tmp % 2 == 0)
                {
                    ++bcnt;
                    tmp >>= 1;
                }
                if (acnt >= bcnt)
                {
                    n = a;
                    ans.Add('+');
                }
                else
                {
                    n = b;
                    ans.Add('-');
                }
            }
        }
        return string.Concat(ans);
    }
}
0