144. 二進位轉換十進位

 avatar
user_6817964
c_cpp
2 years ago
1.7 kB
4
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <math.h>


// val : 紀錄0或1
// next : 記錄下一個結構
struct ListNode
{
    int val;
    struct ListNode* next;
};

int getDecimalValue(struct ListNode* head);

int getDecimalValue(struct ListNode* head)
{
    int count = 0, sum = 0;

    struct ListNode* ptr = head;

    while (1) {
        if (ptr == NULL) {
            break;
        }
        count++;
        ptr = ptr->next;
    }

    ptr = head;
    for (int i = count - 1; i >= 0; i--) {
        sum += ptr->val * pow(2, i);
        ptr = ptr->next;
    }

    return sum;
}

// 輸入二進位,並一位一位輸入,採用遞迴方式輸入,資料存放採用linked list
// 例如數字6的二進位是 1 1 0
// 表示有三個結構,第一個結構的val存1,第一個結構的next紀錄第二個結構,第二個結構的val存1,第二個結構的next紀錄第三個結構,第三個結構的val存0,第三個結構的next紀錄NULL
void Construct(struct ListNode* node, int length, int num)
{
    int a;
    scanf("%d", &a);
    node->val = a;

    if (num >= length)
    {
        node->next = NULL;
        return;
    }
    node->next = (struct ListNode*)malloc(sizeof(struct ListNode));
    num++;
    Construct(node->next, length, num);
}

int main()
{
    struct ListNode* head;
    int n, a, length;
    head = (struct ListNode*)malloc(sizeof(struct ListNode));
    scanf("%d", &n);
    while (n--)
    {
        scanf("%d", &length);
        Construct(head, length, 1);
        int ans = getDecimalValue(head);
        printf("%d\n", ans);
    }
}
Editor is loading...