程序设计竞赛(初赛)(参考答案)
2003年6月26日
/*1-A-1*/
void my_strcat(char *str1,char *str2)
{ char *p=str1;
while(*p) p++;
while(*p++=*str2++);
}
main()
{ char a[80],b[80],*p=a;
int i=0;
gets(a);
while(*p) p++;
*p=' ';
*(p+1)='\0';
p=a;
/*while(*p>='0'&&*p<='9') */
while(i<8)
{ b[i]=*p;
p++; i++;
}
b[i]='\0';
my_strcat(p,b);
puts(p);
}
/*1-A-1*/
main()
{ char a[80],b[80];
int i,j=0;
gets(a);
for(i=8;a[i];i++)
b[j++]=a[i];
b[j++]=’ ’;
for(i=0;i<8;i++)
b[j++]=a[i];
b[j]=’\0’;
puts(b);
}
/*1-A-2*/
main()
{ int a,s=0;
for(a=0;a<=1000;a++)
if(a%7==0&&a%13==0)
s=s+a;
printf("%d\n",s);
}
/*1-A-3*/
#include <math.h>
double fun(double x)
{ return x*x-sin(x); }
main()
{ double x,x1,x2,y,y1,y2; int i=0;
printf("input x1, x2:");
scanf("%lf%lf", &x1, &x2);
y1=fun(x1); y2=fun(x2);
printf("y1=%lf, y2=%lf\n", y1, y2);
do{ x=(x1+x2)/2; y=fun(x);
if(y*y1>0) x1=x;
else x2=x;
i++;
if(!i)
{ printf("no result. i>32767.\n"); break; }
} while(fabs(x1-x2)>1.e-4);
if(i)
printf("%lf\n", x2);
}
/*1-B-1*/
void my_strcat(char *str1,char *str2)
{ char *p=str1;
while(*p) p++;
while(*p++=*str2++);
}
main()
{ char a[80],b[80],*p=a;
int i=0;
gets(a);
while(*p) p++;
*p='*';
*(p+1)='*';
*(p+2)='\0';
p=a;
while(!(*p>='0'&&*p<='9'))
{ b[i]=*p;
p++; i++;
}
b[i]='\0';
my_strcat(p,b);
puts(p);
}
/*1-B-1*/
#include “string.h”
main()
{ char a[80],b[80];
int i,j=0;
gets(a);
for(i=strlen(a)-8;a[i];i++)
b[j++]=a[i];
b[j++]=’*’;
b[j++]=’*’;
for(i=0;i< strlen(a)-8;i++)
b[j++]=a[i];
b[j]=’\0’;
puts(b);
}
/*1-B-2*/
int prime(int m)
{ int i;
if(m<1) { printf("input error.\n"); return 0; }
if(m==1) return 0;
for(i=2;i<=m/2;i++)
if(m%i==0) break;
if(i>m/2) return 1;
else return 0;
}
main()
{ int a[10],i,s=0;
for(i=0;i<10;i++) scanf("%d",a+i);
for(i=0;i<10;i++)
if(prime(a[i])) s+=a[i];
printf("%d\n",s);
}
/*1-B-3*/
#include <math.h>
double fun(double x)
{ return x*x*x-2*x+1; }
double der(double x)
{ return 3*x*x-2; }
main()
{ double x1,x2,y1,y2;
int i=0;
printf("input a approximate root.\n");
scanf("%lf",&x2);
do{ x1=x2;
y1=fun(x1);
x2=x1-y1/der(x1);
++i;
} while(fabs(x1-x2)>1.e-4);
printf("%lf\n",x2);
}
/*2-A-1*/
void my_strcat(char *str1,char *str2)
{ char *p=str1;
while(*p) p++;
while(*p++=*str2++);
}
main()
{ char a[80],b[80];
gets(a);
gets(b);
my_strcat(a,b);
puts(a);
}
/*2-A-1*/
#include “string.h”
main()
{ char a[80],b[80];
gets(a);
gets(b);
strcat(a,b);
puts(a);
}
/*2-A-2*/
double my_pow(double x,int n)
{ double p=1;
int i;
for(i=1;i<=n;i++)
p=p*x;
return p;
}
main()
{ int n=5;
double x=7.01;
printf("%lf\n",my_pow(x,n));
}
/*2-A-3*/
#include <math.h>
double fun(double x)
{ return x*x*x-2*x+1; }
main()
{ double x,x1,x2,y,y1,y2; int i=0;
printf("input x1, x2:");
scanf("%lf%lf", &x1, &x2);
y1=fun(x1); y2=fun(x2);
printf("y1=%lf, y2=%lf\n", y1, y2);
do{ x=(x1+x2)/2; y=fun(x);
if(y*y1>0) x1=x;
else x2=x;
i++;
if(!i)
{ printf("no result. i>32767.\n"); break; }
} while(fabs(x1-x2)>1.e-4);
if(i)
printf("%lf\n", x2);
}
/*2-B-1*/
void my_strcat(char *str1,char *str2)
{ char *p=str1;
while(*p) p++;
while(*p++=*str2++);
}
main()
{ char a[80],b[80];
gets(a);
gets(b);
my_strcat(b,a);
puts(b);
}
/*2-B-1*/
#include “string.h”
main()
{ char a[80],b[80];
gets(a);
gets(b);
strcat(b,a);
puts(b);
}
/*2-B-2*/
#include <math.h>
main()
{ int a[10],i,jmax,b,c;
float error;
float av=0.,max;
for(i=0;i<10;i++)
{ scanf("%d",a+i);
av+=a[i];
}
av/=10;
max=fabs(a[0]-av); jmax=0;
for(i=1;i<10;i++)
{ error=fabs(a[i]-av);
if(error>max) { max=error; jmax=i; }
}
printf("%f,%d ",av+1,a[jmax]);
}
/*2-B-3*/
#include <math.h>
double fun(double x)
{ return 2*x-exp(-x); }
double der(double x)
{ return 2+exp(-x); }
main()
{ double x1,x2,y1,y2;
int i=0;
printf("input a approximate root.\n");
scanf("%lf",&x2);
do{ x1=x2;
y1=fun(x1);
x2=x1-y1/der(x1);
++i;
} while(fabs(x1-x2)>1.e-4);
printf("%lf\n",x2);
}
/*3-A-1*/
void my_strcat(char *str1,char *str2)
{ char *p=str1,*q;
while(*p)
p++;
while(*p++=*str2++);
}
main()
{ char s1[80],s2[80],*p,*q=s2;
gets(s1);
p=s1+8;
while(*q++=*p++);
s1[8]='\0';
my_strcat(s2,s1);
puts(s2);
}
/*3-A-1*/
main()
{ char s1[80],s2[80];
int i,j=0;
gets(s1);
for(i=8;a[i];i++)
s2[j++]=s1[i];
for(i=0;i<8;i++)
s2[j++]=s1[i];
s2[j]=’\0’;
puts(s2);
}
/*3-A-2*/
main()
{ int a[20],i,count=0,sum=0;
for(i=0;i<20;i++)
{ scanf("%d",a+i);
if(a[i]>=0)
{ count++;
sum+=a[i];
}
}
printf("%d,%d\n",count,sum);
}
/*3-A-3*/
#include <math.h>
double fun(double x)
{ return x*x-sin(x); }
double der(double x)
{ return 2*x-cos(x); }
main()
{ double x1,x2,y1,y2;
int i=0;
printf("input a approximate root.\n");
scanf("%lf",&x2);
do{ x1=x2;
y1=fun(x1);
x2=x1-y1/der(x1);
++i;
} while(fabs(x1-x2)>1.e-4);
printf("%lf\n",x2);
}
/*3-B-1*/
void my_strcat(char *str1,char *str2)
{ char *p=str1,*q;
while(*p)
p++;
while(*p++=*str2++);
}
main()
{ char s1[80],s2[80],*p=s1,*q=s2,*r;
gets(s1);
while(!(*p>='0'&&*p<='9')) p++;
r=p;
while(*q++=*p++);
*r='\0';
my_strcat(s2,s1);
puts(s2);
}
/*3-B-1*/
#include “string.h”
main()
{ char s1[80],s2[80];
int i,j=0;
gets(s1);
for(i=strlen(s1)-8;s1[i];i++)
s2[j++]=s1[i];
for(i=0;i< strlen(a)-8;i++)
s2[j++]=s1[i];
s2[j]=’\0’;
puts(s2);
}
/*3-B-2*/
#include <math.h>
main()
{ int a[10],i,imin;
float av=0,error,min;
for(i=0;i<10;i++)
{ scanf("%d",a+i);
av+=a[i];
}
av=av/10;
imin=0;
min=fabs(av-a[0]);
for(i=1;i<10;i++)
if(fabs(av-a[i])<min)
{ min=fabs(av-a[i]);
imin=i;
}
printf("%f,%d\n",av,a[imin]);
}
/*3-B-3*/
#include <math.h>
double fun(double x)
{ return 2*x-exp(-x); }
main()
{ double x,x1,x2,y,y1,y2; int i=0;
/*printf("input x1, x2:");*/
scanf("%lf%lf", &x1, &x2);
y1=fun(x1); y2=fun(x2);
/*printf("y1=%lf, y2=%lf\n", y1, y2);*/
do{ x=(x1+x2)/2; y=fun(x);
if(y*y1>0) x1=x;
else x2=x;
i++;
if(!i)
{ /*printf("no result. i>32767.\n");*/ break; }
} while(fabs(x1-x2)>1.e-4);
if(i)
printf("%lf\n", x2);
}