没输出???啥情况???

B3849 [GESP样题 三级] 进制转换

liyifanglan @ 2024-07-17 15:15:51

#include<bits/stdc++.h>
using namespace std;

char c[30] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'I', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
int main(){
    int n, r;
    cin >> n >> r;
//  cout << n << endl << r;
    int a[1000000], i;
    for (i = 0; n != 0; i++){
        a[i] = n%r;
        n /= r; 
    }
    for (int j = i; j >= 0; j--){
        if (a[i] >= 10) cout << c[a[j]-10];
        else cout << a[j];
    }
    return 0;
}

by Liujiaxiang1 @ 2024-07-17 15:23:16

#include<bits/stdc++.h>
using namespace std;
char c[30] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'I', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
int a[1000000],i;
int main(){
    int n,r;
    cin>>n>>r;
    for (i=0;n!=0;i++){
        a[i]=n%r;
        n/=r;   
    }
    for(int j=i-1;j>=0;j--){
        if(a[j]>=10)cout<<c[a[j]-10];
        else cout << a[j];
    }
    return 0;
}

@liyifanglan


by keep_shining @ 2024-07-17 15:24:30

@liyifanglan 大数组请放到函数外面开,在函数里面有可能会分配内存失败


by Liujiaxiang1 @ 2024-07-17 15:27:52

  1. a数组太大,应定义为全局变量
  2. 12行j=i-1否则有前导0
  3. 13行if(a[j]>=10)

求关qwq


by liyifanglan @ 2024-07-17 15:30:59

谢谢


by Liujiaxiang1 @ 2024-07-17 15:33:03

我说的行是我写的代码 @liyifanglan


by caojinyu0518 @ 2024-08-06 20:07:17

@liyifanglan 谨记 数组请定义全局!!!


by caojinyu0518 @ 2024-08-06 20:12:32

@liyifanglan 否则可能会内存溢出


|