SDUT-2131_数据结构实验之栈与队列一:进制转换
数据结构实验之栈与队列一:进制转换
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_数据结构实验之栈与队列一:进制转换的更多相关文章
- SDUT-2088_数据结构实验之栈与队列十一:refresh的停车场
数据结构实验之栈与队列十一:refresh的停车场 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description refresh最近发 ...
- SDUT-2449_数据结构实验之栈与队列十:走迷宫
数据结构实验之栈与队列十:走迷宫 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 一个由n * m 个格子组成的迷宫,起 ...
- SDUT-1479_数据结构实验之栈与队列九:行编辑器
数据结构实验之栈与队列九:行编辑器 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 一个简单的行编辑程序的功能是:接受用 ...
- SDUT-3335_数据结构实验之栈与队列八:栈的基本操作
数据结构实验之栈与队列八:栈的基本操作 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 堆栈是一种基本的数据结构.堆栈具 ...
- SDUT-3334_数据结构实验之栈与队列七:出栈序列判定
数据结构实验之栈与队列七:出栈序列判定 Time Limit: 30 ms Memory Limit: 1000 KiB Problem Description 给一个初始的入栈序列,其次序即为元素的 ...
- SDUT-3332&3333_数据结构实验之栈与队列五:下一较大值
数据结构实验之栈与队列六:下一较大值 Time Limit: 150 ms Memory Limit: 8000 KiB Problem Description 对于包含n(1<=n<=1 ...
- SDUT-2134_数据结构实验之栈与队列四:括号匹配
数据结构实验之栈与队列四:括号匹配 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给你一串字符,不超过50个字符,可能 ...
- SDUT-2133_数据结构实验之栈与队列三:后缀式求值
数据结构实验之栈与队列三:后缀式求值 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于一个基于二元运算符的后缀表示式 ...
- SDUT-2132_数据结构实验之栈与队列二:一般算术表达式转换成后缀式
数据结构实验之栈与队列二:一般算术表达式转换成后缀式 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于一个基于二元运 ...
随机推荐
- mysql limit的使用方法
mysql的分页limit的使用方法大全 .取表中的n行之后的m条元组(limit n,m) ,; //取出student表中第4行后的8条元组(这里的区间是左开右闭) .取出表中前m行元素(limi ...
- 006-使用python编写一个猜数字的程序
题目:随机生成一个数字,共有三次机会对该数字进行猜测. #功能点# 1.猜错的时候给出提示,告诉用户输入的值是大了还是小了# 2.最多提供三次机会# 3.随机生成需要猜的数字答案 编写思路: 1.刚开 ...
- 计蒜客 Red Black Tree(树形DP)
You are given a rooted tree with n nodes. The nodes are numbered 1..n. The root is node 1, and m of ...
- Leetcode590N-ary Tree Postorder TraversalN叉树的后序遍历
给定一个 N 叉树,返回其节点值的后序遍历. class Node { public: int val; vector<Node*> children; Node() {} Node(in ...
- leetcode 1-20 easy
1.Two Sum Given an array of integers, return indices of the two numbers such that they add up to a s ...
- GCC/GDB学习
GCC学习 1.gcc是根据后缀名来区分文件的 .c : c语言源文件 .a : 目标文件构成的库文件 .C/.cc/.cxx : c++源文件 .h : 头文件 .i : 预处理过的C源文件 .ii ...
- python中数字转换成字符串
数字转换成字符串: num=123 str='%d' %num str就变成了"123"
- Matlab 稀疏矩阵函数
eye 单位矩阵zeros 全零矩阵ones 全1矩阵rand 均匀分布随机阵genmarkov 生成随机Markov矩阵linspace 线性等分向量logspace 对数等分向量logm 矩阵对数 ...
- 大数据心法来了!一站式玩转MaxCompute,还有开发者资源等你领!
阿里云大数据计算平台开发者版2019年3月推出,MaxCompute正在成为开发者的免费大数据平台.今天,MaxCompute在企业构建自己的数据处理平台实践中起到了至关重要的作用,我们特别精选了企业 ...
- 斯坦福CS课程列表
http://exploredegrees.stanford.edu/coursedescriptions/cs/ CS 101. Introduction to Computing Principl ...