結果

問題 No.782 マイナス進数
ユーザー leaf_1415
提出日時 2019-01-11 22:22:15
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 503 bytes
コンパイル時間 540 ms
コンパイル使用メモリ 58,452 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-30 08:35:06
合計ジャッジ時間 2,297 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 27 WA * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int T, B;

void mdiv(int n, int b, int &q, int &r)
{
	if(n > 0){
		q = n/b;
		r = n%b;
		q *= -1;
	}
	else{
		int m = (-n+b-1)/b*b;
		q = m/b;
		r = m+n;
	}
}

int main(void)
{
	cin >> T >> B;
	for(int t = 0; t < T; t++){
		int n;
		cin >> n;
		
		string ans;
		while(n != 0){
			int q, r;
			mdiv(n, -B, q, r);
			ans += r + '0';
			n = q;
		}
		reverse(ans.begin(), ans.end());
		
		cout << ans << endl;
	}
	return 0;
}
0