无头结点的单链表,只带尾插函数。
#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 人评分
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:375 |
C语言程序设计教程(第三版)课后习题7.5 (C语言代码)浏览:853 |
【计算直线的交点数】 (C语言代码)浏览:1445 |
蚂蚁感冒 (C语言代码)浏览:1324 |
1124题解浏览:592 |
剪刀石头布 (C++代码)浏览:1706 |
Tom数 (C语言代码)浏览:553 |
敲七 (C++代码)浏览:1052 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:561 |
多组数据新方法浏览:355 |