1516 - 八进制

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 32 MB

输入一个整数,将其转换成八进制数输出。

题目输入

输入包括一个整数N(0<=N<=100000)。

题目输出

可能有多组测试数据,对于每组数据,

输出N的八进制表示数。

输入/输出样例

输入格式

9
8
5

输出格式

11
10
5

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

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

Java解答

import java.util.*;
public class Main 
{
	public static void main(String[] args) 
	{
		int a,b,n;
		Stack<Integer> s = new Stack<Integer>();
		Scanner cin=new Scanner(System.in);
		while(cin.hasNext()) 
		{
			n=cin.nextInt();
			if(n==0)
				System.out.print("0");
			while(n>0)
			{
				s.push(n%8);
				n=n/8;
			}
			while(s.empty()!=true)
			{
				System.out.print(s.peek());
				s.pop();
			}
			System.out.print("\n");
		}
		cin.close();
	}
}