P2064进制转换

题目:https://www.luogu.org/problemnew/show/P2084

既然这道题放在字符串类型里,那么这里的N肯定得用字符数组来储存(这样也方便输出)。

那么我们不妨定义一个字符串数组a[1001](他说n的位数在1000以下),输出格式就是a[i]*m*k+.......(k为m的几次方),接下来就是求k了。我们发现,最低位的k是0,那么假设n有x位,最高位的k就是x-1.

所以每输出一次,k就要减一。

话不多说,代码奉上:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int m,k;
char a[1001];
int main()
{cin>>m>>a;
k=strlen(a)-1;//最高位的k
int t=strlen(a);
  for(int i=0;i<t;i++)
  {if(a[i]!='0')//0要省略
   cout<<a[i]<<"*"<<m<<"^"<<k; 
  if(k!=0&&a[i+1]!='0')cout<<"+";//"+"是个恶心的玩意,要加特判
  k--;
  }
 
  

猜你喜欢

转载自www.cnblogs.com/lcez56jsy/p/10452912.html
今日推荐