1516 - 八进制
输入一个整数,将其转换成八进制数输出。
Input
输入包括一个整数N(0<=N<=100000)。
Output
可能有多组测试数据,对于每组数据,
输出N的八进制表示数。
Examples
Input
9 8 5
Output
11 10 5
Solution C
#include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 typedef int Status; typedef struct StackNode { int data; struct StackNode *next; }StackNode; typedef struct Stack { StackNode *top; }Stack; Status InitStack(Stack *s) { s->top=NULL; return OK; } Status Push(Stack *s,int e) { StackNode *newnode=(StackNode *)malloc(sizeof(StackNode)); newnode->data=e; newnode->next=s->top; s->top=newnode; return OK; } Status Pop(Stack *s,int *e) { if(s->top==NULL) return ERROR; *e=s->top->data; return OK; } Status StackEmpty(Stack s) { if(s.top==NULL) return OK; return ERROR; } Status DestroyStack(Stack *s) { StackNode *p; p=s->top; s->top=s->top->next; free(p); return OK; } int main() { int N,e; Stack s; while(scanf("%d",&N)!=EOF) { if(N==0) printf("0"); InitStack(&s); while(N) { Push(&s,N%8); N/=8; } while(!StackEmpty(s)) { Pop(&s,&e); printf("%d",e); DestroyStack(&s); } printf("\n"); } return 0; }
Solution C++
#include<stdio.h> int main() { int n,a[10],i,k; while(scanf("%d",&n)!=EOF) { if(!n) { puts("0"); continue; } k=0; while(n) { a[k++]=n%8; n/=8; } for(i=k-1;i>=0;i--) printf("%d",a[i]); puts(""); } return 0; }