qhy数据结构期末复习题

configuration:

#include <iostream>
#include <algorithm>
#include <vector>
#include <cctype>
#include <cstdlib>
#include <list>
#include <stack>
using namespace std;


1001. The first quartile


Description

In descriptive statistics, the quartiles (四分位数) of a ranked set of data values are the three points that divide the data set into four equal groups, each group comprising a quarter of the data. A quartile is a type of quantile. The first quartile (Q1)(第一四分位数) is defined as the middle number between the smallest number and the median (中位数) of the data set. The second quartile (Q2) is the median of the data. The third quartile (Q3) is the middle value between the median and the highest value of the data set. For example, the ordered data set: (6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49), the first quartile Q= 15, the median Q= 40, and Q=43.

Now your task is to compute the first quartile and its location for a given data set. The input data set is a vector v, which is not necessary ordered. For example, v=(1,2,3,2,3,2,4), you will sort the data set and get v = (1,2,2,2,3,3,4), then the first quartile is located at k =[0.25*n](taking the whole number, where n is the number of items in the data set). In this case, the first quartile is located at  k =[0.25*7] = 1, so the first quartile is v[1] = 2.

 

pair<double, int> Q1(vector<double> v)

//input: v is a nonempty data set

//output: the first quartile and its index at v. For example, for data set v=(1,2,3,2,3,2,4), the result is (2,1).

answer:


  
  
