wu


私信TA

用户名:cncfvc

访问量:215393

签 名:

读研狗没有时间刷题了~~

等  级
排  名 2
经  验 36026
参赛次数 8
文章发表 265
年  龄 25
在职情况 学生
学  校 电子科技大学
专  业 通信工程

  自我简介:

写代码 真好玩 ~

解题思路:


这题做起来也可以很简单 ,但是我选择用链表来实现


注意事项:

注意链表的实现过程


参考代码:

#include<iostream>#include<cstdio>
#include<algorithm>
using namespace std;
struct LinkList
{
	int z;
	int fu;
	LinkList *next;
};
LinkList *BuildList(int n)
{
	LinkList *head=NULL;
	int zheng,fushu;
    while(n--)
    {
	   cin>>zheng>>fushu;
	   LinkList *new_node=(LinkList *)malloc(sizeof(LinkList));
		if(new_node==NULL) 
		{
			fprintf(stderr,"malloc failed");
			return head;
		}
		new_node->z=zheng;
		new_node->fu=fushu;
		LinkList *p;
	if(head==NULL)
	{
		new_node->next=NULL;
		head=new_node;
		p=head;
	}
	else 
	{

		p->next=new_node;
		new_node->next=NULL;
		p=new_node;
	} 
  }
	return head;
}
void CountList(LinkList *head)
{
	LinkList *p=head;
	int zheng=0,fu=0;
	while(p)
	{
		zheng+=p->z;
		fu+=p->fu; 
		p=p->next;
	}
    cout<<zheng<<"+"<<fu<<"i"<<endl;
}
int main()
{
	int n;
	cin>>n;
	LinkList *head=BuildList(n);
	CountList(head);
	return 0;
}


 

0.0分

6 人评分

  评论区