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
太简单了。不做了。