Problem:头疼的Litmxs
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
Language:C++
#include <iostream>
using namespace std;
int main()
{
int q;
cin>>q;
while(q--)
{
int n,k;
cin>>n>>k;
int num=0;
int ans[30];
while(n)
{
ans[num]=n%2;
n/=2;
if(k==-2)
{
if( (num%2) && (ans[num]%2) ) n++;
}
num++;
}
if(num)
{
for(int i=num-1;i>=0;i--) cout<<ans[i];
cout<<endl;
}
else cout<<0<<endl;
}
return 0;
}