9-7链表数据求和操作

题目描述

算法训练  9-7链表数据求和操作   
时间限制:1.0s     内存限制:512.0MB
     
  读入10个复数,建立对应链表,然后求所有复数的和。
样例输入
1  2
1  3
4  5
2  3
3  1
2  1
4  2
2  2
3  3
1  1
样例输出
23+23i

#include<iostream>
#include<algorithm>
using namespace std;

struct node//创建结点 
{
	int a,b;//分别是复数 
	node *next;
};
node *creat(int array1[],int array2[])//创建链表,其中,array1数组为复数a+bi的a的集合,同理array2数组是b的集合 
{
	node *head,*pre,*p;
	head = new node;//创建头结点 
	head->next = NULL;//头结点指针域为空,数据域没有 
	pre = head;//前驱结点 
	for (int i=0;i<10;i++)
	{
		p = new node;//创建结点p 
		p->a = array1[i];
		p->b = array2[i];
		p->next = NULL;
		pre->next = p;//将当前结点p赋值给前驱结点的指针域 
		pre = p;//前驱结点加1 
	}
	return head;//返回头结点指针 
}

int main()
{
	int x[10],y[10];
	for (int i=0;i<10;i++)
	cin>>x[i]>>y[i];//输入数据 
	
	int ans1=0,ans2=0;//分别是集合a的和 ,集合b的和。 
	node *L = creat(x,y);//创建链表L 
	L = L->next;//L指向第一个结点(不是头结点) 
	while (L!=NULL)//遍历所有结点 
	{
		ans1+=L->a;
		ans2+=L->b;
		L = L->next;
		
	}
	printf("%d+%di
",ans1,ans2);
}
原文地址:https://www.cnblogs.com/Romantic-Chopin/p/12451219.html