1069745273


私信TA

用户名:1069745273

访问量:5290

签 名:

Just do IT.

等  级
排  名 261
经  验 5704
参赛次数 3
文章发表 166
年  龄 0
在职情况 待业
学  校
专  业 计算机科学与技术

  自我简介:

无头结点的单链表,只带尾插函数。

#include<bits/stdc++.h>
using namespace std;

struct node{
    double shi,xu;
    struct node *Next;
};

typedef struct node Node;

Node *head = NULL;

void tailinsert(double shi,double xu){
    Node *node = (Node*)malloc(sizeof(node));
    Node *temp = head;
    if(head==NULL){
        node->shi = shi;
        node->xu = xu;
        node->Next = NULL;
        head = node;
    }
    else{
        node->shi = shi;
        node->xu = xu;
        node->Next = NULL;
        while(temp->Next!=NULL){
            temp = temp->Next;
        }
        temp->Next = node;
    }
}

void printlist(){
    double sum1(0),sum2(0);
    Node *temp = head;
    while(temp!=NULL){
        sum1 = sum1 + temp->shi;
        sum2 = sum2 + temp->xu;
        temp = temp->Next;
    }
    printf("%.f+%.fi",sum1,sum2);
}

int main() {
    double shi,xu;
    while(cin>>shi>>xu){
        tailinsert(shi,xu);
    }
    printlist();
    return 0;
}

其实这题用 C++ STL 给的 vector、list 等直接就可以做。(下面用的是 list)

#include<bits/stdc++.h>
using namespace std;

int main(){
    list<pair<double,double> > a;
    double shi,xu,sum1(0),sum2(0);
    while(cin>>shi>>xu){
        a.push_back(make_pair(shi,xu));
    }
    for(list<pair<double,double> >::iterator it=a.begin();it!=a.end();it++){
        sum1 = sum1 + (*it).first;
        sum2 = sum2 + (*it).second;
    }
    printf("%.f+%.fi",sum1,sum2);
    return 0;
}


 

0.0分

2 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区