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

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

SDUT-2131_数据结构实验之栈与队列一:进制转换的更多相关文章

  1. SDUT-2088_数据结构实验之栈与队列十一:refresh的停车场

    数据结构实验之栈与队列十一:refresh的停车场 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description refresh最近发 ...

  2. SDUT-2449_数据结构实验之栈与队列十:走迷宫

    数据结构实验之栈与队列十:走迷宫 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 一个由n * m 个格子组成的迷宫,起 ...

  3. SDUT-1479_数据结构实验之栈与队列九:行编辑器

    数据结构实验之栈与队列九:行编辑器 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 一个简单的行编辑程序的功能是:接受用 ...

  4. SDUT-3335_数据结构实验之栈与队列八:栈的基本操作

    数据结构实验之栈与队列八:栈的基本操作 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 堆栈是一种基本的数据结构.堆栈具 ...

  5. SDUT-3334_数据结构实验之栈与队列七:出栈序列判定

    数据结构实验之栈与队列七:出栈序列判定 Time Limit: 30 ms Memory Limit: 1000 KiB Problem Description 给一个初始的入栈序列,其次序即为元素的 ...

  6. SDUT-3332&3333_数据结构实验之栈与队列五:下一较大值

    数据结构实验之栈与队列六:下一较大值 Time Limit: 150 ms Memory Limit: 8000 KiB Problem Description 对于包含n(1<=n<=1 ...

  7. SDUT-2134_数据结构实验之栈与队列四:括号匹配

    数据结构实验之栈与队列四:括号匹配 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给你一串字符,不超过50个字符,可能 ...

  8. SDUT-2133_数据结构实验之栈与队列三:后缀式求值

    数据结构实验之栈与队列三:后缀式求值 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于一个基于二元运算符的后缀表示式 ...

  9. SDUT-2132_数据结构实验之栈与队列二:一般算术表达式转换成后缀式

    数据结构实验之栈与队列二:一般算术表达式转换成后缀式 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于一个基于二元运 ...

随机推荐

  1. 定时任务 $ ls /etc/cron* + cat$ for user in $(cat /etc/passwd | cut -f1 -d:); do crontab -l -u $user; done

    是否有某个定时任务运行过于频繁? 是否有些用户提交了隐藏的定时任务? 在出现故障的时候,是否正好有某个备份任务在执行?

  2. php分页查询的简单实现代码

    <body><h1>分页查询</h1><?phpinclude("DADB.class.php");$db=new DADB();$tj= ...

  3. 引用CDN内容的方法总结

    1.1.1 摘要 CDN相信大家都听说过,甚至使用过相关的技术,也许有些人会回答“没有听说过和使用过该技术”,真的是这样吗? CDN的全称是Content Delivery Network,即内容分发 ...

  4. 如何在CentOS 7 / Fedora 31/30/29上安装ELK Stack

    原文地址:https://computingforgeeks.com/how-to-install-elk-stack-on-centos-fedora/ 原作者: Josphat Mutai 译者: ...

  5. golang数组 排序和查找

    package main import "fmt" func BubbleSort(arr *[5]int){ fmt.Println("排序前arr=",(* ...

  6. webserver的性能问题,一语道破真谛

    一直纠结我们要大减的paas平台需要支持多大的并发数. 看到一个网友所说的,恍然大悟,按原有我的理解和要求,并发达到w级 req/s,已经是非常高的要求了,单纯从软件上是很难实现的,一定要以来硬件上的 ...

  7. Linux SSH远程链接 短时间内断开

    Linux SSH远程链接 短时间内断开 操作系统:RedHat 7.5 问题描述: 在进行SSH链接后,时不时的就断开了 解决方案: 修改 /etc/ssh/sshd_config 文件,找到 Cl ...

  8. 使用帝国备份王软件提示 Parse error: syntax error, unexpected end of file

    使用帝国备份王软件提示 Parse error: syntax error, unexpected end of file时, 可以尝试一下方法: 1.php.ini要把short_open_tag ...

  9. 2019.10.22 用TCP实现服务端并发接收

    client import socket client = socket.socket() client.connect( ('127.0.0.1',8888) ) while 1: msg = in ...

  10. day40-Spring 02-事务的回顾