您的位置:首页 > 理论基础 > 数据结构算法

数据结构实验之栈与队列一:进制转换

2017-10-15 17:44 295 查看

数据结构实验之栈与队列一:进制转换

Time Limit: 1000MSMemory Limit: 65536KB
[align=center]SubmitStatistic[/align]

Problem Description

输入一个十进制非负整数,将其转换成对应的 R (2 <= R <= 9) 进制数,并输出。

Input

第一行输入需要转换的十进制非负整数;

第二行输入 R。

Output

输出转换所得的 R 进制数。

Example Input

1279
8


Example Output

2377


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

#include <iostream>

#include <stdlib.h>

using namespace std;

#define ADDSIZE 50

#define SIZE 100

struct node

{

 int size ;

 int *base,*top;

};

void Inistack(node &s)

{

 s.base=new int[SIZE];

 s.top=s.base;

 s.size=SIZE;

}

int Gettop(node &s)

{

 if(s.top==s.base)

 {

  return 0;

 }

 else

 {

  return *(s.top-1);

 }

}

void Push(node &s,int e)

{

 if(s.top-s.base>=s.size)

 {

  s.base=(int *)realloc(s.base,(s.size+ADDSIZE)*sizeof(int));

  s.top=s.base+s.size;

  s.size+=ADDSIZE;

 }

 *s.top++=e;

}

int Pop(node &s,int e)

{

 return e=*--s.top;

}

int main()

{

 node s;

 Inistack(s);

 int n,m,e;

 cin>>n>>m;

 if(n)//注意零的判断

 {

  while(n)

  {

   Push(s,n%m);

   n=n/m;

  }

  while(Gettop(s))

  {

   e=Pop(s,e);

   cout<<e;

  }

  cout<<endl;

 }

 else

 {

  cout<<"0"<<endl;

 }

 return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: