首页 > 其他分享 >北林OJ204、211、212、214

北林OJ204、211、212、214

时间:2023-09-26 21:32:38浏览次数:57  
标签:214 212 LNode int pbook pos 北林 length include

204

#include<iostream>
#include <string>
#include<iomanip>
#define Maxsize 100
using namespace std;
struct Book
{
  string no;
  string name;
 double price;
};
 
struct sqlist
{
  Book elem[Maxsize];
  int length;
};
//初始化
void Init(sqlist & L)
{
  L.length=0;
}
//输入
void input(sqlist & L)
{
  while(1)
  {
	  cin>>L.elem[L.length].no>>L.elem[L.length].name>>L.elem[L.length].price;
	  if(L.elem[L.length].no=="0"&&L.elem[L.length].name=="0"&&L.elem[L.length].price==0)
		  break;
		L.length++;
	}
}
 
//输出
void output(const sqlist &L)
{
	cout<<L.length<<endl;
	for(int i=0;i<L.length;i++)
	{
		cout<<L.elem[i].no<<" "<<L.elem[i].name<<" "<<fixed<<setprecision(2)<<L.elem[i].price<<endl;
	}
 
}
int main()
{
	sqlist L1;
	Init(L1);
	input( L1);
	output(L1);
 
	return 0;
}

211

#include <iostream>
#include <string>
#include <cassert>
#include<iomanip>
const int Maxsize = 1000;
using namespace std;
//图书
struct Book
{
	string no;
	string name;
	double price;
};
//顺序表
struct sqlist
{
	Book* pbook;
	int length;
};
 
//初始化
int Initsqlist(sqlist& L)
{
	//L.pbook = (Book *)malloc(sizeof(Book)*Maxsize);//开辟空间
	L.pbook = new Book[Maxsize];
	/*if (!L.pbook)
		return 0;*/
	assert(L.pbook);
	L.length = 0;
	return 1;
}
//插入
int Insertsqlist(sqlist& L, int pos, const Book &e)
{
	//判断是否合理
	assert(L.pbook);
	if (pos<1 || pos>=L.length+1)
		return 0;
	if (L.length == Maxsize)
		return 0;
 
	//尾插
	else if (pos == L.length)
	{
		L.pbook[L.length] = e;
		L.length++;
		return 1;
	}
	else 
	{
		//移动:从最后一个元素向后移动
		for (int i = L.length - 1; i >= pos - 1; i--)
		{
			L.pbook[i + 1] = L.pbook[i];
		}
		//插入
		L.pbook[pos - 1] = e;
		L.length++;
		return 1;
	}
 
}
void  Printsqlist(const sqlist& L)
{
	assert(L.pbook);
	for (int i = 0; i <= L.length - 1; i++)
		cout << L.pbook[i].no << " " << L.pbook[i].name << " " << 
		fixed << setprecision(2) << L.pbook[i].price<< endl;
}
int main()
{
	int n=0;
	Book e;
	sqlist L;
	//初始化
	Initsqlist(L);
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> e.no >> e.name >> e.price;
		L.pbook[i] = e;
		L.length++;
		/*Insertsqlist(L, i + 1, e);*/
	}
	int pos = 0;
	cin >> pos;
	//输入新书的信息
	cin >> e.no >> e.name >>  e.price ;
	int flag=Insertsqlist(L, pos, e);
	if (0 == flag)
		cout << "Sorry,the position to be inserted is invalid!" << endl;
	else
		Printsqlist(L);
 
	return 0;
}

212

	#include <iostream>
#include <string>
#include <cassert>
#include<iomanip>
const int Maxsize = 1000;
using namespace std;
//图书
struct Book
{
	string no;
	string name;
	double price;
};
//顺序表
struct sqlist
{
	Book* pbook;
	int length;
};
 
