結果

問題 No.782 マイナス進数
ユーザー leaf_1415leaf_1415
提出日時 2019-01-11 22:24:35
言語 C++11
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 670 bytes
コンパイル時間 602 ms
コンパイル使用メモリ 59,476 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-30 08:43:35
合計ジャッジ時間 3,085 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 WA * 1
other AC * 18 WA * 9 RE * 9
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:51:35: warning: ‘p’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   51 |                 cout << ans.substr(p) << endl;
      |                         ~~~~~~~~~~^~~

ソースコード

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;
		
		if(n == 0){
			cout << 0 << endl;
			continue;
		}
		
		string ans;
		while(n != 0){
			int q, r;
			mdiv(n, -B, q, r);
			ans += r + '0';
			n = q;
		}
		reverse(ans.begin(), ans.end());
		
		int p;
		for(int i = 0; i < ans.size(); i++){
			if(ans[i] == '1'){
				p = i;
				break;
			}
		}
		cout << ans.substr(p) << endl;
	}
	return 0;
}
0