`
xuerenlv
  • 浏览: 6082 次
  • 性别: Icon_minigender_1
  • 来自: 南京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

华中科技大学_2005保研___考研计算机_复试上机

 
阅读更多

1,找位置

题目描述:

对给定的一个字符串,找出有重复的字符,并给出其位置,如:abcaaAB12ab12
输出:a,1;a,4;a,5;a,10,b,2;b,11,1,8;1,12, 2,9;2,13。

输入:

输入包括一个由字母和数字组成的字符串,其长度不超过100。

输出:

可能有多组测试数据,对于每组数据,
按照样例输出的格式将字符出现的位置标出。

样例输入:
abcaaAB12ab12
样例输出:
a:0,a:3,a:4,a:9
b:1,b:10
1:7,1:11
2:8,2:12

自评:太差了,感觉很不好,这个题搞了近一个小时才accepted,还有待练习。

总结: 1,变量定义的很随意,想当然的使用,可能前边的使用已改变其值,后边再用当然出错,这个最难查出来,切记。

2,变量赋值,变量赋值,地点不同,效果当然不一样,特别是在循环中与循环外,这个也是要切记。

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

int main(){
	int i,j;
	string str;
	
	while(getline(cin,str)){
		char print_char[51];
		int k=0;

		//处理字符串
		for(i = 0;i < str.length();i++){
			bool flag=false;
			int place[100];
			int h=0;

			place[h++] = str[i];
			place[h++] = i;
	
			for(j = i+1;j<str.length();j++){
				if(str[j] == str[i]){
					place[h++] = j;
				}
			}

			for(j = 0;j < k;j++){
				if(print_char[j] == str[i]){
					flag = true;
				}
			}    
			
			if(h > 2 && !flag){
				print_char[k++] = (char) place[0];
				for(j = 1;j < h-1;j++){
						cout<<(char)place[0]<<":"<<place[j]<<",";
				}
						cout<<(char)place[0]<<":"<<place[h-1]<<endl;
			}
			
		}

	}



	return 0;
}


2,最大的两个数

题目描述:

输入一个四行五列的矩阵,找出每列最大的两个数。

输入:

输入第一行包括一个整数n(1<=n<=1000),接下来的四行每行包括五个整数。代表一个四行五列的矩阵,矩阵元素全部是整数。

输出:

可能有多组测试数据,对于每组数据,按照样例输出的格式将每列最大的两个数输出,如果最大的两个数中的一个数在这一列中有多个相同的值,则行值取行值小的那一个。
输出时要保留原矩阵的行列顺序,即在原矩阵中行值小的,在输出矩阵中的行值依然小。

样例输入:
1
1  2   4  9  8
-1  4  9  8  8
12  9  8  7  0
7   8  9  7  0
样例输出:
12 9 9 9 8 
7 8 9 8 8 
提示:

每个数字后面都要输出一个空格

自评:不好意思,没有ac,搞不懂,这个题目本身的意思都没有说明,首先n没有讲明做什么,每个数字后都要有空格,无聊啊。

总结:我感觉我的方法不错,找两个数,都可以一前一后设置两个指针,然后进行操作,这种方法太好了,还通用。

#include<iostream>
using namespace std;

int main(){
	int n,i,j;
	int h,k;
	int arr[4][5];
	int prt[2][5];

	while(cin>>n){
		while(n--){
		for(i=0;i<4;i++){
			for(j=0;j<5;j++){
				cin>>arr[i][j];
			}
		}
		
		//生成输出序列
		for(i=0;i<5;i++){
			h=0;
			k=4;
			prt[0][i]=arr[h][i];
			prt[1][i]=arr[k][i];
			
			while(h+1 != k){
				if(arr[h][i] >= arr[k][i]){
					k--;
					if(arr[k][i] > prt[1][i]){
						prt[1][i] = arr[k][i];
					}

				}else if(arr[h][i] < arr[k][i]){
					h++;
					if(arr[h][i] > prt[0][i]){
						prt[0][i] = arr[h][i];
					}
				}
			}  
			
		}
		
		//打印输出序列
		for(i=0;i<2;i++){
			for(j=0;j<5;j++){
				cout<<prt[i][j]<<" ";
			}
			//cout<<prt[i][4]<<endl;
			cout<<endl;
		}
		}
	}

	return 0;
}

3,二叉排序树

题目描述:

输入一系列整数,建立二叉排序数,并进行前序,中序,后序遍历。

输入:

输入第一行包括一个整数n(1<=n<=100)。
接下来的一行包括n个整数。

输出:

可能有多组测试数据,对于每组数据,将题目所给数据建立一个二叉排序树,并对二叉排序树进行前序、中序和后序遍历。
每种遍历结果输出一行。每行最后一个数据之后有一个空格。

样例输入:
5
1 6 5 9 8
样例输出:
1 6 5 9 8 
1 5 6 8 9 
5 8 9 6 1 
提示:

输入中可能有重复元素,但是输出的二叉树遍历序列中重复元素不用输出。

自评:这个题目还好,但是还是参考了一下书本(二叉排序树创建),纯手写还是有待练习。

总结:编程是个细活,细节上还是要特别注意。

#include<iostream>
using namespace std;

struct node{
	int data;
	struct node* l;
	struct node* r;
};

struct node* insert_sort_tree(struct node* root,int num){
	struct node* p = root;
	struct node* father = NULL;

	//查找插入位置
	while(p){
		if(p->data == num){
			return root;
		}
		father=p;
		if(p->data > num){
			p=p->l;
		}else if(p->data < num){
			p=p->r;
		}
	}
	
	//插入元素
	p = new node;
	p->data = num;
	p->l = p->r =NULL;
	if(root == NULL){
		root = p;
		return root;
	}
	if(num < father->data){
		father->l = p;
	}
	if(num > father->data){
		father->r = p;
	}
	return root;
}

void pre_view(struct node* b_s_tree){
	if(b_s_tree){
		cout<<b_s_tree->data<<" ";
		pre_view(b_s_tree->l);
		pre_view(b_s_tree->r);
	}
}

void mid_view(struct node* b_s_tree){
	if(b_s_tree){
		mid_view(b_s_tree->l);
		cout<<b_s_tree->data<<" ";
		mid_view(b_s_tree->r);
	}
}

void last_view(struct node* b_s_tree){
	if(b_s_tree){
		last_view(b_s_tree->l);
		last_view(b_s_tree->r);
		cout<<b_s_tree->data<<" ";
	}
}

void clean_tree(struct node* b_s_tree){
	if(b_s_tree){
		clean_tree(b_s_tree->l);
		clean_tree(b_s_tree->r);
		delete b_s_tree;
	}
}

int main(){
	int n;

	while(cin>>n){
		int num;
		struct node* b_s_tree = NULL;

		while(n--){
			cin>>num;
			b_s_tree = insert_sort_tree(b_s_tree,num);
		}
		pre_view(b_s_tree);
		cout<<endl;
		mid_view(b_s_tree);
		cout<<endl;
		last_view(b_s_tree);
		cout<<endl;
		clean_tree(b_s_tree);
	}


	return 0;
}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics