回覆列表
  • 1 # 使用者2458114238191884

    你可以設定一個字串陣列

    charHEX[16]={"0","1","2",3","4","5","6","7","8","9","A","B","C","D","E","F"}

    以後你得出的數字,比如餘數y是10,那麼HEX[y]打印出來就是A了

    例子(現寫的)

    intnow;

    intres[100];//答案

    intc=0;//位數

    輸入到now;

    while(now>0)

    {

    res[c]=now%16;

    now=now/16;

    c++;

    }

    輸出是:

    for(i=0;i<c;i++)

    {

    輸出HEX[res[i]];

    }

    演算法的精髓都給你啦,好好理解吧

    /**********************************************

    ****************補充*****************

    **********************************************/

    改你的程式碼

    #include"stdio.h"

    voidmain()

    {

    charHEX[16]={"0","1","2",3","4","5","6","7","8","9","A","B","C","D","E","F"};//<----------------這裡加了

    intnum;

    printf("請輸入一個十進位制整數:");

    scanf("%d",&num);

    inta[10];//<----------------這裡改了

    inti=0;

    intyushu;

    while(num>0)

    {

    yushu=num%16;

    a[i]=yushu;

    num=num/16;

    i++;/*關鍵的就在這裡,這後面書上寫的要用SWITCH,但是我用不好,請各位大蝦指教*/

    }

    printf("轉化為十六進位制的數為:");

    for(;i>=0;i--)//倒序輸出

    {

    printf("%c",HEX[a[i]]);//<----------------這裡改了

    }

    printf("\n");

    }

  • 中秋節和大豐收的關聯?
  • 用河蝦怎麼釣魚,蚯蚓怎麼裝鉤?