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

来源:互联网 发布:grub引导linux 编辑:程序博客网 时间:2024/05/22 11:38

Problem Description

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

Input

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

Output

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

Example Input

12798

Example Output

2377



#include <stdio.h>#include <stdio.h>#define max 100000int top, stack[max];int Push(int stack[], int s){ if(top > max) { return 0; } else { stack[top++] = s; return 1; }}int Pop(int stack[]){ if(top == 0) { return 0; } else { top--; return stack[top]; }}int main(){ int n, R, s; scanf("%d%d", &n, &R); if(n == 0) { printf("0\n"); } else { while(n != 0) { s = n % R; Push(stack, s); n = n / R; } while(top) { printf("%d", Pop(stack)); } printf("\n"); } return 0;}
0 0
原创粉丝点击