链表数据求和操作
(本题很水)
题目描述
读入10个复数,建立对应链表,然后求所有复数的和。
输入格式
无
输出格式
无
样例输入
1 2
1 3
4 5
2 3
3 1
2 1
4 2
2 2
3 3
1 1
样例输出
23+23i
#include<bits/stdc++.h>
using namespace std;
//双向循环链表
struct node{
int a;
int b;
node*next;
};
//遍历链表
void print(node * first){
node*p=first->next;
int sum_a=0,sum_b=0;
while(p!=NULL){
sum_a+=p->a;
sum_b+=p->b;
p=p->next;
}
cout<<sum_a<<"+"<<sum_b<<"i";
}
int main(){
int a,b;
int n=10;
node * first=new node;
first->next=NULL;
node * p=first;
node * s;
while(n--){
cin>>a>>b;
s=new node;
s->a=a;
s->b=b;
s->next=NULL;
p->next=s;
p=p->next;
}
print(first);
return 0;
}