結果

問題 No.782 マイナス進数
ユーザー htensai
提出日時 2019-12-25 13:56:35
言語 Java
(openjdk 23)
結果
AC  
実行時間 337 ms / 2,000 ms
コード長 1,127 bytes
コンパイル時間 2,869 ms
コンパイル使用メモリ 75,080 KB
実行使用メモリ 59,176 KB
最終ジャッジ日時 2024-09-25 00:14:56
合計ジャッジ時間 13,597 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        int b = - sc.nextInt();
        StringBuilder ans = new StringBuilder();
        for (int i = 0; i < t; i++) {
            boolean isPlus = true;
            int x = sc.nextInt();
            StringBuilder sb = new StringBuilder();
            if (x == 0) {
                ans.append(0).append("\n");
                continue;
            }
           while (x > 0) {
                if (isPlus) {
                    sb.append(x % b);
                    x /= b;
                    isPlus = false;
                } else {
                    if (x % b == 0) {
                        sb.append(0);
                        x /= b;
                    } else {
                        sb.append(b - x % b);
                        x /= b;
                        x++;
                    }
                    isPlus = true;
                }
            }
            ans.append(sb.reverse()).append("\n");
        }
        System.out.print(ans);
    }
}
0