pta数据结构第三天

总结:今天花了3小时写了4题,其中两题还是用了网上简易的方法,递归的思想属实没有很好的掌握。

先记录这两个需要以后再来写一次的题目;

直接用了stl库的函数,没懂递归的思路

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    int n;
    cin>>n;
    string ch;
    for(int i=0;i<n;i++){
        ch+=i+1+'0';//1+'0'='1'
    }
    do{
        cout<<ch<<endl;
    }while(next_permutation(ch.begin(),ch.end()));
    return 0;
}

然后是第二个;

 

 

 

本来是博弈论的知识,但是不会写代码解决这个问题,答案是对的,但是用了观察法,没有很好的理解用数组求解的思路;

#include<iostream>
#include<vector>
#include<iomanip>
#include<cmath>
using namespace std;


int main() {
	int d, p;
	cin >> d >> p;
    if(p==3){
        cout<<d-1;
    }else{
        cout << (d - p / 2 - 1);
    }
	return 0;
}

第三题比较简单,求两个链表的交集

 

直接用了list,没有重新造轮子

#include<iostream>
#include<vector>
#include<list>
using namespace std;

class solution {
private:
	list<int>ls;
	list<int>ls2;
	list<int>ls3;
public:
	solution() {
		int m;
		while (cin >> m && m != -1) {
			ls.push_back(m);
		}
		while (cin >> m && m != -1) {
			ls2.push_back(m);
		}
	}
	void merge() {
		list<int>::iterator it1 = ls.begin();
		list<int>::iterator it2 = ls2.begin();
		while (it1 != ls.end() && it2 != ls2.end()) {
			if (*it1 == *it2) {
				ls3.push_back(*it1);
				++it1;
				++it2;
			}
			else if (*it1 < *it2) {
				++it1;
			}
			else {
				++it2;
			}
		}
		if (ls3.empty()) {
			cout << "NULL";
		}
		else {
			for (list<int>::iterator i = ls3.begin(); i != ls3.end(); i++) {
				if (i == ls3.begin()) {
					cout << *i;
				}
				else {
					cout << ' ' << *i;
				}
			}
		}
	}
};
int main() {
	solution s;
	s.merge();
	return  0;
}

最后是24点,暴力枚举解决,第一次接触这种题目,写了一个来小时

 

#include<iostream>
#include<vector>
#include<iomanip>
#include<cmath>
using namespace std;

class solution {
private:
	vector<int>v;//输入数字
	vector<char>c = { '+','-','*','/' };//加减乘除
	vector<int>t;//储存乱序数字
	vector<char>t2;//储存乱序的三个符号
public:
	solution() {
		int m;
		for (int i = 0; i < 4; i++) {
			cin >> m;
			v.push_back(m);
		}
	}
	void show();
	bool cal();
	double result(double a,double b,char op);//注意是double
	double model1();
	double model2();
	double model3();
	double model4();
	double model5();
};

int main() {
	solution s;
	s.show();
	return 0;
}

double solution::model1() {
	//123
	double x = result(t[0], t[1], t2[0]);
	double y = result(x, t[2], t2[1]);
	double sum = result(y, t[3], t2[2]);
	return sum;
}
double solution::model2() {
	//132
	double x = result(t[0], t[1], t2[0]);
	double y = result(t[2], t[3], t2[2]);
	double sum = result(x, y, t2[1]);
	return sum;
}
double solution::model3() {
	//213
	double x = result(t[1], t[2], t2[1]);
	double y = result(t[0], x, t2[0]);
	double sum = result(y, t[3], t2[2]);
	return sum;
}
double solution::model4() {
	//231
	double x = result(t[1], t[2], t2[1]);
	double y = result(x, t[3], t2[2]);
	double sum = result(t[0], y, t2[0]);
	return sum;
}
double solution::model5() {
	//321
	double x = result(t[2], t[3], t2[2]);
	double y = result(t[1], x, t2[1]);
	double sum = result(t[0], y, t2[0]);
	return sum;
}
double solution::result(double a, double b, char op) {
	double ret = 0;
	switch (op)
	{
	case '+':
		ret = a + b;
		break;
	case '-':
		ret = a - b;
		break;
	case '*':
		ret = a * b;
		break;
	case '/':
		if (b != 0) {
			ret = 1.0 * a / b;
		}
		break;
	default:
		break;
	}
	return ret;
}
bool solution::cal() {//遍历所有可能的符号和对应的5种顺序
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			for (int k = 0; k < 4; k++) {
				t2.push_back(c[i]);
				t2.push_back(c[j]);
				t2.push_back(c[k]);
				if (model1() == 24) {
					cout << "((" << t[0] << c[i] << t[1] <<")" << c[j] << t[2] <<")" << c[k] << t[3];
					return 1;
				}
				else if (model2() == 24) {
					cout << "(" << t[0] << c[i] << t[1] << ")" << c[j] <<"(" << t[2] << c[k] << t[3]<<")";
					return 1;
				}
				else if (model3() == 24) {
					cout << "(" << t[0] << c[i] <<"(" << t[1]  << c[j] << t[2] << "))" << c[k] << t[3];
					return 1;
				}
				else if (model4() == 24) {
					cout  << t[0] << c[i] << "((" << t[1]  << c[j] << t[2] << ")" << c[k] << t[3] << ")";
					return 1;
				}
				else if (model5() == 24) {
					cout  << t[0] << c[i] <<"(" << t[1]  << c[j] << "(" << t[2]  << c[k] << t[3] << "))";
					return 1;
				}
				t2.clear();
			}
		}
	}
	return 0;
}
void solution::show() {//遍历所有的数字可能
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			if (i == j) {
				continue;
			}
			for (int k = 0; k < 4; k++) {
				if (k == i || k == j) {
					continue;
				}
				for (int l = 0; l < 4; l++) {
					if (l == i || l == j || l == k) {
						continue;
					}
					t.push_back(v[i]);
					t.push_back(v[j]);
					t.push_back(v[k]);
					t.push_back(v[l]);
					int ret = cal();
					t.clear();
					if (ret) {
						return;
					}
				}
			}
		}
	}
	cout << -1;
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值