无头结点的单链表,只带尾插函数。
#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语言程序设计教程(第三版)课后习题11.5 (C语言代码)浏览:629 |
上车人数 (C语言代码)浏览:781 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:449 |
WU-输出正反三角形 (C++代码)浏览:1021 |
【绝对值排序】 (C语言代码)浏览:824 |
有关字符,字符串的输入输出函数说明浏览:479 |
C语言训练-亲密数 (C语言描述,反正怎么都能对)浏览:2169 |
1134题解(求分析)浏览:729 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:449 |
Manchester-弟弟的作业浏览:4008 |