山东理工大学ACM平台题目答案
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1464
#include
#include
struct node
{
int date;
struct node *next;
};
struct node *creat(struct node *head,int m)
{
int i;
struct node *p,*tail;
head->next=NULL;
tail=head;
for(i=1;i<=m;i++)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->date);
p->next=NULL;
tail->next=p;
tail=p;
}
return head;
}
struct node *del(struct node *head,int z,int r)
{
struct node *q,*p;
int i=0;
p=head;
while(p->next!=NULL)
{
if((p->next->date>=z)&&(p->next->date<=r))
{
i=1;
break;
}
else p=p->next;
}
if(i==1)
{
q=p->next;
p->next=q->next;
free(q);
return p;
}
else return 0;
}
void show(struct node *head)
{
struct node *y;
y=head->next;
while(y!=NULL)
{
if(y->next!=NULL)
printf("%d ",y->date);
if(y->next==NULL)
printf("%d",y->date);
y=y->next;
}
printf("\n");
}
int main()
{
int n,v,n1,n2,n3,i;
struct node *head,*q;
scanf("%d",&n1);
for(i=0;i
head=(struct node*)malloc(sizeof(struct node));
scanf("%d %d %d",&n,&n2,&n3);
getchar();
creat(head,n);
q=head;
while(q!=NULL)
{
q=del(q,n2,n3);
++v;
}
if(v-1==n)
printf("-1\n");
else
show(head);
free(q);
free(head);
}
return 0;
}
1138
#include
#include
struct node
{
int date;
struct node *next;
};
struct node *creat(struct node *head,int m)
{
int i;
struct node *p,*tail;
head->next=NULL;
tail=head;
for(i=1;i<=m;i++)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->date);
p->next=NULL;
tail->next=p;
tail=p;
}
return head;
}
struct node *del(struct node *head,int z)
{
struct node *q,*p;
int i=0;
p=head;
while(p->next!=NULL)
{
if(p->next->date==z)
{
i=1;
break;
}
else p=p->next;
}
if(i==1)
{
q=p->next;
p->next=q->next;
free(q);
return p;
}
else return 0;
}
void show(struct node *head)
{
struct node *y;
y=head->next;
while(y!=NULL)
{
if(y->next!=NULL)
printf("%d ",y->date);
if(y->next==NULL)
printf("%d",y->date);
y=y->next;
}
printf("\n");
}
int main()
{
int n,k,v=0;
struct node *head,*q;
head=(struct node*)malloc(sizeof(struct node));
scanf("%d",&n);
creat(head,n);
scanf("%d",&k);
printf("%d\n",n);
show(head);
q=head;
while(q!=NULL)
{
q=del(q,k);
++v;
}
printf("%d\n",n-v+1);
show(head);
return 0;
}
1605
#include
#include
void show(char c,int a,int b)
{
if(c=='+')
printf("%d\n",a+b);
else if(c=='-')
printf("%d\n",a-b);
else if(c=='*')
printf("%d\n",a*b);
else if(c=='/'&&b!=0)
printf("%.2lf\n",1.0*a/b);
}
int main()
{
int n,a[1000],b[1000],i;
char c[1000];
while(scanf("%d",&n)!=EOF)
{
getchar();
for(i=0;i
scanf("%c %d %d",&c[i],&a[i],&b[i]);
getchar();
}
for(i=0;i
show(c[i],a[i],b[i]);
}
}
return 0;
}
160
6
#include
int f(int m)
{
int p;
if(m==0)
p=7;
if(m==1)
p=11;
if(m>1)
p=f(m-1)+f(m-2);
return p;
}
int main()
{
int n,m;
while(scanf("%d",&n)!=EOF)
{
m=f(n);
if(m%3==0)
printf("yes\n");
else
printf("no\n");
}
return 0;
}
1608
#include
#include
int main()
{
int n,m,i,j,k;
int *a;
scanf("%d %d",&n,&k);
a=(int *)malloc(n*sizeof(int));
for(i=0;i
for(i=0;i
if(a[j]{m=a[j];a[j]=a[j+1];a[j+1]=m;}
}
for(i=0;i
if(i
else printf("%d\n",a[i]);
}
return 0;
}
1607
#include
#include
int main()
{
int n,a,b,i;
int *x;
while(scanf("%d %d %d",&a,&b,&n)!=EOF&&(a||b||n))
{
x=(int *)malloc(n*sizeof(int));
x[0]=1;x[1]=1;
for(i=2;i
x[i]=(a*x[i-1]+b*x[i-2])%7;
}
printf("%d\n",x[n-1]);
free(x);
}
return 0;
}
1634
#include
int f(int k)
{
int i,j;
for(i=2;i
if(k%i==0)
{
j=i;
break;
}
}
return j;
}
int show(int a)
{
int i;
for(i=2;i
return 0;
return 1;
}
int main()
{
int n,m,i,k;
scanf("%d",&n);
for(i=0;i
scanf("%d",&m);
k=m;
if(show(m))
printf("%d",m);
if(!show(m))
{
while(!show(k))
{
printf("%d*",f(k));
k=k/f(k);
}
printf("%d",k);
}
printf("\n");
}
return 0;
}
2119
#include
#include
struct node
{
int date;
struct node *next;
};
struct node * creat(int n)
{
struct node *p,*head1,*tail;
head1=(struct node*)malloc(sizeof(struct node));
int i;
head1->next=NULL;
tail=head1;
for(i=1;i<=n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->date);
p->next=NULL;
tail->next=p;
tail=p;
}
return head1;
}
struct node * built(int m)
{
struct node *q,*head2,*end;
head2=(struct node*)malloc(sizeof(struct node));
int j;
head2->next=NULL;
end=head2;
for(j=1;j<=m;j++)
{
q=(struct node *)malloc(sizeof(struct node));
scanf("%d",&q->date);
q->next=NULL;
end->next=q;
end=q;
}
return head2;
}
struct node * merge(struct node *head1,struct node *head2)
{
struct node *p,*q,*tail;
p=head1->next;
q=head2->next;
tail=head1;
free(head2);
while(p&&q)
{
if(p->date
{
tail->next=p;
tail=p;
p=p->next;
}
else
{
tail->next=q;
tail=q;
q=q->next;
}
}
if(p)
tail->next=p;
else
tail->next=q;
return head1;
}
void show(struct node *head1)
{
struct node *p;
p=head1->next;
while(p!=NULL)
{
if(p->next!=NULL)
printf("%d ",p->date);
else
printf("%d\n",p->date);
p=p->next;
}
}
int main()
{
struct node *head1,*head2;
int n,m;
scanf("%d %d",&n,&m);
head1=creat(n);
head2=built(m);
head1=merge(head1,head2);
show(head1);
ret
urn 0;
}
2120
#include
#include
int c=0;int v=0;
struct node
{
int date;
struct node *next;
};
struct node *creat(int n)
{
struct node *head1,*p,*tail;
int i;
head1=(struct node *)malloc(sizeof(struct node));
head1->next=NULL;
tail=head1;
for(i=1;i<=n;i++)
{
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->date);
p->next=NULL;
tail->next=p;
tail=p;
}
return head1;
}
struct node *split(struct node *head1)
{
struct node *head2,*p,*q,*tail,*end;
head2=(struct node *)malloc(sizeof(struct node));
head2->next=NULL;
end=head2;
p=head1->next;
head1->next=NULL;
tail=head1;
q=p->next;
while(p!=NULL)
{
if(p->date%2!=0)
{
p->next=NULL;
tail->next=p;
tail=p;
c++;
}
else
{
p->next=NULL;
end->next=p;
end=p;
v++;
}
p=q;
if(q!=NULL)
q=q->next;
}
return head2;
}
void show(struct node *head1,struct node *head2)
{
struct node *p,*q;
p=head2->next;
q=head1->next;
while(p!=NULL)
{
if(p->next!=NULL)
printf("%d ",p->date);
else
printf("%d\n",p->date);
p=p->next;
}
while(q!=NULL)
{
if(q->next!=NULL)
printf("%d ",q->date);
else
printf("%d\n",q->date);
q=q->next;
}
}
int main()
{
int n;
struct node *head1,*head2;
scanf("%d",&n);
head1=creat(n);
head2=split(head1);
printf("%d %d\n",v,c);
show(head1,head2);
return 0;
}
2187
#include
#include
int main()
{
char a[100000],b[100000];
int i,j,k,m,v,c,n;
while(scanf("%s %s",a,b)!=EOF)
{
n=0;
getchar();
k=strlen(a);
j=strlen(b);
c=0;
for(i=0;i
v=0;
m=n;
for(;m
if(a[i]-b[m]==0)
{
n=m;
v=1;
n=n+1;
break;
}
else m++;
}
if(v==1)
c++;
}
if(c==k)
printf("Yes\n");
else printf("No\n");
}
return 0;
}
2761
#include
#include
int main()
{
int m,j,k,i,c[10000],v;
char a[10000];
while(scanf("%s",a)!=EOF)
{
getchar();
k=strlen(a);
m=0;
for(i=0;i
c[m]=0;
v=0;
for(j=i;;j++)
{
if(a[i]-a[j]==0)
{
c[m]++;
++v;
}
if(a[i]-a[j]!=0)
{
break;
}
}
if(c[m]==1)
printf("%c",a[i]);
if(c[m]>1)
printf("%d%c",c[m],a[i]);
i=i+v;
m++;
}
printf("\n");
}
return 0;
}2765
#include
int main()
{
int n,m,i;
long a[100];
while(scanf("%d %d",&n,&m)!=EOF)
{
for(i=1;i
a[i]=i;
}
for(i=m;i<=n;i++)
a[i]=a[i-m+1]+a[i-1];
printf("%ld\n",a[n]);
}
return 0;
}
2773
#include
int main()
{
int n,m,i,k;
scanf("%d",&n);
for(i=0;i
scanf("%d",&m);
if(m<150)
k=m;
else if(m>150&&m<200)
k=m-150;
else if(m>200&&m<300)
k=m-200;
else if(m>300&&(m%100>0)&&(m%100<50))
k=m%100;
else if(m>300&&(m%100>50)&&(m%100<100))
k=m%100-
50;
else
k=0;
printf("%d\n",k);
}
return 0;
}
2560
#include
#include
int main()
{
int i,j,k,n;
char a[100000],b[100000],c[5];
strcpy(c,"2013");
while(gets(a)!=NULL)
{
if(strcmp(a,c)==0)
break;
k=strlen(a);
j=0;
for(i=0;i
if(a[i]!=' ')
{
b[j]=a[i];
j++;
}
}
n=0;
for(i=0,k=j-1;i
if(b[i]==b[k])
n++;
}
if(n==j/2)
printf("YES\n");
else printf("NO\n");
}
return 0;
}
1916
#include
#include
int main()
{
int n,j,k,i,m,v,h;
char a[1000],b[100000];
scanf("%d",&n);
getchar();
for(i=0;i
scanf("%s",a);
getchar();
h=strlen(a);
k=0;
for(j=0;j
if(a[j]=='-'&&(a[j-1]>='0'&&a[j-1]<='9'&&a[j+1]<='9'&&a[j+1]>='0')&&(a[j+1]-a[j-1]>=0))
{
for(m=j;m
b[k]=a[j-1]+(m-j+1);
k++;
}
}
else if(a[j]=='-'&&(a[j-1]>='a'&&a[j-1]<='z'&&a[j+1]<='z'&&a[j+1]>='a')&&(a[j+1]-a[j-1]>=0))
{
for(m=j;m
b[k]=a[j-1]+(m-j+1);
k++;
}
}
else if(a[j]=='-'&&(a[j-1]>='A'&&a[j-1]<='Z'&&a[j+1]<='Z'&&a[j+1]>='A')&&(a[j+1]-a[j-1]>=0))
{
for(m=j;m
b[k]=a[j-1]+(m-j+1);
k++;
}
}
else
{
b[k]=a[j];
k++;
}
}
for(v=0;v
printf("\n");
}
return 0;
}
2085
#include
#include
int main()
{
int n,m,j,i,k,h,i1,i2,v;
char a[20][201],t;
scanf("%d",&n);
getchar();
for(i=0;i
gets(a[i]);
k=strlen(a[i]);
for(j=0,h=k-1;j
t=a[i][j];a[i][j]=a[i][h];a[i][h]=t;
}
a[i][k]=' ';
m=0;v=0;
for(j=0;j<=k;j++)
{
if(a[i][j]==' ')
{
m=j;
for(i1=v,i2=m-1;i1
v=m+1;
}
}
printf("Case %d:",i+1);
for(h=0;h
printf("\n");
}
return 0;
}
1568
#include
#include
int main()
{
int n,j,k,m,i,v,h;
char a[20][20];
scanf("%d %d",&n,&m);
getchar();
for(i=0;i
gets(a[i]);
}
for(i=0;i
k=0;
for(j=0;j
if(a[i][j]=='1')
++k;
}
if(k==m)
{
for(h=i;h>0;h--)
strcpy(a[h],a[h-1]);
for(v=0;v
}
}
for(i=0;i
for(j=0;j
printf("\n");
}
return 0;
}
1594
#include
#include
int main()
{
int m,n,i,j;
char a[20][20],b[20][20];
scanf("%d %d",&n,&m);
getchar();
for(i=0;i
gets(a[i]);
}
for(i=0;i
for(i=0;i
for(j=n-1;j>=0;j--)
{
printf("%c",b[i][j]);
}
printf("\n");
}
return 0;
}2809
#include
#include
int show(int a)
{
int i;
for(i=2;i<=s
qrt(a);i++)
if(a%i==0)
return 0;
return 1;
}
int main()
{
int m,i;
while(scanf("%d",&m)!=EOF&&(m||m))
{
for(i=2;i
if(show(i)&&show(m-i))
{
printf("%d = %d + %d\n",m,i,m-i);
break;
}
}
}
return 0;
}
2109
#include
#include
using namespace std;
long a[1000004];
int main()
{
int n,i;
scanf("%d",&n);
for(i=0;i
scanf("%ld",&a[i]);
}
sort(a,a+n);
reverse(a,a+n);
for(i=0;i
if(i
if(i==n-1)
printf("%ld\n",a[i]);
}
return 0;
}
2765
#include
int main()
{
int n,i;
long a[21];
while(scanf("%d",&n)!=EOF)
{
a[1]=0;
a[2]=1;
for(i=3;i<=n;i++)
a[i]=(n-1)*(a[i-1]+a[i-2]);
printf("%ld\n",a[n]);
}
return 0;
}
2521
#include
int main()
{
int n, i, j, max, c, x;
long long a[1000];
while(scanf("%d", &n)!=EOF)
{
max = -1;
c = 0;
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for(i = 0; i < n; i++)
{
x = 0;
for(j = i + 1; j < n; j++)
{
if(a[i] == a[j])
x++;
}
if(max < x)
{
max = x;
c = i;
}
else if(max == x)
{
if(a[c] > a[i])
{
c = i;
}
}
else max = max;
}
printf("%lld %d\n", a[c], max+1);
}
return 0;
}
2801
#include
int main()
{
int n,m,i,c,t,j;
while(scanf("%d %d",&n,&m)!=EOF)
{
int a[10000],b[10000];
int g[100000]={0};
c=0;
for(i=0;i
scanf("%d %d",&a[i],&b[i]);
if(a[i]>b[i])
{t=a[i];a[i]=b[i];b[i]=t;}
}
for(i=0;i
for(j=i+1;j
if(a[i]>a[j])
{
t=a[i];a[i]=a[j];a[j]=t;
t=b[i];b[i]=b[j];b[j]=t;
}
}
}
if(a[0]!=0)
printf("0\n");
else
{
g[b[0]]=1;
for(i=0;i
if(g[a[i]]==1)
g[b[i]]=1;
else if(g[b[i]]==1)
g[a[i]]=1;
}
for(i=1;i
if(g[i]==1)
{
c++;
}
}
printf("%d\n",c);
}
}
return 0;
}
2747
#include
#include
char a[81],c[81];
int main()
{
int j,m,i,h,n,v;
while(scanf("%s",a)!=EOF)
{
getchar();
m=-1;
n=strlen(a);
for(i=0;i
v=0;
if(n%(i+1)==0)
{
h=0;
for(j=0;j
c[j]=a[h];
if(h==i)
h=0;
else h++;
}
c[n]='\0';
if(strcmp(a,c)==0)
{
v=i;
break;
}
}
}
for(i=0;i<=v;i++)
printf("%c",a[i]);
printf("\n");
}
return 0;
}
2398
#include
#include
struct node
{
int q;
char p[1000];
}a[10000],b[10000],c[10000];
int main()
{
struct node d;
int n,m,x,j,i,o;
int k,n2;
while(scanf("%d",&n)!
=EOF)
{
int n1[10000];
for(o=0;o
scanf("%d %d",&m,&x);
for(i=0;i
scanf("%s %d",a[i].p,&a[i].q);
}
for(j=m;j
a[j].q=0;
scanf("%s %d",a[j].p,&n1[j]);
for(k=0;k
scanf("%s",c[k].p);
for(i=0;i
a[j].q=a[j].q+a[i].q;
}
}
scanf("%d",&n2);
for(i=0;i
scanf("%s",b[i].p);
}
for(i=0;i
if(strcmp(b[i].p,a[j].p)==0)
{
b[i].q=a[j].q;
}
}
for(i=0;i
if(b[i].q>b[j].q)
{
d=b[i];b[i]=b[j];b[j]=d;
}
}
for(i=0;i
printf("%s\n",b[i].p);
}
printf("\n");
}
printf("\n");
}
return 0;
}
1227
#include
int main()
{
int n,m,j,i,k,c;
int a[100001],b[100001];
scanf("%d %d",&n,&m);
for(i=0;i
for(j=0;j
c=0;k=-1;
for(i=0;i
for(j=0;j
if(a[i]==b[j]&&k
k=j;
c++;
break;
}
}
}
printf("%d\n",c);
return 0;
}
2046
#include
#include
#include
using namespace std;
int main()
{
char a[202],b[202];
int c[202]={0};
int n,m,k,i;
scanf("%s %s",a,b);
getchar();
n=strlen(a);m=strlen(b);
reverse(a,a+n);
reverse(b,b+m);
if(n>m)
{
k=n;
for(i=0;i
if(c[i]+a[i]+b[i]-96<10)
c[i]=(a[i]+b[i]-96+c[i]);
else if(c[i]+a[i]+b[i]-96>=10)
{
c[i]=(a[i]+b[i]-96+c[i])%10;
c[i+1]=1;
}
}
if(c[m]==0)
for(i=m;i
if(c[m]==1)
for(i=m;i
if(c[i]+a[i]-48>=10)
{
c[i]=(a[i]+c[i]-48)%10;
c[i+1]=1;
}
else
c[i]=a[i]-48+c[i];
}
}
else if(n
k=m;
for(i=0;i
if(c[i]+a[i]+b[i]-96<10)
c[i]=(a[i]+b[i]-96+c[i]);
else if(c[i]+a[i]+b[i]-96>=10)
{
c[i]=(a[i]+b[i]-96+c[i])%10;
c[i+1]=1;
}
}
if(c[n]==0)
for(i=n;i
else if(c[n]==1)
for(i=n;i
if(c[i]+b[i]-48>=10)
{
c[i]=(b[i]+c[i]-48)%10;
c[i+1]=1;
}
else
c[i]=b[i]-48+c[i];
}
}
else if(n==m)
{
k=n;
for(i=0;i
if(c[i]+a[i]+b[i]-96<10)
c[i]=(a[i]+b[i]-96+c[i]);
else if(c[i]+a[i]+b[i]-96>=10)
{
c[i]=(a[i]+b[i]-96+c[i])%10;
c[i+1]=1;
}
}
}
if(c[k]==0)
{
reverse(c,c+k);
for(i=0;i
}
if(c[k]==1)
{
reverse(c,c+k+1);
for(i=0;i<=k;i++)
printf("%d",c[i]);
}
printf("\n");
return 0;
}
2074
#include
#include
using namespace std;
int main()
{
int n,m,i;
while(scanf("%d %d",&n,&m)!=EOF)
{
int a[300],b[300];
for(i=0;i
if(n<=m)
{
printf("%d\n",n);
}
else
{
sort(a,a+n);
for(i=1;i
b[i-1]=a[i]-a[i-1]-1;
}
sort(b,b+n-1);
int k=a[n-1]-a[0]+1;
int nline=1;
int devide=n-2;
while((nline
{
nline++;
k=k-b[devide];
devide--;
}
printf("%d\n",k);
}
}
return 0;
}
1299
#include
int main()
{
int n,m,j,i;
int a[1001],b[1001];
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
b[1]=1;
for(i=2;i<=n;i++)
{
m=0;
for(j=1;jif(a[i]>a[j])
if(mm=b[j];
b[i]=m+1;
}
int c=-1;
for(i=1;i<=n;i++)
{
if(cc=b[i];
}
printf("%d\n",c);
return 0;
}
1730
#include
int main()
{
int n,m,j,i,k;
int a[110][110],b[110][110];
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
scanf("%d",&a[i][j]);
for(i=1;i<=n;i++)
b[n][i]=a[n][i];
for(i=n;i>1;i--)
{
for(j=1;jif(b[i][j]>b[i][j+1])
b[i-1][j]=b[i][j]+a[i-1][j];
else
b[i-1][j]=b[i][j+1]+a[i-1][j];
}
printf("%d\n",b[1][1]);
return 0;
}
1491
#include
#include
struct node
{
char a[11];
};
int main()
{
char b[22],d[22],y[22];
struct node c[10001];
int n,m,i;
while(scanf("%d",&n)!=EOF&&(n||n))
{
m=0;
for(i=0;i
scanf("%s",b);
scanf("%s",d);
strcpy(y,b);
strcat(b,d);
strcat(d,y);
for(i=0;i
{
m=1;
break;
}
if(m==1)
printf("yes\n");
else
printf("No\n");
}
return 0;
}