第六十一套:
61.1:计算数学公式
double fun(double e)
{
int i, k; double s, t, x;
s=0; k=1; i=2;
/**********found**********/
x=3.0/4;
/**********found**********/
while(x > e)
{ s=s+k*x;
k=k* (-1);
t=2*i;
/**********found**********/
x=(t+1)/(t*t);
i++;
}
return s;
}
61.2:求分数序列之和
double fun ( int n )
{
int a, b, c, k; double s;
s = 0.0; a = 2; b = 1;
for ( k = 1; k <= n; k++ ) {
/************found************/
s = s + (double)a / b;
c = a; a = a + b; b = c;
}
return s;
}
61.3:找出二维数组中的最大元素
int fun (int a[][M])
{
int i,j,max;
max=a[0][0];
for(i=0;i<2;i++)
for(j=0;j<M;j++)
if(max<a[i][j])
max=a[i][j];
return max;
}
第六十二套:
62.1:计算数学公式
double fun(int n)
{
int i; double s, t;
/**********found**********/
s=0.0;
/**********found**********/
for(i=1; i<=n; i++)
{ t=2.0*i;
/**********found**********/
s=s+(2.0*i-1)*(2.0*i+1)/(t*t);
}
return s;
}
62.2:统计子串出现的次数
int fun (char *str,char *substr)
{
int i,j,k,num=0;
/************found************/
for(i = 0; str[i]; i++)
for(j=i,k=0;substr[k]==str[j];k++,j++)
/************found************/
if(substr[k+1]=='\0')
{ num++;
break;
}
return num;
}
62.3:—计算数学公式(求π值)—
double fun ( double eps)
{
int n=1;
double s=0.0,s1=1.0;
while(s1>=eps)
{
s+=s1;
s1=s1*n/(2*n+1);
n++;
}
return 2*s;
}
第六十三套:
63.1:—统计数字在串中出现的次数—
void fun(char *s, int *t)
{
int i, n;
n=0;
/**********found**********/
for(i=0; s[i] !=0; i++)
/**********found**********/
if(s[i]>='0'&&s[i]<= '9' ) n++;
/**********found**********/
*t=n ;//不能为t[n]=s[i]!
}
63.2:—交换数—
int fun(int *x,int y)
{
int t ;
/**************found**************/
t = *x ; *x = y ;
/**************found**************/
return(t) ;
}
63.3:找出特殊整数
void fun (int *a, int *n)
{
int i,j=0;
for(i=1;i<=1000;i++)
if((i%7==0||i%11==0)&&(i%77))
a[j++]=i;
*n=j;
}
第六十四套:
64.1:删除数组中的偶数
int fun(int a[], int n)
{
int i,j;
j = 0;
for (i=0; i<n; i++)
/**********found**********/
if (a[i]%2==1)
{
/**********found**********/
a[j] = a[i];j++;
}
/**********found**********/
return j;
}
64.2:—求两数的最大公约数—
int fun(int a,int b)
{
int r,t;
if(a<b) {
/************found************/
t=a; a=b; b=t;
}
r=a%b;
while(r!=0)
{ a=b; b=r; r=a%b; }
/************found************/
return(b);
}
64.3:删除串中的*
void fun( char *a )
{
int i,j=0;
for(i=0;a[i]!='\0';i++)
if(a[i]!='*')
a[j++]=a[i];
a[j]='\0';
}
第六十五套:
65.1:取出偶数,逆序排序
unsigned long fun(unsigned long n)
{
unsigned long x=0; int t;
while(n)
{ t=n%10;
/**********found**********/
if(t%2==0)
/**********found**********/
x=x*10+t;
/**********found**********/
n=n/10;
}
return x;
}
65.2:抽出奇数,并重组
void fun (long s, long *t)
{
int d;
long sl=1;
/************found************/
*t = 0;
while ( s > 0)
{ d = s%10;
/************found************/
if (d%2 != 0)
{ *t = d * sl + *t;
sl *= 10;
}
s /= 10;
}
}
65.3:—拼接字符串—
void fun(char p1[], char p2[])
{
int i,j=0;
for(i=0;p1[i]!='\0';i++);
for(j=0;p2[j]!='\0';j++)
p1[i++]=p2[j];
p1[i]='\0';
}
第六十六套:
66.1:—重组小大排列—
void fun(int a[], int n)
{
int i,j, max, min, px, pn, t;
for (i=0; i<n-1; i+=2)
{
/**********found**********/
max = min = a[i];//这里不可以是a[0],注意其含义!!!
px = pn = i;
for (j=i+1; j<n; j++) {
/**********found**********/
if (max<a[j])
{ max = a[j]; px = j; }
/**********found**********/
if (min>a[j])
{ min = a[j]; pn = j; }
}
if (pn != i)
{ t = a[i]; a[i] = min; a[pn] = t;
if (px == i) px =pn;
}
if (px != i+1)
{ t = a[i+1]; a[i+1] = max; a[px] = t; }
}
}
66.2:Fibonacca序列
long fun(int g)
{
/**********found**********/
switch(g)
{ case 0: return 0;
/**********found**********/
case 1 :
case 2 : return 1 ;
}
return( fun(g-1)+fun(g-2) );
}
66.3:—求出学生成绩的平均分—
typedef struct
{ char num[10];
double s[N];
double ave;
} STREC;
void fun(STREC *a)
{
int i;
a->ave=0.0;
for(i=0;i<N;i++)
a->ave+=a->s[i]/N;
}
第六十七套:
67.1:数字字符变相转化
char fun(char ch)
{
/**********found**********/
if (ch>='0' && ch<='9')
/**********found**********/
return '9'- (ch-'0');
return ch ;
}
67.2:每复制三个字符插入一个空格
void fun(char *p, char *b)
{
int i, k=0;
while(*p)
{ i=1;
while( i<=3 && *p ) {
/**********found**********/
b[k]=*p;
k++; p++; i++;
}
if(*p)
{
/**********found**********/
b[k++]=' ';
}
}
b[k]='\0';
}
67.3:—求数据平均分—
struct slist
{ double s;
struct slist *next;
};
typedef struct slist STREC;
double fun( STREC *h )
{
double ave=0.0;
STREC *p=h->next;
while(p!=NULL)
{
ave+=p->s/N;
p=p->next;
}
return ave;
}
第六十八套:
68.1:找出最短字符串(行下标)
int fun(char (*ss)[N], int *n)
{
int i, k=0, len= N;
/**********found**********/
for(i=0; i<M; i++)
{ len=strlen(ss[i]);
if(i==0) *n=len;
/**********found**********/
if(len < *n)
{ *n=len;
k=i;
}
}
/**********found**********/
return(k);
}
68.2:将串中小写字母转化为大写
char* fun( char tt[] )
{
int i;
for( i = 0; tt[i]; i++ )
/**********found***********/
if(( 'a' <= tt[i] )&&( tt[i] <= 'z' ) )
/**********found***********/
tt[i] -= 32;
return( tt );
}
68.3:非素数
void fun( int m, int *k, int xx[] )
{
int i,j,n=0;
for(i=4;i<=m;i++)
{
for(j=2;j<i;j++)
if(i%j==0)
break;
if(j<i)
xx[n++]=i;
}
*k=n;
}
第六十九套:
69.1:将数字字符移到串的尾部
void fun(char *s)
{
int i, j=0, k=0; char t1[80], t2[80];
for(i=0; s[i]!='\0'; i++)
if(s[i]>='0' && s[i]<='9')
{
/**********found**********/
t2[j]=s[i]; j++ ;
}
else t1[k++]=s[i];
t2[j]=0; t1[k]=0;
/**********found**********/
for(i=0; i<k; i++) s[i]=t1[i];
/**********found**********/
for(i=0; i<j; i++) s[k+i]=t2[i];
}
69.2:用冒泡对串进行升序排
void fun ( char *pstr[6])
{
int i, j ;
char *p ;
for (i = 0 ; i < 5 ; i++ ) {
/**************found**************/
for (j = i+1; j < 6; j++)
{
if(strcmp(*(pstr + i), *(pstr + j)) > 0)
{
p = *(pstr + i) ;
/**************found**************/
*(pstr + i) = *(pstr + j) ;
*(pstr + j) = p ;
}
}
}
}
69.3:—找出串中指定字符的个数—
int fun(char *ss, char c)
{
int i=0;
for(;*ss!='\0';ss++)
if(*ss==c)
i++;
return i;
}
第 七十套:
70.1:—将数据插入有序列表中—
typedef struct list
{ int data;
struct list *next;
} SLIST;
void fun( SLIST *h, int x)
{
SLIST *p, *q, *s;
s=(SLIST *)malloc(sizeof(SLIST));
/**********found**********/
s->data=x;
q=h;
p=h->next;
while(p!=NULL && x>p->data) {
/**********found**********/
q=p;
p=p->next;
}
s->next=p;
/**********found**********/
q->next=s;
}
70.2:对数各位求积
long fun (long num)
{
/************found************/
long k=1;
do
{ k*=num%10 ;
/************found************/
num/=10 ;
} while(num) ;
return (k) ;
}
70.3:计算课程平均分
float fun ( float *a , int n )
{
int i;
float av=0.0;
for(i=0;i<n;i++)
av+=a[i]/n;
return av;
}
第七十一套:
71.1: 将数组中的前半部分和后半部分调换
void fun(int a[], int n)
{
int i, t, p;
/**********found**********/
p = (n%2==0)?n/2:n/2+1;
for (i=0; i<n/2; i++)
{
t=a[i];
/**********found**********/
a[i] = a[p+i];
/**********found**********/
a[p+i] = t;
}
}
71.2:三个数的顺序
void fun(float *a,float *b,float *c)
{
/**********found**********/
float k;
if( *a<*b )
{ k=*a; *a=*b; *b=k; }
/**********found**********/
if( *a<*c )
{ k=*c; *c=*a; *a=k; }
if( *b<*c )
{ k=*b; *b=*c; *c=k; }
}
71.3:统计最高分数据
typedef struct
{ char num[10];
int s;
} STREC;
int fun( STREC *a, STREC *b )
{
int i,j=0,max=a[0].s ;
for(i=0;i<N;i++)
if(max<=a[i].s)
max=a[i].s;
for(i=0;i<N;i++)
if(max==a[i].s)
b[j++]=a[i];
return j;
}
第七十二套:
72.1:在串组中选择需要的
int fun(char (*ss)[M], int k)
{
int i,j=0,len;
/**********found**********/
for(i=0; i< N ; i++)
{ len=strlen(ss[i]);
/**********found**********/
if(len<=k )
/**********found**********/
strcpy(ss[j++],ss[i]);
}
return j;
}
72.2:比较两个串,选择性重组新串
void fun(char *p ,char *q, char *c)
{
/************found************/
int k = 0;
/************found************/
while( *p || *q )
{ if( *p<*q ) c[k]=*q;
else c[k]=*p;
if(*p) p++;
if(*q) q++;
k++;
}
}
72.3:—仅保留前导*—串的经典问题4.0
void fun( char *a )
{
int i=0;
char *t=a;
while(*t=='*')
{
a[i++]=*t;
t++;
}
while(*t)
{
if(*t!='*')
a[i++]=*t;
t++;
}
a[i]='\0';
}
第七十三套:
73.1:—倍数筛选法得出素数序列—
int fun(int n)
{
int a[10000], i,j, count=0;
for (i=2; i<=n; i++) a[i] = i;
i = 2;
while (i<n) {
/**********found**********/
for (j=a[i]*2; j<=n; j+=a[i])
a[j] = 0;
i++;
/**********found**********/
while (a[i]==0)
i++;
}
printf("\nThe prime number between 2 to %d\n", n);
for (i=2; i<=n; i++)
/**********found**********/
if (a[i]!=0)
{ count++; printf( count%15?"%5d":"\n%5d",a[i]); }
return count;
}
73.2:—为一偶数寻找两个素数,素数之和为这一偶数(对歌德巴赫猜想的验证)—
void fun(int a, int *b, int *c)
{
int i,j,d,y;
for (i=3;i<=a/2;i=i+2)
{
/*************found**************/
y=1;
for (j=2;j<=sqrt((double)i );j++)
if (i%j==0) y=0;
if (y==1)
{
/*************found**************/
d=a-i;
for (j=2;j<=sqrt((double)d);j++)
if (d%j==0) y=0;
if (y==1)
{*b=i; *c=d;}
}
}
}
73.3:计算出能被特定数整除的自然数之和
double fun(int n)
{
int i;
double s=0.0;
for(i=5;i<=n;i++)
if((i%5==0)||(i%9==0))
s+=1.0/i;
return s;
}
第七十四套:
74.1:隔秒放鞭炮
int fun(int t1, int t2, int t3, int t4, int n)
{
int count, t , maxt=t1;
if (maxt < t2) maxt = t2;
if (maxt < t3) maxt = t3;
if (maxt < t4) maxt = t4;
count=1; /* 给count赋初值 */
/**********found**********/
for(t=1; t< maxt*(n-1); t++)
{
if(OK(t, t1, n) || OK(t, t2, n)|| OK(t, t3, n) || OK(t, t4, n) )
count++;
}
/**********found**********/
return count ;
}
74.2:判定三角形
int fun(int a,int b,int c)
{
if(a+b>c&&b+c>a&&a+c>b)
{if(a==b&&b==c)
/*************found**************/
return 3;
else if(a==b||b==c||a==c)
return 2;
/*************found**************/
else return 1;
}
else return 0;
}
74.3:范围内素数的平方根之和
double fun(int n)
{
int i,j;
double s=0.0;
for(i=3;i<=n;i++)
{
for(j=2;j<i;j++)
if(i%j==0)
break;
if(i==j)
s+=sqrt(1.0*i);
}
return s;
}
第七十五套:
75.1:—找出中间数—
int fun(int a,int b,int c)
{ int t;
t=(a>b)?(b>c?b:(a>c?c:a))
:((a>c)?a
:((b>c)?c:b));
return t;
}
75.2:字母分段转化
char fun(char c)
{
if(c>='A' && c<='Z')
/*************found**************/
c=c+32;
if(c>='a' && c<='u')
/*************found**************/
c=c+5;
else if(c>='v' && c<='z')
c=c-21;
return c;
}
75.3:计算数学公式
double fun(int n)
{
double s=1.0,s1=1.0;
for(int i=2;i<=n;i++)
{
s1+=pow(i,0.5);
s+=s1;
}
return s;
}