[OJ.2131]資料結構實驗之棧與佇列一:進位制轉換
阿新 • • 發佈:2018-12-15
資料結構實驗之棧與佇列一:進位制轉換
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
Hint
程式碼
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <algorithm> #define Maxsize 2000 using namespace std; typedef struct node { int *data; int maxsize; int top; }stack; void in(stack &s) { s.data = new int[Maxsize]; s.maxsize = Maxsize; s.top = -1; }//初始化操作 void push(stack &s, int k) { s.data[++s.top] = k; } void pop(stack &s) { s.top--; } int isempty(stack &s) { return(s.top==-1); } int top(stack &s) { return s.data[s.top]; } int main() { int n, m; stack s; cin>>n>>m; in(s); while(n>=m) { push(s, n%m); n/=m; }//進位制轉化問題的演算法 push(s, n); while(!isempty(s)) { cout<<top(s); pop(s); } cout<<endl; return 0; }