poj 1057-File mapping

这道题基本思路其实很简单,就是个堆栈的问题,用函数递归做也能得到很好的效果。唯一比较烦的就是每个记录开头都有个自编号,偷懒干脆就在递归函数里多加了个参数,哈哈哈。

跑了一遍目测没什么问题提交之,结果来了个PE,我晕!重新审题后,发现尼玛indent是5个空格而不是一个tab,默默画圈圈……

改了之后直接AC,下面是代码,没怎么考虑优化。


#include <iostream>
#include <string.h>
using namespace std;

typedef struct n{
	char temp[64];
	struct n* next;
} node,*pnode;

void insert (pnode p1,pnode p2)
{
	pnode ptemp1 = p1;
	while (ptemp1->next!=NULL)
	{
		if (strcmp(ptemp1->next->temp,p2->temp)>0)
		{
			p2->next=ptemp1->next;
			ptemp1->next=p2;
			return;
		}
		ptemp1=ptemp1->next;
	}
	p2->next=ptemp1->next;
	ptemp1->next=p2;
	return;
}

int calc (unsigned char depth, unsigned char nset)
{
	pnode p=NULL,p1=new node;
	p1->next=NULL;
	while (1)
	{
		p = new node;
		cin>>p->temp;
		if (p->temp[0]=='#')
		{
			return 0;
		}
		if (depth == 1 && p1->next == NULL)
		{
			cout<<"DATA SET "<<(unsigned int)nset<<':'<<endl;
			cout<<"ROOT"<<endl;
		}
		if (p->temp[0]=='*' || p->temp[0]==']')
		{
			while (p1->next != NULL)
			{
				for (int i = 0;i<depth-1;i++)
				{
					cout<<"|     ";
				}
				cout<<p1->next->temp<<endl;
				p1 = p1->next;
			}
			if (depth == 1)
			{
				cout<<endl;
			}
			break;
		}
		else
		{
			if (p->temp[0]=='f')
			{
				insert(p1,p);
			}
			else if (p->temp[0]=='d')
			{
				for (int i = 0;i<depth;i++)
				{
					cout<<"|     ";
				}
				cout<<p->temp<<endl;
				calc(depth+1,nset);
			}
		}
	}
	return 1;
}

int main ()
{
	int ret= -1;
	unsigned char nset = 1;
	while (ret != 0)
	{
		ret = calc(1, nset);
		nset++;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值