acm2 - 3-10

命令行的使用

在这里插入图片描述

计算二叉树叶子的带权路径长度的算法

在这里插入图片描述

第一步: 找叶子。(无顺序要求)

第二步:找全所有叶子,(且每一个叶子都要去过)

第一种输入,已知个数的

#include<iostream>
using namespace std;

struct BtreeNode
{
    int data;
	 struct BtreeNode *child[2];
};



void CountWPL(BtreeNode *root,int lev,int &w)
{
	if(root==0) return ;
	if((root->child[0]==0)&&(root->child[1]==0))
	   w=w+root->data*lev;
	CountWPL(root->child[0],lev+1,w);
    CountWPL(root->child[1],lev+1,w);
}

struct BtreeNode * create()
{   int x;
    struct BtreeNode *p;
	cin>>x;
    if(x==-1) return 0;
	else 
	{
		 p=new struct BtreeNode;
		 p->data=x;
		 p->child[0]=create();
         p->child[1]=create();
		 return p;
	}

}

int main()
{ 
  BtreeNode *root;
  int level=0,wpl=0;
  int num=0;
  cin>>num;
  int n=0;
   while(n<num)
  {
 	wpl=0;
    root=create();
    CountWPL(root,level,wpl);
    cout<<wpl;
    n++;
  }
  return 0;
}

第二种、输入以0结尾

#include<iostream>
using namespace std;
struct BtreeNode
{
    int data;
	 struct BtreeNode *child[2];
};
void CountWPL(BtreeNode *root,int lev,int &w)
{
	if(root==0) return ;
	if((root->child[0]==0)&&(root->child[1]==0))
	   w=w+root->data*lev;
	CountWPL(root->child[0],lev+1,w);
    CountWPL(root->child[1],lev+1,w);
}
struct BtreeNode * create(int first=0)
{   int x;
    struct BtreeNode *p;
	cin>>x;
    if(x==-1) return 0;
    else if( x==0 ) return 0;
	else 
	{
		 p=new struct BtreeNode;
		 p->data=x;
		 p->child[0]=create();
         p->child[1]=create();
		 return p;
	}

}

int main()
{ 
  BtreeNode *root;
  int level=0,wpl=0;
   while(cin>>x&&x!=0)           //对每行的第一个数字判断
  {
 	wpl=0,level=0;
    root=create(1);
    CountWPL(root,level,wpl);    //这里需要清内存,及时回收内存 
    cout<<wpl<<endl;
  }
  return 0;
}

第三种


struct BtreeNode * create(int first=0)
{   int x;
    struct BtreeNode *p;
	cin>>x;
    if(x==-1) return 0;
    else if( x==0 ) return 0;
	else 
	{
		 p=new struct BtreeNode;
		 p->data=x;
		 p->child[0]=create();
         p->child[1]=create();
		 return p;
	}

}

int x; 
int main()
{ 
  BtreeNode *root;
  int level=0,wpl=0;
   while(cin>>x)           //对每行的第一个数字判断
  {
 	  wpl=0,level=0;
    root=create(1);
    CountWPL(root,level,wpl);    //这里需要清内存,及时回收内存 
    cout<<wpl<<endl;
  }
  return 0;
}

STL使用

容器适配器

1.栈
2.队列
3.优先队列(堆)
一、

pop()
top()
push()

队列
back() //取最后元素,不删
empty() 判空
front() 取队头
pop() 出队首
size()
在这里插入图片描述

优先队列、堆
priority_queue 堆排序技术实现,保证最大元素在最前面。
在这里插入图片描述

根据最大优先队列的构建最小优先队列

STL有三大核心部分 : 容器、算法、迭代器 ( 类似数组下标 ,链表的结点地址)

(1)顺序容器
vetor : 后部插入/删除
deque : 双端队列
list : 双向链表、
(2)关联容器
set : 集合,快速查找,无重复元素
multiset : 有序的,从小到大(允许有重复值)
map : 一对一映射
multimap: 一对一映射,可有重复元素, 基于关键字查找。

作业

1383 Binary Numbers

在这里插入图片描述

Output

The output should consists of exactly d lines, one line for each data set.

Line i, 1 <= i <= d, should contain increasing sequence of integers separated by single spaces - the positions of 1’s in the binary representation of the i-th input number.

Sample Input

1
13

Sample Output

0 2 3

#include<iostream>
#include<queue>
using namespace std;

int main()
{
	int i=0,n=0;
	cin>>i;
	char ch;
	for(int x=0;x<i;x++)
	{
		cin>>n;
		queue<char> st;
		while(n>0)
		{
			if(n%2==1)
			{
				st.push('1');
			 } else{
			 	st.push('0');
			 }
			 n/=2;
		}
		int m=0;
		while(!st.empty())
		{
			ch=st.front();
			if(ch=='1'){
				cout<<m;
				st.pop();
			  if(!st.empty())
		    	{
				cout<<" ";
			    }
			}
			else
			{
				st.pop();
			}
			m++;
		}
		cout<<endl;
	}
	return 0;
}

vector

在这里插入图片描述

练习

输入N,表示一共有几个数字。
找一组数中的最小值,输出最小值后,删除该值。
以0为结束

目的练习使用multiset

在这里插入图片描述


#include<iostream>
#include <set>
using namespace std;
multiset<int> base;
int a[100];
int main()
{
	int N = 0, num = 0, i = 0,j=0;
	char ch;
	while (cin >> N)
	{
		if (N == 0)
			break;
		for (i = 0; i < N; i++)
		{
			cin >> ch ;
			if (ch == 'B')
			{
				cin >> num;
		        base.insert(num);
			}
			else
			{
				a[j] = *base.begin();
				cout << a[j] << endl;
				base.erase(a[j]);
				j++;
			}
		}
	}
	return 0;
}

/*
8
B 20
B 10
G
B 9
G 
B 100
B 25
G 
0


*/
http://poj.org

2503 Babelfish

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 64471 Accepted: 26066

Description
You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.

Input
Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.

Output
Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as “eh”.

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

Sample Output

cat
eh
loops

Hint
Huge input and output,scanf and printf are recommended.

Source

难点

怎么处理输入空行?

思路一、 自己分割一行的单词

思路二、

在这里插入图片描述

作业: 1965
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值