浙大计算机研究生复试上机考试-2005年

HDU 1228 http://acm.hdu.edu.cn/showproblem.php?pid=1228

考察:输入输出

#include<bits/stdc++.h>
#define inf 0x3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1  
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)		
using namespace std;
typedef long long ll;
const int _max = 105;

int main(){
	char str[_max];
	while(gets(str)!= NULL){
		char s1[_max];
		int ret = 0,sum = 0,pos = 0;
		for(int i = 0 ; i < strlen(str) ; i++){
			if(str[i] <= 'z' && str[i] >= 'a')	s1[pos++] = str[i];
			else if(str[i] == ' '){
				s1[pos]='\0';
				if(!strcmp(s1,"zero")) ret = ret*10+0;   
                else if(!strcmp(s1,"one")) ret = ret*10+1;  
                else if(!strcmp(s1,"two")) ret = ret*10+2;  
                else if(!strcmp(s1,"three"))ret = ret*10+3;  
                else if(!strcmp(s1,"four")) ret = ret*10+4;  
                else if(!strcmp(s1,"five")) ret = ret*10+5;  
                else if(!strcmp(s1,"six")) ret = ret*10+6;  
                else if(!strcmp(s1,"seven")) ret = ret*10+7;  
            	else if(!strcmp(s1,"eight")) ret = ret*10+8;  
        		else if(!strcmp(s1,"nine")) ret = ret*10+9;  
				pos = 0;
			}
			else{
				sum += ret;
				ret = 0;
			}	
		}
		if(sum == 0) break;
		printf("%d\n",sum); 
	}
	return 0;
}

HDU 1231 http://acm.hdu.edu.cn/showproblem.php?pid=1231

贪心,模拟

#include<bits/stdc++.h>
#define inf 0x3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1  
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)		
using namespace std;
typedef long long ll;
const int _max = 10050;
int a[_max];
int main(){
	int n;
	while(cin>>n){
		if(!n) break;
		memset(a,0,sizeof(a));
		for(int i = 1 ; i <= n ; i++) cin>>a[i];
		int ans = -inf,cnt = 0;
		int s = 1,e = n;
		int i = 0, j = 0;
		for(int k = 1 ; k <= n ; k++){
			if(cnt + a[k] < 0){
				i = 0;
				j = 0;
				cnt = 0;
				continue ;
			}
			if(i == 0 && j == 0) i = k;
			j = k;
			cnt += a[k];
			if(cnt > ans){
				ans = cnt;
				s = i;
				e = j;
			}
		}
		if(ans == -inf) ans = 0;
		cout<<ans<<' '<<a[s]<<' '<<a[e]<<endl;	
	}	
	return 0;
}

HDU 1232 http://acm.hdu.edu.cn/showproblem.php?pid=1232

并查集

#include<bits/stdc++.h>
#define inf 0x3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1  
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)		
using namespace std;
typedef long long ll;
const int _max = 1050;
int father[_max];
void init(int n){
	for(int i = 1; i <= n ; i++){
		father[i] = i;
	} 
	return ;
}
inline int find(int x){
	while(father[x] != x)
		x = father[x];
	return x;
}
inline void Union(int a,int b){
	int temp_a = find(a);
	int temp_b = find(b);
	if(temp_a != temp_b) father[temp_a] = temp_b;
}
int main(){
	int n,m;
	while(cin>>n){
		if(!n) break;
		cin>>m;
		init(n);	
		int u,v;
		int ans = 0;
		for(int i = 1 ; i <= m ; i++){
			cin>>u>>v;
			Union(u,v);
		}
		for(int i = 1 ; i <= n ; i++)
			if(father[i] == i) ans++;
		cout<<ans-1<<endl;
	}
	return 0;
}

HDU 1234 http://acm.hdu.edu.cn/showproblem.php?pid=1234

sort,cmp。没啥好说的

#include<bits/stdc++.h>
#define inf 0x3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1  
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)		
using namespace std;
typedef long long ll;
const int _max = 1050;
typedef struct {
	string s,e;
	string id;
}node;
node a[_max];
bool cmp1(node n1,node n2){
	int len = 8;
	for(int i = 0 ; i < len ; i++)
		if(n1.s[i] < n2.s[i]) return true;
		else if(n1.s[i] > n2.s[i]) return false;
	return true;
}
bool cmp2(node n1 ,node n2){
	int len = 8;
	for(int i = 0 ; i < len ; i++)
		if(n1.e[i] < n2.e[i]) return true;
		else if(n1.e[i] > n2.e[i]) return false;
	return true;	
}

int main(){
	int n,m;
	cin>>n;
	while(n--){
		cin>>m;
		for(int i = 1 ; i <= m ; i++)
			cin>>a[i].id>>a[i].s>>a[i].e;
		sort(a+1,a+m+1,cmp1);
		cout<<a[1].id<<' ';
		sort(a+1,a+m+1,cmp2);
		cout<<a[m].id<<endl;
		
	}
	return 0;
}

HDU 1236 http://acm.hdu.edu.cn/showproblem.php?pid=1236

太简单了。不做了。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值