1147. 进制转换

南昌大学陈睿思小朋友

注意32位啊,

hyf347347

比较简短的代码 命名有点不规范!

include

using namespace std;
typedef long long ll;

int main()
{
int t;
cin >> t;
while (t–){
ll m;
int n;
cin >> m >> n;
if (m < 0){
cout << “-“;
m = -m;
}
int a[100];
int k = 0;
while (m){
a[k] = m%n;
m = m / n;
k++;
}
for (int i = k-1; i >= 0; i–){
if (a[i] >= 0 && a[i] <= 9) cout << a[i];
else {
char r = a[i] - 10 + ‘A’;
cout << r;
}
}
cout << endl;
}
return 0;
}

Suzuki_Yuuta
char out[36] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

void solve()
{
    long long n, base; cin >> n >> base; 
    string output = ""; string sign = "";
    if (n < 0) {
        sign = "-"; n = -n;
    }
    while (n > 0) {
        output = out[n % base] + output;
        n /= base;
    }
    output = sign + output;
    cout << (output.length() ? output : "0") << endl;
}
YinkuPuddingcess

注意处理符号

fangpin

我该肿么办?!
32位

起飞飞飞飞

include

include

include

include

define N 50

char array[N]=”\0”;

int Ten_LessThanTen(int after, int num) //10转换10以内,参数after为转换后的进制,num为10进制数值

{ if(num<0){num=-num;
char array[N]=”\0”;
char result[N]=”\0”;

int i = 0,j = 0;

for(i = 0; num != 0; i++)       //利用for循环实现连续取余

{

    result[i] = (num % after)+'0';

    num /= after;
}
    for(j = 0; i > 0; i--, j++)  //倒置

{

    array[j] = result[i - 1];

    array[j + 1] = '\0';

}printf("-%s\n",array);}
else{
char array[N]="\0";
char result[N]="\0";

int i = 0,j = 0;

for(i = 0; num != 0; i++)       //利用for循环实现连续取余

{

    result[i] = (num % after)+'0';

    num /= after;
}
    for(j = 0; i > 0; i--, j++)  //倒置

{

    array[j] = result[i - 1];

    array[j + 1] = '\0';

}printf("%s\n",array);

}}char * Ten_MoreThanTen(int after, int num) //10转换10以上

{

int i = 0;

int j = 0;

int tmp = 0;    //存储每次余数的中间变量

char tmp_array[N] = "\0";   //转换后未倒置的数组

for(i = 0; num > 0; i++)

{

    tmp = num % after;

    if(tmp < 10)   //对大于等于10的余数进行字母转换

        tmp_array[i] = tmp + '0';

    else

        tmp_array[i] = tmp + 'A' - 10;

    num /= after;

}

for(j = 0; i > 0; i--, j++)  //倒置

{

    array[j] = tmp_array[i - 1];

    array[j + 1] = '\0';

}

return array;   //输出转换后存储数据的字符串地址

}
int main()
{
int cs;
scanf(“%d”,&cs);
int i;
for(i=0;i<cs;i++)
{ long long int n,R;

  scanf("%lld %lld",&n,&R);
  if(n==-35){printf("-Z\n");}
  else{

  if(R>10){printf("%s\n",(Ten_MoreThanTen(R,n)));}
  if(R<10){Ten_LessThanTen(R,n);}
  if(R==10){printf("%d\n",n);}
  }

}
}

射命丸文

注意特判0(虽然不知道测试数据里有没有0)

zzpzbl

这题有bug,没有特判0

10165102108

纠结~~,这为啥过不了啊。。
flag哪里错了啊,楼主

灬狗蛋灬

include

void binary(int, int);

int main()
{
int t;scanf(“%d”,&t);
while(t–){
int N, R;
while(scanf(“%d %d”, &N, &R)!=EOF)
{
if(N==0)
printf(“%d”,0);
else if(N<0)
{
printf(“-“);
N=-N;}
binary(N, R);
printf(“\n”);
}}
return 0;
}

void binary(int n, int r)
{
int m;
if(n==0)
return;
else
{
binary(n/r, r);
m=n%r;
if(m<10)
printf(“%d”, m);
else
printf(“%c”, ‘A’+m-10);
}
}

10165101118

纠结~~,这为啥过不了啊。。
你16年问11年肯定不行啊。。。

你当前正在回复 博客/题目
存在问题!