编程练习20180916_1给出树的双亲表示法,求该树的高度;2字符流:处理不定长输入的情况_STL排序算法

1.给出树的双亲表示法,求该树的高度

有一棵合法的树(不一定是二叉树),节点用数字表示,现给出所有节点的父子关系,求该树的高度!

输入包含若干行,每行2个数字,中间以空格隔开,第一个数字表示父亲节点,第二个数字表示孩子节点的编号

0 1

0 2

1 3

1 4

第一种解法:采用树的双亲表示法,对每一个节点,顺每一个节点向上递归求其父亲节点直到树根,然后每一步计数,最后取计数最大的的值为树的高度。

树的双亲表示法:以一组连续的空间存储树的结点,同时在每个结点中附设一个指示器指示其双亲结点在数组中的下标。


#include "stdafx.h"

#include<iostream>
#include<vector>
#include<set>

using namespace std;

struct parentChild{
	int parent;
	int child;
};

void TreeHeightCount(vector<int> & pcTree,int i ,int & count)
{
	count++;
	if(pcTree[i] != -1)
	{
		TreeHeightCount(pcTree, pcTree[i], count);
	}
}

int treeHeight(vector<parentChild>& pc)
{
	set<int> treeNode;
	for (auto ele : pc)
	{
		treeNode.insert(ele.parent);
		treeNode.insert(ele.child);
	}
	int NodeNum = treeNode.size();

	vector<int> pcTree(NodeNum, -1);
	for (auto ele : pc)
	{
		pcTree[ele.child] = ele.parent;
	}

	int TreeHeight = -1;
	int count=0;
	
	for (int i = 0; i < NodeNum; i++)
	{
		count = 0;
		TreeHeightCount(pcTree, i, count);
		if (count > TreeHeight)
		{
			TreeHeight = count;
		}
	}
	return TreeHeight;
}

int main()
{
	vector<parentChild> pc;
	int p, c;
	//while (cin >> p >> c)
	//{
	//	parentChild pcTemp = { p, c };
	//	pc.push_back(pcTemp);
	//}
	//while (scanf("%d %d", &p, &c) != EOF)
	//{
	//	parentChild pcTemp = { p, c };
	//	pc.push_back(pcTemp);
	//}

	for (int i = 0; i < 4; i++)
	{
		scanf("%d %d", &p, &c);
		parentChild pcTemp = { p, c };
		pc.push_back(pcTemp);
	}

	cout << treeHeight(pc);


	system("pause");
	return 0;
}


/*

输入:
0 1
0 2
1 3
1 4

输出:
3

结果对但运行超时了??之前是代码有点小bug,无限递归下去啦!已经改正!
*/

2.1处理不定长字符串的输入:

若干行,每行:学生的姓名 若干门课程的成绩 如:

Bob 95 95
Ally 100 90 95
Li 92 86
Cat 98 99
Wang 99

每行数据结构体的定义:

struct nameAndGrade{
	int id;
	string name;
	vector<int> grade;
	int average;
};

字符串流对象的:

#include<iostream>
#include<string>
#include<vector>
#include<sstream>
	
    vector<nameAndGrade> ngVec;
	string str;
	int grade;
	istringstream record;//字符串输入流对象!

	while (getline(cin, str))
	{
		getline(cin, str);
		nameAndGrade temp;
		temp.id = id++;//这个id其实用不到了,使用了稳定排序算法!
		temp.average = 0;
		record.clear();//重复使用字符串流时,每次都要调用clear
		record.str(str);//将记录绑定到刚读入的行

		record >> temp.name;
		while (record >> grade)//流对象实现string 到int类型的转换!
		{
			temp.grade.push_back(grade);
		}
		ngVec.push_back(temp);
	}

输入一组学生的的姓名和若干门课程的成绩,输出按平均分排序的结果(平均分的计算结果四舍五入取整数),若干两个学生的平均分相等,则按照输入时的先后关系进行排序。

/*
按平均分排序,要求分数相同的按照输入的先后顺序排序(用的了稳定的排序算法stable_sort),而不需要自己去设置第二排序规则,
输入:(输入的行数不定,且没行的成绩数不定,这样的处理应该利用好字符流!!
Bob 95 95
Ally 100 90 95
Li 92 86
Cat 98 99
Wang 99

输出:
Wang 99
Cat 99
Bob 95
Ally 95
Li 89

运行结果:
Bob 95 95
Ally 100 90 95
Li 92 86
Cat 98 99
Wang 99
Wang 99
Cat 98
Bob 95
Ally 95
Li 89
*/


#include<iostream>
#include<string>
#include<vector>
#include<sstream>
#include<numeric>
#include<cmath>
#include<algorithm>

using namespace std;


struct nameAndGrade{
	int id;
	string name;
	vector<int> grade;
	int average;
};

class SortRule{
public:
	bool operator()(nameAndGrade a, nameAndGrade b)
	{
		if (a.average > b.average)
			return a.average > b.average;
		else if (a.average == b.average)
		{
			return a.id < b.id;
		}
	}
};

class SortRuleStable{
public:
	bool operator()(nameAndGrade a, nameAndGrade b)
	{
		return a.average > b.average;
	}
};

int main()
{
	vector<nameAndGrade> ngVec;

	string str;
	int grade;
	istringstream record;//字符串输入流对象!
	int id = 1;
	for (int i = 0; i < 5; i++)
	{
		getline(cin, str);
		nameAndGrade temp;
		temp.id = id++;//这个id其实用不到了,使用了稳定排序算法!
		temp.average = 0;
		record.clear();//重复使用字符串流时,每次都要调用clear
		record.str(str);//将记录绑定到刚读入的行
		
		record >> temp.name;
		while (record >> grade)//流对象实现string 到int类型的转换!
		{
			temp.grade.push_back(grade);
		}
		ngVec.push_back(temp);
	}

	//下面的代码等于白算,因为ngVec的每个元素的平均值根本没变!因为是auto ele ,压根不会影响ngVec的值,真是gg!
	//for (auto ele : ngVec)
	//{
	//	float ave= accumulate(ele.grade.begin(), ele.grade.end(), 0) / ele.grade.size();
	//	//还要进行四舍五入
	//	if (ave - floor(ave)< 0.5)
	//	{
	//		ele.average =(int) floor(ave);
	//	}
	//	else
	//	{
	//		ele.average = (int)ceil(ave);
	//	}
	//}

	int ngVecLen = ngVec.size();
	for (int i = 0; i < ngVecLen; i++)
	{
		float ave = accumulate(ngVec[i].grade.begin(), ngVec[i].grade.end(), 0) / ngVec[i].grade.size();
		//还要进行四舍五入
		if (ave - floor(ave)< 0.5)
		{
			ngVec[i].average = (int)floor(ave);
		}
		else
		{
			ngVec[i].average = (int)ceil(ave);
		}
	}

	//sort(ngVec.begin(), ngVec.end(), SortRule());//使用普通排序算法 ,有bug!
	stable_sort(ngVec.begin(), ngVec.end(), SortRuleStable());

	for (auto ele : ngVec)
	{
		cout << ele.name << " " << ele.average << endl;
	}

	return 0;
}

编程练习题

猜你喜欢

转载自blog.csdn.net/m0_37357063/article/details/82731033
今日推荐