zoukankan      html  css  js  c++  java
  • 洛谷 1017 进制转换 (NOIp2000提高组T1)

    【题解】

      纯模拟题。

      我们都知道十进制数化成m进制数可以用短除法,即除m取余、逆序排列。而m进制数化为十进制数,按权展开求和即可。

      但在本题中进制的基数R可能为负数,我们知道a%R的符号与R一致,也就是说在本题中我们用短除法得到的某一位上的值可能为负数,要注意向上一位借1化成正的。

     

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 #define LL long long
     5 #define rg register
     6 #define N 200010
     7 using namespace std;
     8 int n,r,tot,a[N];
     9 inline int read(){
    10     int k=0,f=1; char c=getchar();
    11     while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
    12     while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar();
    13     return k*f;
    14 }
    15 int main(){
    16     n=read(); r=read();
    17     printf("%d=",n);
    18     while(n){
    19         a[++tot]=n%r;
    20         n/=r;
    21         if(a[tot]<0) a[tot]-=r,n+=1; 
    22     }
    23     for(rg int i=tot;i;i--) printf("%c",a[i]<10?a[i]+'0':a[i]-10+'A');
    24     printf("(base%d)
    ",r);
    25     return 0;
    26 }
    View Code
  • 相关阅读:
    【poj3764】 The xor-longest Path
    【poj3261】 Milk Patterns
    【poj3237】 Tree
    【bzoj2654】 tree
    【poj3122】 Pie
    【poj1011】 Sticks
    【poj1186】 方程的解数
    【poj2741】 Colored Cubes
    【poj3141】 Distant Galaxy
    【bzoj2456】 mode
  • 原文地址:https://www.cnblogs.com/DriverLao/p/9397672.html
Copyright © 2011-2022 走看看