京东笔试-交易清单
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了京东笔试-交易清单相关的知识,希望对你有一定的参考价值。
题目要求:
1、使用c语言实现,不能使用c++的stl容器,map、vector等等
2、自己定义数据结构
3、如果需要排序,自己写排序算法
股票交易中,高峰期委托买卖数量是十分大的,尤其在9:30分左右,而证券公司关心的抢手的信息条数会远小于委托买卖数量。实际情况是题目中的n会非常大,而s很小,因此需要考虑算法的时间复杂度。
题目如下:
京东笔试-交易清单(京东2016实习生真题)
题目描述
金融证券行业超好的薪酬待遇,吸引了大批的求职者前往应聘,小东也不例外,准备应聘一家证券公司。面试官为考察她的数据分析、处理和编码能力,为她准备了以下问题。
股票交易中,委托是指股票交易者在证券公司买卖股票。每手委托包括一个委托单号i、价格pi、买入或卖出标记di及交易数量qi。
交易处理中,需要把同类业务(买入或卖出)中相同价格的所有委托合并起来,形成一个清单。清单的第一部分为按价格降序排列的合并后的卖出委托,紧随其后的是按相同顺序排列的买入合并委托。证券公司比较关心的是比较抢手的s条合并委托信息,需要得到买入及卖出最抢手的s条合并委托。对于买入委托,抢手的是指报价高的委托,而卖出委托中报价低的较为抢手。若买或卖的合并委托数小于s条,则全部列入清单中。
现在小东拿到的是n个委托,请你帮忙找出最抢手的s个合并委托。
输入
输入有若干组,每组的第一行为两个正整数n和s(1<=n<=1000,1<=s<=50),分别表示委托数和最抢手的清单数,接下来的n行为具体的委托信息,每行包含3部分,第一部分为一个字母‘B’或‘S’,表示买入或卖出,后两部分为两个整数p和q,表示报价和数量。任何卖出委托的报价都比买入委托的报价高。
输出
输出不超过2s行合并委托清单,格式与输入相同。
样例输入
6 2
B 10 3
S 50 2
S 40 1
S 50 6
B 20 4
B 25 10
样例输出
S 40 1
S 50 8
B 25 10
B 20 4
#include<stdio.h> #include<stdlib.h> #include<assert.h> #include<stdbool.h> typedef struct { unsigned int in; unsigned int out; }InOut; typedef struct { char type; // transaction type, ‘B‘ means buy, ‘S‘ means sell. unsigned int price; unsigned int quantity; } TransData; // DDL - Doubly Linked List. typedef struct _node { TransData data; struct _node *next, *prev; }DDLNode; typedef struct _list { DDLNode *head, *tail; }DDL; DDLNode* CreateDDLNode(TransData *pData); void ShowDDLNode(DDLNode *pNode); void InitDDL(DDL *pddl); bool IsEmptyDDL(DDL *pList); bool IsHeadNode(DDL *pList, DDLNode *pNode); bool IsTailNode(DDL *pList, DDLNode *pNode); // DDDL - Descending DDL. int InsertToDDDL(DDL *pList, TransData *pData); void ShowDDLFromHead(DDL *pList, unsigned int cnt); void ShowDDLFromTail(DDL *pList, unsigned int cnt); #define CHECK_NULL_POINTER(ptr) if(!ptr){ fprintf(stderr, "null pointer at %s, line %d\n", __FUNCTION__, __LINE__); exit(-1); } int main(){ InOut io = {0,0}; DDL sddl, bddl; int i; printf("Please input transactions count (in out):"); scanf(" %u %u", &io.in, &io.out); //printf("io(%u, %u)\n", io.in, io.out); printf("Please input the raw transaction data.\"(‘B‘ price quantity)\" for buying stocks, \"(‘S‘ price quantity)\" for selling stocks.\n"); TransData record[io.in]; for(i = 0; i < io.in; ){ scanf(" %c %u %u", &(record[i].type), &(record[i].price), &(record[i].quantity)); //printf("record[%d]:%c, %u, %u\n", i, record[i].type, record[i].price, record[i].quantity); if(record[i].type != ‘B‘ && record[i].type != ‘S‘){ printf("Wrong transaction type. It must be ‘B‘ or ‘S‘!\n"); continue; } else{ ++i; } } InitDDL(&sddl); InitDDL(&bddl); for(i = 0; i < io.in; ++i){ //printf("\ninsert record[%d]: %c %u %u\n", i, record[i].type, record[i].price, record[i].quantity); if(record[i].type == ‘B‘){ InsertToDDDL(&bddl, &(record[i]) ); } else if(record[i].type == ‘S‘){ InsertToDDDL(&sddl, &(record[i]) ); } } printf("\noutput:\n"); ShowDDLFromTail(&sddl, io.out); ShowDDLFromHead(&bddl, io.out); return 0; } DDLNode* CreateDDLNode(TransData *pData){ DDLNode *pNewNode = (DDLNode *)malloc(sizeof(DDLNode)); CHECK_NULL_POINTER(pNewNode); if(pData){ pNewNode->data.type = pData->type; pNewNode->data.price = pData->price; pNewNode->data.quantity = pData->quantity; } else{ pNewNode->data.type = 0; pNewNode->data.price = 0; pNewNode->data.quantity = 0; } pNewNode->next = NULL; pNewNode->prev = NULL; } void ShowDDLNode(DDLNode *pNode){ if(pNode) printf("%c %u %u %p %p %p\n", pNode->data.type, pNode->data.price, pNode->data.quantity, pNode, pNode->prev, pNode->next); else printf("null node\n"); } void InitDDL(DDL *pList){ CHECK_NULL_POINTER(pList); pList->head = NULL; pList->tail = NULL; } bool IsEmptyDDL(DDL *pList){ CHECK_NULL_POINTER(pList); return pList->head == NULL && pList->tail == NULL; } bool IsHeadNode(DDL *pList, DDLNode *pNode){ CHECK_NULL_POINTER(pList); return pList->head == pNode; } bool IsTailNode(DDL *pList, DDLNode *pNode){ CHECK_NULL_POINTER(pList); return pList->tail == pNode; } /*Function: create a new node to save data, and insert or merge it to descending DDL. *@return value: 0 : success, -1 : fail. */ int InsertToDDDL(DDL *pList, TransData *pData){ CHECK_NULL_POINTER(pList); if(IsEmptyDDL(pList) ){ DDLNode *pNewNode = CreateDDLNode(pData); pList->head = pNewNode; pList->tail = pNewNode; return 0; } DDLNode *pNode = pList->head; while(pNode){ if(pNode->data.price == pData->price){ pNode->data.quantity += pData->quantity; return 0; } if(pNode->data.price < pData->price){ //Insert a new node before pNode. DDLNode *pNewNode = CreateDDLNode(pData); if(IsHeadNode(pList, pNode) ){ pNewNode->next = pNode; pNode->prev = pNewNode; pList->head = pNewNode; return 0; } pNewNode->prev = pNode->prev; pNode->prev->next = pNewNode; pNewNode->next = pNode; pNode->prev = pNewNode; return 0; } if(pNode->data.price > pData->price){ if(IsTailNode(pList, pNode) ){ //Insert a new node after pNode. DDLNode *pNewNode = CreateDDLNode(pData); pNode->next = pNewNode; pNewNode->prev = pNode; pList->tail = pNewNode; return 0; } pNode = pNode->next; } } fprintf(stderr, "Fail to insert node to descending DDL."); return -1; } void ShowDDLFromHead(DDL *pList, unsigned int cnt){ CHECK_NULL_POINTER(pList); unsigned int i=0; DDLNode *pNode = pList->head; for(; i < cnt && pNode; ++i, pNode = pNode->next){ printf("%c %u %u\n", pNode->data.type, pNode->data.price, pNode->data.quantity); } } void ShowDDLFromTail(DDL *pList, unsigned int cnt){ CHECK_NULL_POINTER(pList); unsigned int i=0; DDLNode *pNode = pList->tail; for(; i < cnt && pNode; ++i, pNode = pNode->prev){ printf("%c %u %u\n", pNode->data.type, pNode->data.price, pNode->data.quantity); } }
本文出自 “用C++写诗” 博客,谢绝转载!
以上是关于京东笔试-交易清单的主要内容,如果未能解决你的问题,请参考以下文章
2018京东笔试编程:完善JavaScript,实现删除一行,增加一行,计算总量。不能改动给出的html。