0分求教

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

PH13 @ 2024-10-07 16:33:23

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long

using namespace std;

ll n;
int r;
char a[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char b[]="";

int main(){
    scanf("%lld%d", &n, &r);
    int cnt = 0;
    while(n > 0){
        int i = n % r;
        b[cnt++] = a[i];
        n /= r;
    }
    for(int i=cnt-1; i>=0; i--){
        printf("%c", b[i]);
    }
    return 0;
}

为啥样例能过,我自己敲的转16进制的也能过,提交就全wa啊


by tt_eason @ 2024-10-07 16:52:05

在提交建面把O2优化关掉就能AC,O2优化不支持这个:b[]=""


by zhaiziyiJoey @ 2024-10-07 16:59:04

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long

using namespace std;

ll n;
int r;
char a[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string b;

int main(){
    scanf("%lld%d", &n, &r);
    int cnt = 0;
    while(n > 0){
        int i = n % r;
        b[cnt++] = a[i];
        n /= r;
    }
    for(int i=cnt-1; i>=0; i--){
        printf("%c", b[i]);
    }
    return 0;
}

把char b[]改成string b就可以AC了


by PH13 @ 2024-10-07 17:13:26

感谢两位大佬www


by CHEVROLET_CAMARO @ 2024-12-15 10:59:02

咋在按要求排版的情况下发布代码


|