AC代码
实训(四)
B.确定数制
#include<iostream>
using namespace std;
int maxd(int n)
{
int maxd=0,k;
while(n!=0)
{
k=n%10;
if(k>maxd)
maxd=k;
n/=10;
}
return maxd;
}
int cov(int m,int k)
{
int p,s;
s=0;p=1;
while(m!=0)
{
s+=m%10*p;
m/=10;
p*=k;
}
return s;
}
int main()
{
int n;
cin>>n;
while(n--)
{
int p,q,r,dp,dq,dr;
cin>>p>>q>>r;
int mp=maxd(p);
int mq=maxd(q);
int mr=maxd(r);
if(mp>mq) mq=mp;
if(mq>mr) mr=mq;
for(mr++;mr<=16;mr++)
{
dp=cov(p,mr);
dq=cov(q,mr);
dr=cov(r,mr);
if(dp*dq==dr)
break;
}
if(mr>16)
mr=0;
cout<<mr<<endl;
}
return 0;
}
C.最长子串
#include<stdio.h>
#include<string.h>
int main()
{
char str[100][101],minstr[101],substr[101],revsubstr[101];
unsigned int i,j,k,t,m,n,substrlen,found,minlen;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
minlen=101;
for(i=0;i<n;i++)
{
scanf("%s",str[i]);
for(k=0;str[i][k]!='\0';k++)
if(str[i][k]>='A' && str[i][k]<='Z')
str[i][k]+=32;
if(strlen(str[i])<minlen)
{
minlen=strlen(str[i]);
m=i;
}
}
strcpy(minstr,str[m]);
substrlen=minlen;
while(substrlen>0)
{
for(i=0;i<=minlen-substrlen;i++)
{
strncpy(substr,minstr+i,substrlen);
substr[substrlen]='\0';
strncpy(revsubstr,minstr+i,substrlen);
revsubstr[substrlen]='\0';
strrev(revsubstr);
found=1;
for(j=0;j<n;j++)
{
if(strstr(str[j],substr)==NULL && strstr(str[j],revsubstr)==NULL)
{
found=0;
break;
}
}
if(found)
break;
}
if(found)
break;
substrlen--;
}
printf("%d\n",substrlen);
}
return 0;
}
实训(三)
D.分数数列
#include<iostream>
using namespace std;
int main()
{
int c[2100],d[2100],n;
int m,f;
while(cin>>n)
{
c[1]=1;d[1]=2;
c[2]=3;d[2]=5;
for(int i=3;i<=n;i++)
{
m=c[i-1]+1;
while(1)
{
f=0;
for(int j=i-1;j>0;j--)
{
if(m==d[j])
{
f=1;
break;
}
if(d[j]<m)
break;
}
if(f==0)
{
c[i]=m;
break;
}
m++;
}
d[i]=c[i]+i;
}
cout<<c[n]<<"/"<<d[n]<<endl;
}
return 0;
}
E.过河卒
#include<iostream>
using namespace std;
const int N=30,M=30;
int main()
{
int i,j,n,m,x,y;
long f[N][M];
while(cin>>n>>m>>x>>y)
{
for(i=0;i<=n;i++)
for(j=0;j<=m;j++)
{
f[i][j]=0;
}
f[x][y]=1;
for(i=x+1;i<=n;i++)
f[i][y]=1;
for(j=y+1;j<=m;j++)
f[x][j]=1;
for(i=x+1;i<=n;i++)
for(j=y+1;j<=m;j++)
f[i][j]=(f[i-1][j]+f[i][j-1])%1000000007;
cout<<f[n][m]<<endl;
}
return 0;
}