SDUT-2131_数据结构实验之栈与队列一:进制转换
2024-10-07 22:45:58
数据结构实验之栈与队列一:进制转换
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
输入一个十进制非负整数,将其转换成对应的 R (2 <= R <= 9) 进制数,并输出。
Input
第一行输入需要转换的十进制非负整数;
第二行输入 R。
Output
输出转换所得的 R 进制数。
Sample Input
1279
8
Sample Output
2377
这题考了栈的进栈与出栈,其他的就是进制转换的模板了,由于只有2到9,还是比较简单的。
非线性
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node//栈的节点
{
int data;
struct node *next;
}Node;
typedef struct stack
{
Node *base,*top;
}Stack;
Node *newnode()//开辟一个节点
{
Node *t;
t = (Node *)malloc(sizeof(Node));
t->next = NULL;
return t;
}
Stack *Newstack()//建立一个新栈
{
Stack *t;
t = (Stack *)malloc(sizeof(Stack));
t->top = newnode();
t->base = t->top;
return t;
}
void push(Stack *t,int x)//入栈
{
Node *p = newnode();
p->data = x;
p->next = t->top->next;
t->top->next = p;
t->base = p;
}
int top(Stack *t)//询问栈顶元素
{
return t->top->next->data;
}
void pop(Stack *t)//出栈
{
Node *p;
p = t->top->next;
t->top->next = t->top->next->next;
free(p);
}
void show(Stack *t)//输出栈
{
while(t->top->next)
{
printf("%d",top(t));
pop(t);
}
printf("\n");
}
int main()
{
int n,r;
scanf("%d%d",&n,&r);
Stack *t;
t = Newstack();
if(n==0)
printf("0\n");
while(n)
{
push(t,n%r);
n /= r;
}
show(t);
return 0;
}
线性
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct stack
{
int *top,*base;
int len;
}Stack;
Stack newstack()//建立新栈
{
Stack t;
t.top = (int *)malloc(40*sizeof(int));
t.base = t.top;
t.len = 0;
return t;
}
int top(Stack *t)//询问栈顶元素
{
return *(t->top-1);
}
void pop(Stack *t)//出栈
{
t->top--;
t->len--;
}
void push(Stack *t,int x)//进栈
{
*(t->top) = x;
t->top++;
t->len++;
}
int main()
{
int x,r;
Stack t;
t = newstack();
scanf("%d%d",&x,&r);
if(x==0)
push(&t,0);
while(x)
{
push(&t,x%r);
x /= r;
}
while(t.len)
{
printf("%d",top(&t));
pop(&t);
}
printf("\n");
return 0;
}
最新文章
- CentOS:ECDSA host key ";ip地址"; for has changed and you have requested strict checking(转)
- ps如何裁剪掉图片的不规则区域
- linux 命令行启动虚拟机
- JavaWeb学习总结-02 Tomcat 学习和使用
- css中width的计算方式,以及width:100%的参考系
- [笔记]--Oracle 10g在Windows 32位系统使用2G以上内存
- c语言_帮助别人
- POJ1860 Currency Exchange(最短路)
- linux内存管理--slab及其代码解析
- 网页 CSS样式表
- 【转载】图文详解 IntelliJ IDEA 15 创建普通 Java Web 项目
- 【Android Developers Training】 24. 保存键值对
- Monit : 开源监控工具介绍
- C# 委托详解(一)
- 从零开始学习前端开发 — 2、CSS基础
- Java AQS 概述
- pyinstaller 打包生成exe之后运行提示‘no module name &#39;xxx&#39;’错误
- How do I configure a Wired Ethernet interface
- Python下字符画(ascii art)生成
- g++编译器的使用