C语言谭浩强(第二版)课后答案第八章

8.1

int yue(int a,int b)

{

         int t;

         if(a<b)

         {t=a;a=b;b=t;}

         while(a%b!=0)

    {

             t=a%b;

             a=b;b=t;

         }

         return(b);

}

int bei(int a,int b,int c)

{

         int t;

         t=a*b/c;

         returnt;

}

int main(int argc, char *argv[]) {

int a,b,c,d;

printf("输入要求的数:\n");

scanf("%d,%d",&a,&b);

c=yue(a,b);

d=bei(a,b,c);

printf("最大公约数:%d\n",c);

printf("最小公倍数:%d\n",d);

}

8.2

略,书上有

8.3

略,书上有

8.4

int convert(int a[3][3])

{

         inti,j,b[3][3];

         for(i=0;i<3;i++)

         for(j=0;j<3;j++)

         b[j][i]=a[i][j];

         for(i=0;i<3;i++)

         for(j=0;j<3;j++)

         a[i][j]=b[i][j];

}

int main(int argc, char *argv[]) {

int i,j;

int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};

convert(a);

         for(i=0;i<3;i++)

         for(j=0;j<3;j++)

         printf("%d",a[i][j]);

         if(j%3==0)printf("\n");

}

8.8

int main(int argc, char *argv[]) {

int a,t,b,m,n;

scanf("%d",&a);

 t=a/1000;

 printf("%d",t);

 b=(a-t*1000)/100;

 printf("%d ",b);

 m=(a-1000*t-100*b)/10;

   printf("%d",m);

   n=a%10;

   printf("%d",n);

}

8.10

int word(char c)

{int flag;

         if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))return1;

         elsereturn 0;

}

char longist(char a[])

{

         inti,point,len=0,maxlen=0,maxpoint,flag=1;

         for(i=0;i<strlen(a);i++)

         {

         if(word(a[i]))

                  if(flag==1)

                  {

                          point=i;

                          flag=0;

                  }

                  else

                  len++;

         else

         {

                  flag=1;

                  if(len>maxlen)

                  {

                          maxlen=len;

                          maxpoint=point;

                  }

                  len=0;              

    }

         returnmaxpoint;

}

}

char a[81];

int i;

gets(a);

for(i=longist(a);word(a[i]);i++)

printf("%c",a[i]);

8.11

int main(int argc, char *argv[]) {

int paixu(int a[]);

int i,a[10]={1,4,6,8,3,5,2,9,7,10};

paixu(a);

for(i=0;i<10;i++)

printf("%d",a[i]);

}

int paixu(int a[])

{

         inti,j,t;

         for(i=0;i<10;i++)

         for(j=0;j<10-i-1;j++)

         {

                  if(a[j]>a[j+1])

                  {

                     t=a[j];a[j]=a[j+1];a[j+1]=t;     

                  }

         }

}

8.12

牛顿迭代法原理就是:给定一个初始值x0,做一条垂线与函数f(x)相交,得到交点(x0,y0),过该点做f(x)上的一条切线,得到该切线与x轴的交点(x1,0)之后对(x1,0)重复上述步骤,直到与x轴交点的横坐标逐渐收敛到f(x)=0.

int main(int argc, char *argv[]) {

         doublediedai(double a);

         doublex=1,t;

         t=diedai(x);

         printf("%lf",t);

}

double diedai(double x)

{

         doublex0,f,f1;

         do

         {

                  x0=x;

                  f=x0*x0*x0+2*x0*x0+3*x0+4;

                  f1=3*x0*x0+4*x0+3;

                  x=x0-f/f1;

         }while(fabs(x-x0)>1e-5);

         returnx;

}

8.13

int main(int argc, char *argv[]) {

long duoxiang(long n,long x);

long x=3,n;

scanf("%d",&n);

printf("%d",duoxiang(n,x));

}

long duoxiang(long n,long x)

