【思路】:针对测试数据的直接加就行。不过我还是想试一下链表,数据结构学了一个年后确实写起来有点麻烦。。唉。
注意:节点链接的写法。以及在遍历的时候p->p,掠过头节点。
【AC代码】:
#include <iostream> #include <algorithm> #include <string> #include <cstdio> #include <cstring> using namespace std; #define MAX 10 struct Node { double a; double b; Node *next; }; int main() { freopen("in.txt", "r", stdin); int i = 0; Node *p; Node *q = p; p = q = (Node*)malloc(sizeof(Node)); for (i = 0; i < MAX; i++) { Node *node; node = (Node*)malloc(sizeof(Node)); cin >> node->a >> node->b; q->next = node; q = node; } q->next = NULL; double m = 0, n = 0; for (p = p->next; p != NULL; p = p->next) { m = p->a + m; n = p->b + n; } cout << m << "+" << n << "i"; return 0; }
原文地址:http://blog.csdn.net/weijj6608/article/details/44683519