//初始化
int Initsqlist(sqlist& L)
{
	//L.pbook = (Book *)malloc(sizeof(Book)*Maxsize);//开辟空间
	L.pbook = new Book[Maxsize];
	/*if (!L.pbook)
		return 0;*/
	assert(L.pbook);
	L.length = 0;
	return 1;
}
//插入
int Insertsqlist(sqlist& L, int pos, const Book &e)
{
	//判断是否合理
	assert(L.pbook);
	if (pos<1 || pos>=L.length+1)
		return 0;
	if (L.length == Maxsize)
		return 0;
 
	//尾插
	else if (pos == L.length)
	{
		L.pbook[L.length] = e;
		L.length++;
		return 1;
	}
	else 
	{
		//移动:从最后一个元素向后移动
		for (int i = L.length - 1; i >= pos - 1; i--)
		{
			L.pbook[i + 1] = L.pbook[i];
		}
		//插入
		L.pbook[pos - 1] = e;
		L.length++;
		return 1;
	}
 
}
void  Printsqlist(const sqlist& L)
{
	assert(L.pbook);
	for (int i = 0; i <= L.length - 1; i++)
		cout << L.pbook[i].no << " " << L.pbook[i].name << " " << 
		fixed << setprecision(2) << L.pbook[i].price<< endl;
}
int deletesqlist(sqlist& L, int pos)
{
	/*assert(L.pbook);*/
	if (!L.pbook)
		return 0;
	//判断位置是否合理
	if (pos<1 || pos>L.length)
		return 0;
	//移动元素
	for (int i = pos; i <L.length; i++)
		L.pbook[i - 1] = L.pbook[i];
	L.length--;
	return 1;
}
int main()
{
	int n=0;
	Book e;
	sqlist L;
	//初始化
	Initsqlist(L);
	//图书总数
	cin >> n;
	//图书入库
	for (int i = 0; i < n; i++)
	{
		cin >> e.no >> e.name >> e.price;
		L.pbook[i] = e;
		L.length++;
		/*Insertsqlist(L, i + 1, e);*/
	}
	int pos = 0;
	cin >> pos;
	//删除
	int flag=deletesqlist(L, pos);
 
	////输入新书的信息
	//cin >> e.no >> e.name >>  e.price ;
	//int flag=Insertsqlist(L, pos, e);
	if (0 == flag)
		cout << "Sorry,the position to be deleted is invalid!" << endl;
	else
		Printsqlist(L);
 
	return 0;
}

214

	#include <iostream>
#include<cassert>
#include <iostream>
#include <string>
#include <cassert>
#include<iomanip>
 
using namespace std;
//图书
typedef struct Book
{
	string no;
	string name;
	double price;
}ElemType;
//链表
struct LNode
{
	//数据域
	ElemType data;
	LNode* next;
};
//初始化
void InitList(LNode *& L)
{
	//创建头节点
	L= new LNode;
	L->next = NULL;//头结点的指针域置空
}
//尾插
/*int PushbackList(LNode &L,ElemType e)
{
	//尾指针
	LNode * tail=L
	while(1)
	{
		//新节点
	LNode *r=new LNode;
	cin>>e.no>>e.name>>e.price;
	r->data=e;
 
 
	}
}*/
 
//遍历--计算长度
int TraversalList(LNode *& L)
{
	LNode* tmp = L->next;
	int count = 0;
	while (tmp)
	{
		tmp = tmp->next;
		count++;
	}
	return count;
}
//尾插
int PushbackList(LNode*& L, ElemType e)
{
	LNode* tmp = L;//error
	int count = 0;
	while (tmp->next)//tmp指向最后一个节点
	{
		tmp = tmp->next;
	}
	LNode *pnode=new LNode;
	pnode->data = e;
	pnode->next = NULL;
	//插入
	tmp->next = pnode;
	return 1;
}
//打印链表
void PrintList(LNode*& L)
{
	LNode* tmp = L->next;
	while (tmp)
	{
		//打印
		cout << tmp->data.no <<" " << tmp->data.name <<" " << fixed << setprecision(2) << tmp->data.price << endl;
		tmp = tmp->next;
	}
 
}
int main()
{
	//链表
	LNode* L;
	//初始化
	InitList(L);
	ElemType e;
	//读入图书信息,创建链表
	while (1)
	{
		cin >> e.no >> e.name >> e.price;
		if (e.no == "0" && e.name == "0" && e.price == 0)
			break;
		PushbackList(L, e);
	}
	//统计图书个数
	int length = TraversalList(L);
	//打印个数及图书信息
	cout << length << endl;
	PrintList(L);
 
	return 0;
}

