Description
今天队友扔了个题给 Litmxs,小 Litmxs 很吃惊,因为这题似曾相识,却又无从下手,他又来求助于你们了!队友给了 Litmxs 一个整数 n(10进制),要求 Litmxs 将 n 转化为 k 进制。如果能转化为 k 进制,上天将会让 Litmxs 偶遇他的女朋友。你能帮帮他吗?
Input
输入一个q代表q次询问, 每组数据两个数n,k,保证答案存在
(q <=110,0<=n<=1e9, k==-2 || k==2)
Output
输出对应的k进制,不要有多余的换行和空格
Sample Input
2
5 2
5 -2
Sample Output
101
101
除负数时,要特殊考虑
#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
int main(){
int t;
scanf("%d", &t);
while(t--){
int n, k;
string ans="";
scanf("%d %d", &n, &k);
if(n == 0){
printf("0\n");
continue;
}
while(n){
int t = n%k;
n /= k;
if(t < 0) t += 2,n++;
ans += '0'+t;
}
reverse(ans.begin(),ans.end());
cout<<ans<<endl;
}
return 0;
}