{

         long p;

         if(n==0)p=1;

         elseif(n==1)p=x;

         elseif(n>=1)

         {

                  p=((2*n-1)*x-duoxiang(n-1,x)-(n-1)*duoxiang(n-2,x))/(n*1.0);

         }

         returnp;

}

8.14

int main(int argc, char *argv[]) {

float ave(int a[][5]);

float danke(int a[][5]);

int max(int a[][5]);

inta[5][5]={{61,57,89,90,99},{85,83,87,89,96},{88,76,95,46,91},{88,69,92,93,94},{56,89,68,78,94}};

int i,j;

ave(a);

printf("\n\n");

danke(a);

printf("\n");

max(a);

}

float ave(int a[][5])

{

         int i,j;

         floatt=0,p=0,q=0;

         for(i=0;i<5;i++)

         {

         for(j=0;j<5;j++)

         t=t+a[i][j]/5.0;

         printf("第%d个学生平均分是:%.2f\n",i+1,t);

         p=p+t*t;q=q+(t/5)*(t/5);

         t=0;

         }

         printf("\n");

         printf("平均分方差:%.2f",p/5-q);

}

float danke(int a[][5])

{

         int i,j;

         floatt=0;

         for(i=0;i<5;i++)

         {

         for(j=0;j<5;j++)

         t=t+(a[j][i]/5.0);

         printf("单科%d个学生平均分是:%.2f\n",i+1,t);

         t=0;

         }

}

int max(int a[][5])

{

         inti,j,max=0,p,t;

         for(i=0;i<5;i++)

         for(j=0;j<5;j++)

                  if(max<a[i][j])

                  {

                          max=a[i][j];

                          p=i;t=j;

                  }

         printf("最高分%d是第%d人的第%d学科",max,p+1,t+1);  

}

8.16

int main(int argc, char *argv[]) {

int convert(int a,char *p);

char a[22];

gets(a);

int b=strlen(a);

printf("%d",convert(b,a));  

}

int convert(int a,char *p)//a为字符串长度,p为需要转换字符串指针

 int i,j,sum;

 

 sum=0;

 for(i=0;i<a;i++)

 {  

 if(*(p+i)<='f'&&*(p+i)>='a') 

  j=(int)(*(p+i))-87; 

  elseif(*(p+i)<='F'&&*(p+i)>='A') 

  j=(int)(*(p+i))-55;

  else   

  j=(int)(*(p+i))-48; 

 sum=sum+pow(16.0,a-i-1)*j;

 

 }

 return (sum);

}

8.17

char N2Char(int n) 

    int i;  

    char flag ='a'; 

    char c; 

    if((i=n/10)!=0) 

       N2Char(i); 

    c = n%10 +'0'; 

   printf("%c", c); 

    returnflag; 

}

int main(int argc, char *argv[]) {

      intn; 

       printf("请输入一个整数:\n"); 

       scanf("%d", &n); 

        if(n<= 0) 

        { 

           printf("抱歉,您输入的值有误!\n请输入一个大于零的数:\n"); 

           scanf("%d", &n); 

        } 

       printf("将整数转换为字符串为:\n"); 

       if(N2Char(n) == 'a') 

               printf("转换成功!"); 

        return0;  

}

8.19

 

int main(int argc, char *argv[]) {

int nian(int n);

int year,m,month,day,t=0,i;

int a[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};

int b[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

scanf("%d,%d,%d",&year,&month,&day);

printf("%d,%d,%d",year,month,day);

m=nian(year);

if(m)

{

         for(i=0;i<month;i++)

                  t=t+a[i];

         printf("这是一年中的第%d天",t+day);

}

else

{

                  for(i=1;i<month;i++)

                      t=t+b[i];

         printf("这是一年中的第%d天",t+day);  

}

}

 

int nian(int n)

{

int t;

if(n%4==0)

{

         if(n%100==0)

         {

                  if(n%400==0)t=1;

                  elset=0;

         }

         elset=1;

}

else t=0;

return t;

}
展开阅读全文

没有更多推荐了,返回首页