标签:214,212,LNode,int,pbook,pos,北林,length,include
From: https://blog.51cto.com/u_15805257/7614780

相关文章

  • 102102128汪伟杰
    作业①要求:用requests和BeautifulSoup库方法定向爬取给定网址(http://www.shanghairanking.cn/rankings/bcur/2020)的数据,屏幕打印爬取的大学排名信息。输出信息:排名学校名称省市学校类型总分1清华大学北京综合852.52............代码importreques......
  • Spring Boot 目录遍历--表达式注入--代码执行--(CVE-2021-21234)&&(CVE-2022-22963)&&
    SpringBoot目录遍历--表达式注入--代码执行--(CVE-2021-21234)&&(CVE-2022-22963)&&(CVE-2022-22947)&&(CVE-2022-2296)SpringBoot目录遍历(CVE-2021-21234)漏洞简介spring-boot-actuator-logview是一个简单的日志文件查看器作为SpringBoot执行器端点,在0.2.13版本之前存......
  • abc212 差F
    abc212差FA-Alloy(5')B-WeakPassword(64')C-MinDifference(246')两个数组中各取一数,最小化差的绝对值排序,各用一个指针D-QueryingMultiset(775')三种操作:加入一个数x、所有数+x、输出并删除最小数小根堆,维护全局addE-SafetyJourney(1410')n个点的完......
  • P3214 [HNOI2011] 卡农
    原题首先我们先简化一下题意。为什么呢?因为这个题如果不简化题意是不太好做的我们考虑用二进制表示集合,这样题意为:有\(2^n-1\)个数,我们要从中选一个大小为\(m\)的无序子集,满足以下条件:集合中所有数的异或和为\(0\)集合中元素不可重复首先无序子集是吓人的,因为我们可......
  • 实验1实验2_212106091_林佳铭
    实验1基础代码实验1进阶代码pingall截图同一交换机内部的主机间连通性及通信带宽测试(h1h2)相同汇聚交换机下不同机架的主机间测试(h1h3)相同核心交换机不同汇聚交换机下的主机间测试(h1h5)实验2基础Pingall命令截图Ovs流表的命令结果截图H1pingH3抓包H2pin......
  • 212 模拟科三
    下午去,晚上七点多回来模拟紧张,发挥不好,一次OK,一次不行(无缘无故变更车道没打转向灯),要记得灯光复位,不要发懵。记得考试流程,这个项目是什么,下个项目又是什么。不要紧张,明天考试好好发挥,一次过!......
  • 【9月摸鱼计划】英特尔酷睿i3-2120与显卡
    酷睿i32120配什么显卡酷睿i3-2120是一款比较老旧的处理器,属于Intel第二代酷睿系列,其使用的主板接口为LGA1155,最大支持DDR3内存。根据其性能水平,推荐搭配中低档次的显卡,以达到较好的性价比。以下是几款适合搭配酷睿i3-2120的显卡:GTX1050Ti:这款显卡是中低端显卡中的佼佼者,搭配i3-212......
  • 物通博联工业智能网关实现环保HJ212协议对接到水污染监控平台
    环保HJ212协议是一种用于环保数据上报的行业标准协议。它定义了数据格式、数据传输方式等规范,以便在全国范围内实现统一的环境监测和管理。HJ212协议上报的过程包括数据采集、数据封装、网络传输、服务器接收等步骤。 对此,物通博联推出的工业智能网关可以实现HJ212对接到水污染监......
  • NC26212 小石的签到题
    题目链接题目题目描述小石和小阳玩游戏,一共有\(n\)个数,分别为\(1\simn\)。两人轮流取数,小石先手。对于每轮取数,都必须选择剩下数中的任意一个数\(x\),同时还要取走\(x,\left\lfloor\frac{x}{2}\right\rfloor,\left\lfloor\frac{\left\lfloor\frac{x}{2}\right......
  • 北大ACM poj2141 Message Decowding
    MessageDecowdingTimeLimit:1000MS MemoryLimit:65536KTotalSubmissions:10326 Accepted:5672DescriptionThecowsarethrilledbecausethey'vejustlearnedaboutencryptingmessages.Theythinktheywillbeabletousesecretmessagestoplot......