SDUT 2131 数据结构实验之栈一:进制转换

来源:互联网 发布:定向增发数据 编辑:程序博客网 时间:2024/04/26 16:23

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

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

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

输入

第一行输入需要转换的十进制数;
第二行输入R。

输出

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

示例输入

12798

示例输出

2377

提示

十进制数不断对R取余数后整除,余数入栈,直至十进制数为0,最后依次弹出栈顶元素即可。
#include <bits/stdc++.h>using namespace std;typedef int anytype;struct stacks{struct node{anytype data;struct node *next;}*head;stacks(){head=(struct node *)malloc(sizeof(struct node));head->next=NULL;}bool empty(){if(head->next)return false;return true;}void push(anytype n){struct node *p;p=(struct node *)malloc(sizeof(struct node));p->data=n;p->next=head->next;head->next=p;}void pop(){struct node *p;p=head->next;if(p){head->next=p->next;free(p);}}anytype top(){if(!empty())return head->next->data;return 0;}};int main(){ios::sync_with_stdio(false);int num,r;stacks s;cin>>num>>r;while(num){s.push(num%r);num/=r;}while(!s.empty()){cout<<s.top();s.pop();}cout<<endl;return 0;}


0 0
原创粉丝点击