晚上躺在床上翻来覆去睡不着觉,突然想起了上学时候老师出的很久以前的一个问题,请写一个将输入的十进制数转换成八进制数的题目。我想了四种方法,分别是最高效率的,最短的,是用堆栈的,不用堆栈的,大家提供一下思路,答的最好的给100,只给一人。抢答开始。

解决方案 »

  1.   

    int a = 11111;
    int b[ 10 ];
    int i;
    for( i = 0; i < 10; i++ )
    {
       b[ i ] = ( a >> ( i * 3 ) & 0x07 );
       a >>= ( i * 3 ) & 0x07;
       if( !a )
            break;
    }
      

  2.   

    #include "stdafx.h"
    #include "stdio.h"int main(int argc, char* argv[])
    {
    int i=89567;
    printf("%o",i);
    return 0;
    }
      

  3.   

    #include "stdafx.h"
    #include "stdio.h"
    #include "stdlib.h"int main(int argc, char* argv[])
    {
    int i=89567;
    char str[100]; itoa(i,str,8);
    int b=atoi(str);
    printf("%d\n",b); return 0;
    }
      

  4.   

    #include<stdio.h>
    #include<stdlib.h>
    #include<malloc.h>
    #include<conio.h>
    #define ERROR 0
    #define TRUE 1
    #define FALSE 0
    #define OK 1
    #define EQUAL 1
    #define OVERFLOW -1
    #define STACK_INIT_SIZE 100
    #define STACKINCREMENT 10typedef int Status ;typedef int SElemType;struct STACK
    {
      SElemType *base;
      SElemType *top;
      int stacksize;
    };typedef struct STACK SqStack;
    typedef struct STACK *pSqstack;Status InitStack(SqStack  **S);
    Status DestroyStack(SqStack *S);
    Status ClearStack(SqStack *S);
    Status StackEmpty(SqStack S);
    int StackLength(SqStack S);
    Status GetTop(SqStack S,SElemType *e);
    Status Push(SqStack *S,SElemType e);
    Status Pop(SqStack *S,SElemType *e);
    Status StackTraverse(SqStack S,Status (*visit)());Status InitStack(SqStack **S)
    {
      (*S)=(SqStack *)malloc(sizeof(SqStack));
      (*S)->base=(SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType));
      if(!(*S)->base)exit(OVERFLOW);
      (*S)->top=(*S)->base;
      (*S)->stacksize=STACK_INIT_SIZE;
      return OK;
    }Status DestroyStack(SqStack *S)
    {
     free(S->base); 
     free(S);
     return OK;
    }Status ClearStack(SqStack *S)
    {
      S->top=S->base;
      return OK;
    }Status StackEmpty(SqStack S)
    {
      if(S.top==S.base) return TRUE;
      else
        return FALSE;
    }int StackLength(SqStack S)
    {
      int i;
      SElemType *p;
      i=0;
      p=S.top;
      while(p!=S.base)
        {p++;
         i++;
        }
      return i;
    }Status GetTop(SqStack S,SElemType *e)
    {
      if(S.top==S.base) return ERROR;
      *e=*(S.top-1);
      return OK;
    }Status Push(SqStack *S,SElemType e)
    {
     
      if(S->top - S->base>=S->stacksize)
       {     S->base=(SElemType *) realloc(S->base,
        (S->stacksize + STACKINCREMENT) * sizeof(SElemType));
         if(!S->base)exit(OVERFLOW);
         S->top=S->base+S->stacksize;
         S->stacksize += STACKINCREMENT;
       }
        *(S->top++)=e;
      return OK;
    }Status Pop(SqStack *S,SElemType *e)
    {
      if(S->top==S->base) return ERROR;
      *e=*--S->top;
      return OK;
    }Status visit(SElemType * e)
    {
      printf(" %d  ", *e);
      return OK;
    }void conversion()
    {
     pSqstack S;
     SElemType e; int n;
     InitStack(&S);
     printf("Input a number to convert to OCT:\n");
     scanf("%d",&n); if(n<0)
       {
         printf("\nThe number must be over 0.");
         return;
       }
     if(!n) Push(S,0); while(n){
       Push(S,n%8);
       n=n/8;
     } printf("the result is:        ");
     while(!StackEmpty(*S)){
       Pop(S,&e);
       printf("%d",e);
     }
    }void main()
    {
      printf("\n\n\n\n");
      conversion();
      getch();
      return;
    }
    /* 挺简单,节选自http://www.njnz.com/sjjg/index.htm ,大家看着玩吧 */
      

  5.   

    同意 testsofter(众人拾柴) 
    输入的数按八进制输出出来就行了。
      

  6.   

    用全局数据和栈
    ----------------------------------------------------------------
    #include "stdio.h"
    #include "stdlib.h"int result=0;
    int i=0;void func(int init)
    {
    int mul=1;
    for(int j=0;j<i;j++)
    mul*=10;
    i++;
    result+=(init%8)*mul; if(init<8)
    return; func(init/8);
    }int main(int argc, char* argv[])
    {
    func(89567);
    printf("%d\n",result);
    return 0;
    }
      

  7.   

    完全同意 GZCompilerprintf("%o",i);
      

  8.   

    GZCompiler(编译器) 你真是我的偶像
      

  9.   

    to : jennyvenus(JennyVenus) 能解释一下你的程序吗?
      

  10.   

    approach()那段够长,偶喜欢。呵呵。再看看
      

  11.   

    approach()的程序中写了一个栈的功能函数,但主要部分在这里:
    // in function conversion
    while(n)
    {
       Push(S,n%8);
       n=n/8;
    }
      

  12.   

    各位注意以下,可能我的意思表述不是很准确,要求是能够用一个整数来表示八进制的十进制数,例如:
    #include "stdio.h"
    #include "stdlib.h"
    void main(int argc,char **argv)
    {
    int in,out;
    char temp[200];
    in=87655;//是一个十进制整数
    {
    sprintf(temp,"%o\n",in);
    out=atoi(temp);//out是得到的八进制数
    }
    }
    当然这是一种很简单的写法,但实质性的效率却不高,大家可以参考这个例子来写出自己的想法
      

  13.   

    to:approach()
    我说的堆栈指的是程序的运算过程使得得编译器处理的时候实用堆栈,而不是指数据结构的抽象堆栈,可以理解为  GZCompiler(编译器)使用的递归算法。
      

  14.   

    /*-----------------------------------------------------------------------*
     * filename - ltoa1.c
     *
     * function(s)
     *        itoa      - converts an integer to a string
     *        _ltoa      - converts a long to a string
     *        ultoa     - converts an unsigned long to a string
     *        _itow     - converts an integer to a wide-character string
     *        _ltow     - converts a long to a wide-character string
     *        _ultow     - converts an unsigned long to a wide-character string
     *-----------------------------------------------------------------------*//*
     *      C/C++ Run Time Library - Version 10.0
     *
     *      Copyright (c) 1987, 2000 by Inprise Corporation
     *      All Rights Reserved.
     *
     *//* $Revision:   9.0  $        */#include <stdlib.h>
    #include <_printf.h>
    #include <tchar.h>
    #include <_tchar.h>/*-----------------------------------------------------------------------*Name            itoa  - converts an integer to a string
                    _ltoa  - converts a long to a string
                    ultoa - converts an unsigned long to a stringUsage           char *itoa(int value, char *strP, int radix);
                    char *_ltoa(long value, char *strP, int radix);
                    char *ultoa(unsigned long value, char *strP, int radix);
                    wchar_t *itow(int value, wchar_t *strP, int radix);
                    wchar_t *ltow(long value, wchar_t *strP, int radix);
                    wchar_t *ultow(unsigned long value, wchar_t *strP, int radix);Prototype in    stdlib.h
                    _printf.h for __longtoaDescription     These functions  convert value to a  null-terminated string
                    and  store the  result in  string. With  itoa, value  is an
                    integer;  with _ltoa  it is  a  long;  with ultoa  it is  an
                    unsigned long.  __longtoa is the  internal routine used for
                    all these conversions to ASCII (in longtoa.cas).                radix specifies the base to be used in converting value. it
                    must be between  2 and 36 (inclusive). With  itoa and _ltoa,
                    if value is negative, and  radix is 10, the first character
                    of string is  the minus sign (-). This does  not occur with
                    ultoa. Also, ultoa performs no overflow checking.                maybeSigned is treated as a boolean. If false then value is
                    treated  as unsigned  long and  no sign  will be  placed in
                    *strP.                hexStyle  may take  the values  'a' or  'A' and  determines
                    whether lower or  upper case alphabetics are used  when the
                    radix is 11 or greater.                Note: The space  allocated for string must be  large enough
                    to hold the returned  string including the terminating null
                    character (\0).  These functions can return up to 33 bytes.Return value    All these functions return a pointer to string. There is no
                    error return.*------------------------------------------------------------------------*/_TCHAR *_RTLENTRY _EXPFUNC _itot( int value, _TCHAR *strP, int radix )
    {
    #define dword   unsigned long        return  _longtot ((radix == 10) ? (long) value :
                               (dword)((unsigned)value), strP, radix, (radix == 10),
                               _TEXT('a'));
    }
    /*-----------------------------------------------------------------------*Name            ultoa - converts an unsigned long to a string
                    _ultow - converts an unsigned long to a stringUsage           char *ultoa(unsigned long value, char *string, int radix);
                    wchar_t *_ultow(unsigned long value, wchar_t *string, int radix);Prototype in    stdlib.hDescription     see itoa*------------------------------------------------------------------------*/_TCHAR * _RTLENTRY _EXPFUNC _ultot (unsigned long value, _TCHAR *strP, int radix)
    {
            return  _longtot (value, strP, radix, 0, _TEXT('a'));
    }
    /*-----------------------------------------------------------------------*Name            _ltoa - converts a long to a string
                    _ltow - converts a long to a stringUsage           char *_ltoa(long value, char *string, int radix);Prototype in    stdlib.hDescription     see itoa*------------------------------------------------------------------------*/_TCHAR  * _RTLENTRY _EXPFUNC _ltot (long value, _TCHAR *strP, int radix)
    {
            return  _longtot (value, strP, radix, (radix == 10), _TEXT('a'));
    }这个应当是比较不错的了。
    呵呵。:)
      

  15.   

    /*-----------------------------------------------------------------------*
     * filename - longtoa.c
     *
     * function(s)
     *    __longtoa - converts a long to a character string
     *    __utoa    - converts an unsigned int to a decimal string
     *    __longtow - converts a long to a wide-character string
     *    __utow    - converts an unsigned int to a wide-character decimal string
     *-----------------------------------------------------------------------*//*
     *      C/C++ Run Time Library - Version 10.0
     *
     *      Copyright (c) 1987, 2000 by Inprise Corporation
     *      All Rights Reserved.
     *
     *//* $Revision:   9.0  $        */#include <stdlib.h>
    #include <_printf.h>
    #include <tchar.h>
    #include <_tchar.h>/*-----------------------------------------------------------------------*Name            __longtoa - converts a long to a character string
    __longtow - converts a long to a wide-character stringUsage           char * __longtoa (long value, char *strP, int radix,
                                            char maybeSigned, char hexStyle);
                    wchar_t * __longtow (long value, wchar_t *strP, int radix,
                                            char maybeSigned, wchar_t hexStyle);Prototype in    _printf.hDescription     This function converts a long value to a  null-terminated string
                    and  stores the result in  string strP.                radix specifies the base to be used in converting value. it
                    must be between  2 and 36 (inclusive).                maybeSigned is treated as a boolean. If false then value is
                    treated  as unsigned  long and  no sign  will be  placed in
                    *strP.                hexStyle  may take  the values  'a' or  'A' and  determines
                    whether lower or  upper case alphabetics are used  when the
                    radix is 11 or greater.                Note: The space  allocated for string must be  large enough
                    to hold the returned  string including the terminating null
                    character (\0).  itoa can return  up to 17  bytes; ltoa and
                    ultoa, up to 33 bytes.Return value    pointer to the string*------------------------------------------------------------------------*/_TCHAR * _longtot (long value, _TCHAR *strP, int radix,
                            _TCHAR maybeSigned, _TCHAR hexStyle)
    {
        _TCHAR    buf [34];
        _TCHAR    c, *p, *bufp;    p = strP;    /* If the requested radix is invalid, generate an empty result.
         */
        if (radix >= 2 && radix <= 36)
        {        /* If the value is signed and less than zero, generate a minus sign.
             */
            if (value < 0 && maybeSigned != 0)
            {
                *p++ = _TEXT('-');
                value = -value;
            }        /* Now loop, taking each digit as modulo radix, and reducing the value
             * by dividing by radix, until the value is zeroed.  Note that
             * at least one loop occurs even if the value begins as 0,
             * since we want "0" to be generated rather than "".
             */
            bufp = buf;
            for (;;)
            {
                *bufp++ = (char)((unsigned long)value % radix);
                if ((value = (unsigned long)value / radix) == 0)
                    break;
            }        /* The digits in the buffer must now be copied in reverse order into
             * the target string, translating to ASCII as they are moved.
             */
            while (bufp != buf)
            {
                if ((c = *--bufp) < 10)
                    *p++ = (_TCHAR)(c + _TEXT('0'));
                else
                    *p++ = (_TCHAR)((c - 10) + hexStyle);
            }
        }    /* terminate the output string with a zero.
         */
        *p = _TEXT('\0');
        return (strP);          /* return a pointer to the string */
    }/*-----------------------------------------------------------------------*Name            __utoa, __utow - converts an unsigned int to a decimal stringUsage           char *__utoa(unsigned value, char *buf)
                    wchar_t *__utoa(unsigned value, wchar_t *buf)Prototype in    _printf.hDescription     see __longtoa above.*------------------------------------------------------------------------*/_TCHAR * _utot(unsigned value, _TCHAR *buf)
    {
        return  _longtot ((long)(unsigned long)value, buf, 10, 0, _TEXT('a'));
    }
      

  16.   

    to RomanticProgrammer(兰企鹅) ( ) :哈哈。上面这些很眼熟吧。哈哈。常回家看看。