中国石油大学华东C语言答案之函数.docx
《中国石油大学华东C语言答案之函数.docx》由会员分享,可在线阅读,更多相关《中国石油大学华东C语言答案之函数.docx(14页珍藏版)》请在冰豆网上搜索。
![中国石油大学华东C语言答案之函数.docx](https://file1.bdocx.com/fileroot1/2022-11/16/034e86c2-c052-4d77-a745-3ddc3d082f22/034e86c2-c052-4d77-a745-3ddc3d082f221.gif)
中国石油大学华东C语言答案之函数
第九章函数
9.1
#include
intfun(intx,inty);
intmain()
{
inta,b,c;
scanf("%d%d",&a,&b);
c=fun(a,b);
printf("c=%d\n",c);
return0;
}
intfun(intx,inty)
{
intm,n,k,l,ch;
m=x%10;n=x/10;
k=y%10;l=y/10;
ch=1000*m+100*k+10*n+l;
returnch;
}
9.2
#include
intfun(intx);
intmain()
{
inta;
scanf("%d",&a);
if(fun(a))printf("Prime\n");
elseprintf("NonPrime\n");
return0;
}
intfun(intx)
{
inta;
for(a=2;a{if(x%a==0)
return0;
}
if(a>=x)
return1;
}
9.3
#include
intfun(doubley,intm);
intmain()
{
doublex;
intn;
scanf("%lf%d",&x,&n);
printf("%lf\n",fun(x,n));
return0;
}
doublefun(doubley,intm)
{
inti,j=1;
doublek=1;
floats=1;
for(i=1;i<=m;i++)
{k=k*y;
j=j*i;
s=s+k/j;
}
returns;
}
9.4
#include
voidfun(intm,intn,intzz[]);
intmain()
{
intm,n,zz[1000];
scanf("%d%d",&m,&n);
fun(m,n,zz);
for(m=0;mprintf("%6d",zz[m]);
printf("\n");
return0;
}
voidfun(intm,intn,intzz[])
{
inti,j=0,k;
for(i=m+1;j{for(k=2;k<=i/2;k++)
{if(i%k==0)
break;
}
if(k>i/2)
{zz[j]=i;
j++;
}
}
}
9.5
#include
voidfun(charstr[],charch);
intmain()
{
charstr[80];
charch;
gets(str);
scanf("%c",&ch);
fun(str,ch);
printf("%s\n",str);
return0;
}
voidfun(charstr[],charch)
{
inti,j=0;
for(i=0;str[i]!
='\0';i++)
{if(str[i]==ch)
{for(j=i;str[j]!
='\0';j++)
str[j]=str[j+1];
i--;
}
}
}
9.6
#include
intfun(intx,inty);
intmain()
{
intm,n,s;
scanf("%d%d",&n,&m);
s=fun(n,m);
printf("%d\n",s);
return0;
}
intfun(intx,inty)
{
intm;
if(x<=y)
m=y+fun(x,y-1);
else
m=0;
returnm;
}
9.7
#include
intGCD(intx,inty);
intmain()
{
intn,m;
scanf("%d%d",&n,&m);
printf("%d\n",GCD(n,m));
return0;
}
intGCD(intx,inty)
{
intm,a;
if(y<=x&&x%y==0)
m=y;
if(y<=x&&x%y!
=0)
{a=x%y;
m=GCD(y,a);
}
if(y>x&&y%x==0)
m=x;
if(y>x&&y%x!
=0)
{a=y%x;
m=GCD(a,x);
}
returnm;
}
9.8
#include
intfun(intx);
intmain()
{
intn;
scanf("%d",&n);
printf("%d\n",fun(n));
return0;
}
intfun(intx)
{
intm;
if(x==1||x==2)
m=1;
if(x>2)
{if(x%2==1)
m=fun(x-1)+fun(x-2);
if(x%2==0)
m=fun(x-1)-fun(x-2);
}
returnm;
}
9.9
#include
voidfun(intx);
intmain()
{
inta,m[100],i;
scanf("%d",&a);
m[0]=a;
for(i=1;m[i-1]!
=0;i++)
scanf("%d",&m[i]);
for(i=0;m[i]!
=0;i++)
{if(m[i]%2==1)
printf("%disoddnumber!
\n",m[i]);
if(m[i]%2==0)
fun(m[i]);
}
}
voidfun(intx)
{
inti,n,j,k;
for(i=2;;i++)
{n=x-i;
for(k=2;k<=i/2;k++)
{
{if(i%k==0)
break;}
if(k>i/2)
break;
}
for(j=2;j<=n/2;j++)
{if(n%j==0)
break;
}
if(j>n/2)
break;
}
printf("%d=%d+%d\n",x,i,n);
}
9.10
#include
#include
doublefun(doublex);
intmain()
{doublea;
scanf("%lf",&a);
printf("%lf\n",fun(a));
return0;
}
doublefun(doublex)
{
intm;
doublen;
m=(int)(x*100+0.5);
n=m/100.0;
returnn;
}
9.11
#include
intfun(intx);
intmain()
{
intn;
scanf("%d",&n);
printf("Theresultis:
%d\n",fun(n));
return0;
}
intfun(intx)
{
intm[100],i,y=0,j;
for(i=0;x!
=0;i++)
{m[i]=x%10;
x=x/10;
}
for(j=i-1;j>=0;j--)
{if(m[j]%2==0)
y=y*10+m[j];
}
returny;
}
9.12
#include
doublefun(intx);
intmain()
{
intn;
doubles;
scanf("%d",&n);
s=fun(n);
printf("s=%f\n",s);
return0;
}
doublefun(intx)
{
inti,m=1;
doubley=1;
for(i=1;i<=x;i++)
{
m=m*i;
y=y+1.0/m;
}
returny;
}
9.13
#include
intDeposit(intx);
intWithdraw(intx,inty);
intmain()
{
intn,i,day;
intamount,dpt,wd;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&amount);
for(day=1;;day++)
{
dpt=Deposit(day);
wd=Withdraw(day,amount);
if(dpt-wd>0)
{
printf("%d天骗取现金%d元\n",day,dpt-wd);
break;
}
}
}
return0;
}
intDeposit(intx)
{
intm=1,i,n;
if(x==1)
m=1;
if(x>1)
{n=1;
for(i=2;i<=x;i++)
{n=2*n;
m=m+n;
}
}
returnm;
}
intWithdraw(intx,inty)
{
intm;
m=x*y;
returnm;
}
9.14
#include
voidfun(charm[]);
intmain()
{
chars[80];
gets(s);
fun(s);
puts(s);
return0;
}
voidfun(chars[])
{
charM[80],N[80];
inti,j=0,k=0;
for(i=0;s[i]!
='\0';i++)
{if(s[i]>='0'&&s[i]<='9')
{{M[j]=s[i];j++;}
M[j]='\0';
}
else
{{N[k]=s[i];k++;}
N[k]='\0';
}
}
for(i=0;N[i]!
='\0';i++)
s[i]=N[i];
for(j=i,k=0;M[k]!
='\0';j++,k++)
s[j]=M[k];
s[j]='\0';
}
9.15
#include
#defineM3
#defineN4
voidfun(intx[M][N]);
intmain()
{
intx[M][N],i,j;
for(i=0;ifor(j=0;jscanf("%d",&x[i][j]);