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

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;
}

最新文章

  1. CentOS:ECDSA host key &quot;ip地址&quot; for has changed and you have requested strict checking(转)
  2. ps如何裁剪掉图片的不规则区域
  3. linux 命令行启动虚拟机
  4. JavaWeb学习总结-02 Tomcat 学习和使用
  5. css中width的计算方式,以及width:100%的参考系
  6. [笔记]--Oracle 10g在Windows 32位系统使用2G以上内存
  7. c语言_帮助别人
  8. POJ1860 Currency Exchange(最短路)
  9. linux内存管理--slab及其代码解析
  10. 网页 CSS样式表
  11. 【转载】图文详解 IntelliJ IDEA 15 创建普通 Java Web 项目
  12. 【Android Developers Training】 24. 保存键值对
  13. Monit : 开源监控工具介绍
  14. C# 委托详解(一)
  15. 从零开始学习前端开发 — 2、CSS基础
  16. Java AQS 概述
  17. pyinstaller 打包生成exe之后运行提示‘no module name &#39;xxx&#39;’错误
  18. How do I configure a Wired Ethernet interface
  19. Python下字符画(ascii art)生成
  20. g++编译器的使用

热门文章

  1. 自定义确定框(confirm)
  2. CesiumLab地形处理成果在Tomcat和IIS上发布
  3. Zookeeper 扫盲
  4. 【react】react-bookManager
  5. 考试总结 模拟28(W)
  6. gcd(辗转相除法)
  7. 洛谷1081 (NOIp2012) 开车旅行——倍增预处理
  8. Linux常用命令操作详解
  9. JS---案例:开机动画
  10. 确定比赛名次 HDU - 1285 (拓扑排序)