华为OD统一考试B卷 | 200分】阿里巴巴找黄金宝箱(IV

这种题目还收费的人真恶心

example: input  :3,2,4,5,-1

               output:4,4,5,-1,3

比3大的第一个数是4

比2大的第一个数是4

比4大的第一个数是5

比5大的第一个数是没有,故为-1

比-1大的第一个数是3(从头开始)

#include <stdio.h>

#include<stdlib.h>

#include<string.h>

typedef struct node

{

    node* next;

    int value;

};

void creatCircleLinkedList(node* root,int value)

{

    node* p = root;

    while (p->next!=NULL)

    {

        p = p->next;

    }

    p->value = value;

    p->next = (node*)malloc(sizeof(node));

    p->next->next = NULL;

}

int main() {

    int A[5] = { 3,2,4,5,-1 };

    int count = sizeof(A) / sizeof(A[0]);

    node root;

    root.next = NULL;

    for (size_t i = 0; i < count; i++)

    {

        creatCircleLinkedList(&root, A[i]);

    }

    node* end=&root;

    while (end->next->next!= NULL)

    {

        end = end->next;

    }

    end->next = &root;

     //需要遍历多少个数的next

    for (size_t i = 0; i < count; i++)

    {

        //每个数的下表是多少

        node* t = &root;

        for (size_t j = 0; j < i; j++)

        {

            t = t->next;

        }

        int currentNodeValue = t->value;

         //从该下标再继续遍历多少个数

        int k = 0;

        for( k = 0; k < count-1; k++)

        {

            t = t->next;

            if (currentNodeValue < t->value)

            {

                printf("%d,", t->value);

                break;

            }

        }

        if (k == count - 1) 

        {

            printf("-1,");

        }

    }

    return 0;

}

全部评论

相关推荐

3 2 评论
分享
牛客网
牛客企业服务