题解

短除法进制转换

当取模数为负数???

P1017 进制转换

 

 加上上面的操作就可以防止取模结果为负数啦

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<cmath>

using namespace std;
typedef long long ll;

inline int read()
{
    int ans=0;
    char last=' ',ch=getchar();
    while(ch<'0'||ch>'9') last=ch,ch=getchar();
    while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
    if(last=='-') ans=-ans;
    return ans;
}

const int maxn=1e5+10;
int tot=0,a[maxn];
int n,m,mod;

int main()
{
    n=read();mod=read();m=n;
    while(n){
        a[++tot]=n%mod;
        n/=mod;
        if(a[tot]<0) a[tot]=(a[tot]+(-mod))%(-mod),n++;
    //    printf("n=%d  a[tot]=%d\n",n,a[tot]);    
    }
    printf("%d=",m);
    for(int i=tot;i>=1;i--){
        if(a[i]>9){
            printf("%c",64+a[i]-9);
        }
        else printf("%d",a[i]);
    }
    printf("(base%d)\n",mod);
    return 0;
}

 

相关文章:

  • 2022-12-23
  • 2021-10-09
  • 2021-12-29
  • 2021-06-06
  • 2022-12-23
  • 2021-10-12
  • 2021-11-01
  • 2021-09-28
猜你喜欢
  • 2021-09-01
  • 2021-10-31
  • 2022-01-02
  • 2021-12-15
  • 2021-12-12
  • 2022-12-23
相关资源
相似解决方案