要点:
详细代码如下:
#include <stdio.h>
#define N 4
int fun(int t[][N], int n)
{ int i, sum;
/**********found**********/
sum = 0;
for(i=0; i<n; i++)
/**********found**********/
sum+=t[i][i] ;
for(i=0; i<n; i++)
/**********found**********/
sum+= t[i][n-i-1] ;
return sum;
}
void main()
{ int t[][N]={21,2,13,24,25,16,47,38,29,11,32,54,42,21,3,10},i,j;
printf("\nThe original data:\n");
for(i=0; i<N; i++)
{ for(j=0; j<N; j++) printf("%4d",t[i][j]);
printf("\n");
}
printf("The result is: %d",fun(t,N));
}
要点:
二分求解法
绝对误差不超过0.001 ,所以fabos(n-m)>0.001
详细代码如下:
#include <stdio.h>
#include <math.h>
double funx(double x)
{ return(2*x*x*x-4*x*x+3*x-6); }
double fun( double m, double n) //m ,n 必须一正一负
{
/************found************/
double r;
r=(m+n)/2;
/************found************/
while(fabs(n-m)>0.001)
{ if(funx(r)*funx(n)<0) m=r;
else n=r;
r=(m+n)/2;
}
return r;
}
void main( )
{ double m,n, root;
printf("Enter m n : \n"); scanf("%lf%lf",&m,&n);
root=fun( m,n );
printf("root = %6.3f\n",root);
}
要点:
三个while循环,分段存取
详细代码如下:
#include <stdio.h>
void fun( char *a, char *h,char *p )
{
int i = 0;
char *q = a;
while(q<h)
{
a[i] = *q;
q++;
i++;
}
while(q<p)
{
if(*q != '*')
{
a[i] = *q;
i++;
}
q++;
}
while(*q)
{
a[i] = *q;
i++;
q++;
}
a[i] = '\0';
}
void main()
{ char s[81],*t, *f;
void NONO ( );
printf("Enter a string:\n");gets(s);
t=f=s;
while(*t)t++;
t--;
while(*t=='*')t--;
while(*f=='*')f++;
fun( s , f,t );
printf("The string after deleted:\n");puts(s);
NONO();
}