作者在 2013-10-28 16:39:59 发布以下内容
Problem Description
Input
输入数据的第一行为一个正整数T, 表示测试数据的组数. 然后是T组测试数据. 每组测试数据包括整数n和k。
Output
对于每个n,先输出n, 然后输出一个空格,最后输出对应的k进制数。
Sample Input
5 5 3 123 8 0 5 -12 2 1 2Sample Output
5 12 123 173 0 0 -12 -1100 1 1
#include<iostream>
using namespace std;
int main()
{
int t;cin>>t;
for(int i=0;i<t;i++)
{
int n,k,cnt=0,a[100];cin>>n>>k;int m=n;//把n的值放在m中,因为后来会对n进行多次处理,最后输出的是n=0,这与例子不符
if(n==0) cout<<n<<" "<<"0";
else
{
bool flag=false;
if(n<0)
{
flag=true;
n=-n;
}
while(n>0)
{
a[cnt++]=n%k;n/=k;
}
if(flag==true)cout<<m<<" "<<"-";
else cout<<m<<" ";
for(int j=cnt-1;j>=0;j--)
{
cout<<a[j];
}
}
cout<<endl;
}
return 0;
}
本题是根据除k取余法来结局问题的