题解 | #复数集合#链表做法

复数集合

http://www.nowcoder.com/practice/abdd24fa839c414a9b83aa9c4ecd05cc

using namespace std;

struct virtual_num{
    int real_model;
    int virtual_model;
    double model;
    virtual_num* next;
    virtual_num(int r,int v):real_model(r),virtual_model(v),model(pow((pow(r,2) + pow(v,2)),0.5)),next(NULL){};
};

int LinkedListSize(virtual_num* root){
    /*
     * 输出以root为头结点的链表的size。
     */
    int nodeNum = 0;
    while(root->next!=NULL){
        nodeNum++;
        root = root->next;
    }
    return nodeNum;
}

int main() {
    int num;
    while(cin>>num){
        virtual_num* root = new virtual_num(-1,-1);   //链表根节点
        virtual_num* tmp = root;
        virtual_num* addNode;
        string choice;
        int real_num,virtual_num;

        for(int i=0;i<num;i++){
            tmp = root;
            cin>>choice;
            if(choice == "Pop"){
                if(LinkedListSize(root) == 0){
                    cout<<"empty"<<endl;
                }else{
                    cout<<root->next->real_model<<"+i"<<root->next->virtual_model<<endl;
                    root->next = root->next->next;
                    cout<<"SIZE = "<<LinkedListSize(root)<<endl;
                }
            }else if(choice == "Insert"){
                cin>>real_num;
                getchar();
                getchar();
                cin>>virtual_num;
                while(true){
                    if(tmp->next == NULL){  //链表长度为0,直接添加
                        tmp->next = new struct virtual_num(real_num,virtual_num);
                        break;
                    }else{
                        if(tmp->next->model>pow((pow(real_num,2) + pow(virtual_num,2)),0.5)){
                            tmp = tmp->next;
                        }else if(tmp->next->model == pow((pow(real_num,2)+ pow(virtual_num,2)),0.5)){
                            //插入
                            addNode = new struct virtual_num(real_num, virtual_num);
                            addNode->next = tmp->next;
                            tmp->next = addNode;
                            if(addNode->next->virtual_model < addNode->virtual_model){
                                addNode->virtual_model = addNode->next->virtual_model;
                                addNode->next->virtual_model = virtual_num;
                                addNode->real_model = addNode->next->real_model;
                                addNode->next->real_model = real_num;
                            }
                            break;
                        }else{
                            addNode = new struct virtual_num(real_num, virtual_num);
                            addNode->next = tmp->next;
                            tmp->next = addNode;
                            break;
                        }
                    }
                }
                cout<<"SIZE = "<<LinkedListSize(root)<<endl;
            }
        }
        root = NULL;
    }
    return 0;
}
全部评论

相关推荐

程序员鼠鼠_春招版:都很烂大街,rpc也基本没人问,考研吧,不然就包装一段实习再去
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务