pair<double, int> Q1(vector<double> v){ //sort sort(v.begin(),v.end()); int q1 = v.size() * 0.25; double d = v[q1]; pair<double, int> p = pair<double, int>(d,q1); return p; }


1002 Means
Description

 Implement the following function which reads the probability distribution and computes the mean (期望值) for a discrete random variable.

double mean(){

 //read input, compute the mean and return it.

}

 

Input

 The first line is an integer n (0<n<100), the number of values which a discrete random variable X can take. Then follows n integers which are the values X can take, then n doubles which are the corresponding probability.

Sample Input
 Copy sample input to clipboard
3
0    1    2
0.4 0.2 0.6
Sample Output
1.4
Hint

 Your function mean() simple reads data from standard input, computes the mean and returns it. Don't print anything. Submit only your function mean().



double mean(){
	int n;
	cin>>n;
	vector<int> values;
	vector<double> probabilities;
	int t;
	for (int i = 0; i < n; ++i)
	{	
		cin>>t;
		values.push_back(t);
	}
	double tt;
	for (int i = 0; i < n; ++i)
	{	
		cin>>tt;
		probabilities.push_back(tt);
	}
	double mean = 0;;
	for (int i = 0; i < n; ++i)
	{
		mean += values[i] * probabilities[i];
	}
	return mean;
}


1003 Set Insection
Description

A set of integers is repsented by an ordered list of integers in decsending (递减) order. Given two sets, your task is to compute the intersection(交). 

Input

There are multiple test cases.

The first line is the number of test cases. Then follow the test cases. For each test case, the first line is the number of elements in the first set, then the next line contains the elements in descending order, the third line is the number of elements in the second set, then follows the elements  of the second set in descending order.

Output

For every test case, output all elements in descending order and leave one space after each element. Every output takes one line.

Sample Input
 Copy sample input to clipboard
3
3
5 3 2
2
6 3
2
4 2
1
3
4
9 6 3 2
3
9 5 2
Sample Output
3 

9 2 

//Set intersection
list<int> intersection(list<int> &l, list<int> &m){
	list<int> result;
	while(!l.empty() && !m.empty()){
		if(l.front() == m.front()){
			result.push_back(l.front());
			l.pop_front();
			m.pop_front();
		}
		else if(l.front()>m.front()){
			l.pop_front();
		}
		else{
			m.pop_front();
		}
	}
	return result;
}

void print(list<int> & l){
	for (std::list<int>::iterator i = l.begin(); i != l.end(); ++i)
	{
		cout<<*i<<" ";
	}
	cout<<endl;
}


int main(){
	int numOfCases;
	cin>>numOfCases;
	for (int eachCase = 0; eachCase < numOfCases; ++eachCase)
	{
		int numset1;
		cin>>numset1;
		list<int> set1,set2;
		int t;
		for (int i = 0; i < numset1; ++i)
		{
			cin>>t;
			set1.push_back(t);
		}
		int numset2;
		cin>>numset2;
		for (int i = 0; i < numset2; ++i)
		{
			cin>>t;
			set2.push_back(t);
		}

		list<int> result = intersection(set1,set2);
		print(result);
	}
}



1004. Simple Stack Application
 
 
Time Limit: 1sec    Memory Limit:256MB
Description

//pop out all strings in s and print them on the screen one by one in one line. Leave one space after each string, and add a nelwline after the last string (and the space).
void print_it(stack<string> s){

//insert here

}

void print_it(stack<string> s){
	while(!s.empty()){
		cout<<s.top()<<" ";
		s.pop();
	}
	cout<<endl;
}

1005. Searchin in a hash table
 
     
     
 
Time Limit: 1sec    Memory Limit:256MB
Description

The hash table t is established by chaining(拉链法), where each record is a pair of key (int) and the associated information (string). Your task is to implement the search function: if there is a record with key k, then returns the associated information, otherwise, returns the empty string.

string find(vector<vector<pair<int, string> > > t, int k)

//v[i] stores all records whose hashing addresses are i.

{
   // use hash function h(k) = k%t.size()

  //insert here

}


string find(vector<vector<pair<int, string> > > t, int k)

//v[i] stores all records whose hashing addresses are i.

{
   // use hash function h(k) = k%t.size()
	int index = k%t.size();
	vector<pair<int, string> v = t[index];
	for (int i = 0; i < v.size(); ++i)
	{
		if(v[i].first == k){
			return v[i].second;
		}
	}
  //insert here
	return "";
}

1006. Look up problem
 
     
     
 
Time Limit: 1sec    Memory Limit:256MB
Description

Your task if to look up a string w in a set of words S, and returns true if w belong S and false otherwise.

Input

The first line is an integer n (less than 120000), the number of words in the set, then n words are followed. 

The next line is an integer m(less than 120000), which is the number of words you will look up in the set of words given above.

All the words are not necessarily ordered.

Output

For each of the m words, if the search is successful, print ‘Y', otherwise print 'N'.  

Sample Input
 Copy sample input to clipboard
4
algortihm
design
stack
complexity
5
data
design
stacks
vector
sign
Sample Output
NYNNN
Hint

 You need a logarithmic algorithm. 

//look up problem
void Bsearch(vector<string> & orderedSet, string & w, int & position){
	string data;
	int bottom = 0,top = orderedSet.size()-1;
	while(bottom <= top){
		position = (bottom + top)/2;
		data = orderedSet[position];
		if(data == w) return;
		if(data < w) bottom = position + 1;
		else top = position - 1;
	}
	position = -1;
}
int main(){
	int n;
	cin>>n;
	vector<string> v;
	string s;
	for (int i = 0; i < n; ++i)
	{
		cin>>s;
		v.push_back(s);	
	}
	sort(v.begin(), v.end());
	int m;
	cin>>m;
	std::vector<string> l;
	for (int i = 0; i < m; ++i)
	{
			cin>>s;
			l.push_back(s);
	}
	for (int i = 0; i < m; ++i)
	{
		int position;
		Bsearch(v,l[i],position);
		if(position>=0)
			cout<<"Y";
		else cout<<"N";
	}

}
1007. Boolean expression equivalence
 
     
     
 
Time Limit: 1sec    Memory Limit:256MB
Description

Given two boolean expressions and an assignment to all the variables in the expressions, you will check if they compute to the same value. The boolean expressions are given by postfix forms, which contain only lower case letters for variables and operation signs: ~ for negation, + for disjunction (|| in C++) and * for conjunction (&& in C++). The asignment is give by a map which maps characters to booleans.

Now your task is to implement the following function:

bool equal(map<char, bool> &f, const string &e1, const string &e2)

where strings e1 and e2 are two boolean expressions in postfix form and f is the substitution. The function returns true if e1 and e2 compute to the same value under f, returns false otherwise.

For example, e1="ab+~", e2="b~a*", f['a']=false, f ['b']=true, both e1 and e2 compute to false, so equal(f, e1,e2) is true. If e1="ab+", e2="ab*“, f['a']=true, f['b']=false, then e1 and e2 compute to true and false respectively, so equal(f,e1,e2)  is false in this case.

Input

Submit only your function evaluate().


//Boolean expression equivalence


bool equal(map<char, bool> &f, const string &e1, const string &e2){
	stack<bool> s1,s2;
	for (int i = 0; i < e1.size(); ++i)
	{
		char c = e1[i];
		if(islower(c)) s1.push(f[c]);
		if(c == '~'){
			bool t = !s1.top();
			s1.pop();
			s1.push(t);
		}
		if(c=='+'){
			bool t1 = s1.top();
			s1.pop();
			bool t2 = s1.top();
			s1.pop();
			t1 = t1 || t2;
			s1.push(t1);
		}
		if(c == '*'){
			bool t1 = s1.top();
			s1.pop();
			bool t2 = s1.top();
			s1.pop();
			t1 = t1 && t2;
			s1.push(t1);
		}
	}
	//e2
	for (int i = 0; i < e2.size(); ++i)
	{
		char c = e2[i];
		if(islower(c)) s2.push(f[c]);
		if(c == '~'){
			bool t = !s2.top();
			s2.pop();
			s2.push(t);
		}
		if(c=='+'){
			bool t1 = s2.top();
			s2.pop();
			bool t2 = s2.top();
			s2.pop();
			t1 = t1 || t2;
			s2.push(t1);
		}
		if(c == '*'){
			bool t1 = s2.top();
			s2.pop();
			bool t2 = s2.top();
			s2.pop();
			t1 = t1 && t2;
			s2.push(t1);
		}
	}
	return s1.top() == s2.top();
}

1008. Buiding a binary search tree
 
     
     
 
Time Limit: 1sec    Memory Limit:256MB
Description

Given a list of integers, we can build a binary search tree by inserting them one by one starting from the empty tree. Could you compute the height of the tree? The heigth of then empty binary search tree is -1, and hence the singltong binary tree has height 0. 

Input

The first line is the number of test cases.  Then follows the test cases. Each test case has two line: the first one is the number of integer keys, the second line list n distinct integers separated with spaces.

Output

For each test case, output the height on one line. It is guaranteed that all the input and output are ints.

Sample Input
 Copy sample input to clipboard
2
3
3 5 2
4
7 5 4 9
Sample Output
1
2
//building a binary searchj tree

struct BinaryNode{  
  int data;  
  BinaryNode *left, *right;  
  BinaryNode(T d, BinaryNode *l=NULL, BinaryNode* r=NULL):data(d), left(l), right(r) {};  
};  

int height(BinaryNode* root){  
    int leftheight,rightheight;  
    if(root==NULL) return -1;  
    else{  
        leftheight = height(root->left);  
        rightheight = height(root->right);  
        return (leftheight>rightheight)? (leftheight+1) : (rightheight+1);  
  
    }  
  
}   
  
void recursive_insert(BinaryNode* &p, int data){  
    if(p == NULL){  
        p = new BinaryNode(data);  
    }  
    else if(data < p->data){  
        recursive_insert(p->left,data);  
    }  
    else if(data > p->data){  
        recursive_insert(p->right,data);  
    }  
}  
  
void BST_insert(BinaryNode* &root,int data){  
    recursive_insert(root, data);  
}  
int main(){
	int cn;
	cin>>cn;
	for (int eache = 0; eache < cn; ++eache)
	{
		int n;cin>>n;  
	    int t;    
	    BinaryNode* root = NULL;  
	    for (int i = 0; i < n; ++i)  
	    {  
	        cin>>t;  
	        BST_insert(root,t);  
	    }  
	      
	      
	    cout<<height(root)<<endl;
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值