算法思路:
(1) 读入N进制的数,先将其转换为10进制
(2)然后再把十进制转换为M进制.
(3)要用到的数据结构,栈,队列
代码如下(N,M必须小于10.)
queue.h
stack.h
主程序:
(1) 读入N进制的数,先将其转换为10进制
(2)然后再把十进制转换为M进制.
(3)要用到的数据结构,栈,队列
代码如下(N,M必须小于10.)
queue.h
#ifndef __queue__h
#define __queue__h
#define MAX 100
class queue
{
public:
queue()
{
head = tail = 0;
}
void Enqueue(int value)
{
if((tail + 1)%MAX == head)return;
s[tail] = value;
tail = (tail + 1)%MAX;
}
int Dequeue()
{
if(head == tail )return 0;
int e;
e = s[head];
head = (head+1)%MAX;
return e;
}
bool IsEmpty()
{
return tail == head;
}
int Count()
{
return (tail -head+MAX)%MAX;
}
private:
int s[MAX];
int head;
int tail;
};
#endif
#define __queue__h
#define MAX 100
class queue
{
public:
queue()
{
head = tail = 0;
}
void Enqueue(int value)
{
if((tail + 1)%MAX == head)return;
s[tail] = value;
tail = (tail + 1)%MAX;
}
int Dequeue()
{
if(head == tail )return 0;
int e;
e = s[head];
head = (head+1)%MAX;
return e;
}
bool IsEmpty()
{
return tail == head;
}
int Count()
{
return (tail -head+MAX)%MAX;
}
private:
int s[MAX];
int head;
int tail;
};
#endif
stack.h
#ifndef __stack__h
#define __stack__h
#define MAX 100
class stack
{
public:
stack()
{
top = 0;
}
bool IsEmpty()
{
return top == 0;
}
void Push(int value)
{
if(top <= MAX)
{
s[top] = value;
top++;
}
}
int Pop()
{
if(top >= 0)
{
top--;
return s[top];
}
else
return -1;
}
private:
int s[MAX];
int top;
};
#endif
#define __stack__h
#define MAX 100
class stack
{
public:
stack()
{
top = 0;
}
bool IsEmpty()
{
return top == 0;
}
void Push(int value)
{
if(top <= MAX)
{
s[top] = value;
top++;
}
}
int Pop()
{
if(top >= 0)
{
top--;
return s[top];
}
else
return -1;
}
private:
int s[MAX];
int top;
};
#endif
主程序:
#include "queue.h"
#include "stack.h"
#include<stdio.h>
int main()
{
int n,m,x,sum = 0;
queue q;
stack s;
scanf("%d%d",&n,&m);
scanf("%d",&x);
while(x >= 10)
{
q.Enqueue(x%10);
x = x/10;
}
q.Enqueue(x%10);
int count = q.Count();
for(int i = 0;i < count;i++)
{
if(q.IsEmpty())
break;
x = q.Dequeue();
if(x == 0)
continue;
for(int j = 0;j < i;j++)
x*=n;
sum+=x;
}
while(sum > 0)
{
s.Push(sum % m);
sum = sum /m;
}
while(!s.IsEmpty())
{
printf("%d ",s.Pop());
}
return 0;
}
#include "stack.h"
#include<stdio.h>
int main()
{
int n,m,x,sum = 0;
queue q;
stack s;
scanf("%d%d",&n,&m);
scanf("%d",&x);
while(x >= 10)
{
q.Enqueue(x%10);
x = x/10;
}
q.Enqueue(x%10);
int count = q.Count();
for(int i = 0;i < count;i++)
{
if(q.IsEmpty())
break;
x = q.Dequeue();
if(x == 0)
continue;
for(int j = 0;j < i;j++)
x*=n;
sum+=x;
}
while(sum > 0)
{
s.Push(sum % m);
sum = sum /m;
}
while(!s.IsEmpty())
{
printf("%d ",s.Pop());
}
return 0;
}