Homework - Recursion

2018/5/22

POJ 2106  Boolean Expressions

#include <iostream> 
#include <cstring> 
#include <cstdio> 
using namespace std;
char s[101];
int pos=0; 
bool value();
bool read(){
	bool val=value(); //得到目前这一小部分的值
	while(s[pos]=='&'||s[pos]=='|'){
		char op=s[pos]; ++pos;
		if(op=='&') val=val&value();
		else val=val|value();
	}
	return val;
} 
bool value(){   
//本函数运行完后,pos位于下一个 & 或 | 处 
	bool val=true;
	switch(s[pos]){
		case('V'):++pos; break;
		case('F'):++pos; val=false; break;
		case('!'):++pos; val=!value(); break;
		case('('):++pos; val=read(); ++pos; break;
	}
	return val;
}

int main(){
	int n=0; char tmp[1000000];
	while(gets(tmp)){
		int len=0;
		for(int i=0;tmp[i]!='\0';++i){
			if(tmp[i]!=' ') s[len++]=tmp[i];
		}
		s[len]='\0';
		++n; pos=0;
		printf("Expression %d: %s\n",n,read()?"V":"F");
	}
	return 0;
}

其他思路:

①双栈法:stack 在表达式题目中很有用,有助于判断运算符优先级。

我们使用 value 和 operator 两个栈。分别读取,如果遇到的运算符优先级低于已有,计算已有,清理栈。注意()的清理有微小不同。

②一般递归法:跟自己写的差不多

③LL1文法:将表达式分成基础模块,……,确实很简洁




POJ 1057  FILE MAPPING

这是自己写的,有点受到上一题的影响,比如全局 pos 的使用;感觉写得比较繁杂。

#include <iostream> 
#include <cstring> 
#include <algorithm>
using namespace std;
string s[101],head="|     ";
int pos=0, l; 
string file[31][31]; int FileNumber[31]={};
void MyPrint(int i){ //i级目录
	string myhead; for(int j=0;j<i;++j) myhead+=head; 
	if(s[pos][0]=='d'){
		printf("%s%s\n",(myhead+head).c_str(),s[pos].c_str());
		++pos; MyPrint(i+1);
	}
	else if(s[pos][0]=='f') {
		file[i][FileNumber[i]++]=s[pos];
		++pos; MyPrint(i);
	}
	else if(s[pos][0]==']') {
		if(FileNumber[i]){
			sort(file[i],file[i]+FileNumber[i]);
			for(int j=0;j<FileNumber[i];++j) printf("%s%s\n",myhead.c_str(),file[i][j].c_str());
			FileNumber[i]=0;
		}
		++pos; MyPrint(i-1);
	}
	else
	if(FileNumber[0]){
			sort(file[0],file[0]+FileNumber[0]);
			for(int j=0;j<FileNumber[0];++j) printf("%s%s\n",myhead.c_str(),file[0][j].c_str());
		}
	return;
}

int main(){
	int t=0; string judge;
	while(cin>>judge&&judge!="#"){
		if(t) printf("\n");
		s[0]=judge;
		for(l=1;;++l){
			cin>>s[l];
			if(s[l]=="*") break;
		} 
		++t;
		printf("DATA SET %d:\nROOT\n",t);
		memset(FileNumber,0,31); pos=0;
		MyPrint(0);
	}
	return 0;
}


这是别人写的,思路迥异,参考一下

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int End=0;

struct Node{
  vector<string> file;
  vector<Node> dir;
  string name;
};

void fileInput(Node &node){
  string tmp;
  while(1){
    cin>>tmp;
    if(tmp[0]=='#') End=1;
    else if(tmp[0]=='f'){
      node.file.push_back(tmp);
    }
    else if(tmp[0]=='d'){
      Node n;
      n.name=tmp;
      fileInput(n);
      node.dir.push_back(n);
    }
    else break;
  }
  sort(node.file.begin(),node.file.end());
  return;
}

void fileOutput(Node &node,int n){
  int i,j;
  for(i=0;i<n;i++)
     cout<<"|     ";
  cout<<node.name<<endl;
  for(i=0;i<node.dir.size();i++){
    fileOutput(node.dir[i], n+1);
  }
  for(j=0;j<node.file.size();j++){
    for(i=0;i<n;i++) cout<<"|     ";
    cout<<node.file[j]<<endl;
  }
  return;
}

int  main() {
  int Case=1;
  while(Case){
    Node root;
    root.name="ROOT";
    fileInput(root);
    if(End) break;
    cout<<"DATA SET "<<Case<<":"<<endl;
    fileOutput(root,0);
    cout<<endl;
    Case++;
  }
  return 0;
}

POJ 1941  The Sierpinski Fractal

看似复杂,其实很简单

①自小至大

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

string s[2]={" /\\","/__\\"},out[1025];

void f(int depth,int k,int n){
	if(depth>n) return;
	if(depth==1) {out[0]=s[0],out[1]=s[1];}
	else{
		int i;
		for(i=k;i<2*k;++i) {
			int len=out[i-k].size();
			out[i]=out[i-k]+string(2*k-len,' ')+out[i-k];
		}
		for(i=0;i<k;++i) out[i]=string(k,' ')+out[i];
	}
	f(depth+1,2*k,n);
}

int main(){
	int n,flag=0;
	while(cin>>n&&n){
		f(1,1,n);
		int k=(1<<n); //这样比用pow快得多
        	if(flag) printf("\n");
		for(int i=0;i<k;++i) printf("%s\n",out[i].c_str()); 
		flag=1;
	}
	return 0;
}


②由大及小

先画出最大的三角形,在其中画倒三角

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值