北京大学国际大学生程序竞赛的经典题目的代码和思路Word文件下载.docx
《北京大学国际大学生程序竞赛的经典题目的代码和思路Word文件下载.docx》由会员分享,可在线阅读,更多相关《北京大学国际大学生程序竞赛的经典题目的代码和思路Word文件下载.docx(105页珍藏版)》请在冰点文库上搜索。
intctoi(chara){
intb;
if(a=='
A'
)b=0;
C'
)b=1;
G'
)b=2;
T'
)b=3;
)b=4;
returnb;
}
intmain()
{
intt,j,k,m,n;
intf1,f2,f3;
intf[101][101];
intarr[5][5]={{5,-1,-2,-1,-3},{-1,5,-3,-2,-4},{-2,-3,5,-2,-2},{-1,-2,-2,5,-1},{-3,-4,-2,-1,0}};
stringa,b;
cin>
>
t;
while(t--){
j=k=0;
memset(f,0,sizeof(f));
m>
a;
n>
b;
for(j=0;
j<
=m;
j++)
{
for(k=0;
k<
=n;
k++)
if(j==0&
&
k==0)
f[j][k]=0;
}
elseif(j==0)
f[j][k]=f[j][k-1]+arr[ctoi('
)][ctoi(b[k-1])];
elseif(k==0)
f[j][k]=f[j-1][k]+arr[ctoi(a[j-1])][ctoi('
)];
else
f1=f[j-1][k]+arr[ctoi(a[j-1])][ctoi('
f2=f[j][k-1]+arr[ctoi('
f3=f[j-1][k-1]+arr[ctoi(a[j-1])][ctoi(b[k-1])];
f[j][k]=MAX(f1,f2,f3);
cout<
return0;
2、/*1088-滑雪.cpp*/
#include
constinthighest=100000;
inthigh[100][100];
booltravel[100][100];
intvalue[100][100];
intr,c;
constintdir[4][2]={0,1,1,0,-1,0,0,-1};
intfind_rout(intx,inty){
inti,temp;
if(x<
0||y<
0||x>
=r||y>
=c)
if(travel[x][y])
returnvalue[x][y];
intmax_rout=1;
for(i=0;
i<
4;
++i){
if(high[x][y]>
high[x+dir[i][0]][y+dir[i][1]])
temp=find_rout(x+dir[i][0],y+dir[i][1]);
if(max_rout<
temp+1)
max_rout=temp+1;
travel[x][y]=true;
value[x][y]=max_rout;
returnmax_rout;
intmain(){
cin>
r>
c;
inti,j;
r;
++i)
for(j=0;
j<
++j){
travel[i][j]=false;
high[i][j];
++j){
inttemp=find_rout(i,j);
if(temp>
max_rout)
max_rout=temp;
cout<
<
max_rout<
endl;
3、精度运算
/*2262-Goldbach'
sConjecture.cpp*/
voiddoRun(){
inti,j,sum,a;
longt;
while(cin>
a){
t=0;
if(a==0||a>
10000)
break;
sum=0;
i=1;
while
(1){
sum+=i;
if(sum>
=a){
t=a-(sum-i);
i++;
t=t*i;
for(j=1;
jt+=j*j;
printf("
%ld%ld"
a,b);
if(a>
b){
n=a;
a=b;
b=n;
s=0;
for(i=a;
i<
=b;
i++){
count=1;
temp=i;
while(temp!
=1){
if(temp%2==1)temp=3*temp+1;
elsetemp=temp/2;
count++;
if(count>
s)s=count;
%ld\n"
s);
doRun();
5、/*2080-Calendar.cpp*/
intmday[2][13]={0,31,28,31,30,31,30,31,31,30,31,30,31,0,31,29,31,30,31,30,31,31,30,31,30,31};
charweek[7][20]={"
Saturday"
"
Sunday"
Monday"
Tuesday"
Wednesday"
Thursday"
Friday"
};
intmain()
intn;
ints,y,m,d,t;
while
(1){
n;
if(n<
0)
d=n%7;
n++;
for(y=s=0;
s<
n;
s+=t,y++)
if(y%400==0||y%4==0&
y%100)
t=366;
t=365;
y--;
s-=t;
n-=s;
y+2000<
'
;
t-=365;
for(m=0,s=0;
m++,s+=mday[t][m]);
setw
(2)<
setfill('
0'
)<
m<
s-=mday[t][m];
n<
week[d]<
6、/*1298-TheHardestProblemEver.cpp*/
voidmain()
stringS1("
ABCDEFGHIJKLMNOPQRSTUVWXYZ"
);
stringS2("
VWXYZABCDEFGHIJKLMNOPQRSTU"
stringstart,end,over("
ENDOFINPUT"
stringstr;
charch;
inti,j;
while
(1)
getline(cin,start);
if(pare(over)==0)break;
/*
while((ch=getchar())!
='
\n'
)
if(ch<
Z'
ch>
j=S1.find(ch);
ch=S2[j];
getline(cin,str);
for(i=0;
i{
if(str[i]<
str[i]>
j=S1.find(str[i]);
str[i]=S2[j];
getline(cin,end);
7、/*1745-Divisibility.cpp*/
intd[105],e[105],a,n,k;
inti,p,q;
scanf("
%d%d"
&
n,&
k);
memset(d,0,k*sizeof(int));
%d"
a);
a=a%k;
if(a<
0)a+=k;
d[a]=1;
while(--n)
p=a%k;
q=(-a)%k;
if(p<
0)p+=k;
if(q<
0)q+=k;
memset(e,0,k*sizeof(int));
for(i=0;
k;
i++,p++,q++)
if(d[i]==1)
e[p%k]=e[q%k]=1;
memcpy(d,e,k*sizeof(int));
printf(d[0]?
"
Divisible\n"
:
Notdivisible\n"
);
8、/*2249-BinomialShowdown.cpp*/
intn,k;
doubletemp;
k)
if(n==0&
k==0)break;
temp=1;
if(n-k>
for(inti=n-k+1;
i<
++i)
temp*=i;
for(intj=1;
=k;
++j)
temp/=j;
for(inti=k+1;
=n-k;
9、/*2309-BST.c*/
voiddoRun()
longlongi,j,k,s;
longlonga;
scanf("
&
n);
%I64d"
a);
s=a;
if((s&
1)==1)
%I64d%I64d\n"
s,s);
j=0;
if((s&
1)==1)
s>
=1;
j++;
k=1;
k<
=j;
a-k+1,a+k-1);
10、/*1767-WhichisNext.cpp*/
longNextTreeIdentify(stringt)
strings,t2,t001("
001"
s=t;
longres;
intnode,i,posof0,posof001;
boolend=true;
=s.size()-3;
i++)
t2=s.substr(i,3);
if(pare(t001)==0)
posof001=i;
end=true;
for(i=posof001+3;
if(s[i]!
='
1'
end=false;
if(!
end)
/*s.erase(posof001+1,2);
posof0=s.find_first_of("
0"
posof001+1);
s.insert(posof0+1,"
1"
s.replace(posof001,3,"
s.replace(posof0,1,"
s[0]='
for(i=1;
if(i%2==0)
s[i]='
i=s.size()-1;
res=0;
while(i>
=2)
node=s[i]-'
if(node==1)
res+=node<
i--;
returnres;
longres,n,t;
intnode;
stacktree;
strings;
%ld"
if(n==0||n==4)
res=n;
t=n;
node=t%2;
tree.push(node);
if(t<
=1)break;
t>
=1;
if(tree.size()>
30)
return;
while(!
tree.empty())
s.insert(0,(char*)(tree.top()+'
));
tree.pop();
res=NextTreeIdentify(s);
%ld\n"
res);
11、/*2229-Sumsets.cpp*/
#defineM1000001
intb[M];
inti;
b[1]=1;
b[2]=2;
n=0;
for(i=2;
b[i]=b[i++-1];
b[i]=(b[i-2]+b[i/2])%1000000000;
while(scanf("
i)!
=EOF)
%d\n"
b[i]);
12、/*2533-LongestOrderedSubsequence.c*/
voidprocess(int*a,intsum)
intf[1000],res,max;
=sum;
i++)
f[i]=0;
for(i=sum;
i>
0;
i--)
max=0;
for(j=i+1;
j<
j++)
if(a[j]>
a[i])
if(max{
max=f[j];
}
f[i]=max+1;
if(res{
res=f[i];
main()
intsum;
inta[1000];
sum);
a+i);
process(a,sum);
13、/*2545-HammingProblem.c*/
intisPrime(longlonga)
intflag=1;
longlongi;
if(a%i==0&
a!
flag=0;
break;
returnflag;
longlonggetMin3(longlonga,longlongb,longlongc)
longlongt=a>
b:
returnt>
c:
t;
longlonggetNum(longlonga,longlongb,longlongc,longlongpos)
longlongp[3],i,j,count=1;
longlongtemp[3]={0},min;
longlong*num=(longlong*)malloc((pos+2)*sizeof(longlong));
p[0]=a;
p[1]=b;
p[2]=c;
num[0]=1;
while(count!
=pos+1)
3;
j{
temp[i]=num[j]*p[i];
if(temp[i]>
num[count-1])
min=getMin3(temp[0],temp[1],temp[2]);
num[count]=min;
min=num[pos];
returnmin;
longlongp1,p2,p3,i;
%I64d%I64d%I64d%I64d"
p1,&
p2,&
p3,&
i);
\
if(isPrime(p1)==0||isPrime(p2)==0||isPrime(p3)==0||p1==p2||p2==p3||p1==p3)
return1;
getNum(p1,p2,p3,i));
getchar();
return0;
14、/*2567-CodetheTree.cpp*/
structnode
intparent;
intchild[51];
intdu;
}nd[51];
inti,j,k,len,temp,root;
chars[256],b[2];
stacksp;
intp[51];
while(gets(s)!
=NULL)
51;
nd[i].du=1;
nd[i].parent=0;
nd[