saowu's Blog

C语言程序集(61-75)

C语言程序集(61-75)
2020-04-03 · 10 min read
C 日记

61."ABCDEFG****"☞"ABCDEFG*****"


#include "stdio.h"
int main() {
    char s[]="****A*BC*DEF*G*****",*t=s;
    int i,j;
    while(*t) {
        t++;
    }
    t--;
    while(*t=='*') {
        t--;
    }
    for(i=0,j=0; &s[i]!=t; i++) {
        if(s[i]!='*')
            s[j++]=s[i];
    }
    while(*t) {
        s[j++]=*t++;
    }
    s[j]='\0';
    puts(s);
    return 0;
}

#include "stdio.h"
int main() {
    char s[]="****A*BC*DEF*G*****",*t=s;
    int i,j;
    while(*++t);
    while(*--t=='*');
    for(i=0,j=0; &s[i]!=t; i++) {
        if(s[i]!='*') s[j++]=s[i];
    }
    while(*t) {
        s[j++]=*t++;
    }
    s[j]='\0';
    puts(s);
    return 0;
}

62.二维数组的指针的运用

#include<stdio.h>
int main()
{   
    int m[4][3]= {{5,2,3},{4,5,6},{7,8,9},{10,11,12}};
    int i,j,*a=m,**p=&a,(*b)[3]=m;
    printf("%d\n",*(*(b+2)+0));
    printf("%d",*(*p+6));
    return 0;
}

63.库函数strcmp()

#include<string.h>
int strcmp ( char* src,char* dst)
{
    int ret = 0 ;
    while( !(ret = *src - *dst) && *dst)
        ++src, ++dst;
    if ( ret < 0 )
        ret = -1 ;
    else if ( ret > 0 )
        ret = 1 ;
    return( ret );
}

64.int **p双重指针

#include<stdio.h>
int main()
{
    int p[3][4]= {15,2,3,4,5,6,7,8,9};
    int *s=p;
    int **a=&s;
    printf("%d\n",p);
    printf("%d\n",s);
    printf("%d",a);
    return 0;
}

65.折半插入排序(自己实现)

1)升序
#include<stdio.h>
int insertsort(int a[],int n) {
    int bottom,mid,top,temp,i,j;
    for(i=1; i<n; i++) {
        //bottom值为0,top值为被排序值前一位
        bottom=0;
        top=i-1;
        while(bottom<=top) {
            mid=(bottom+top)/2;
            if(a[i]>a[mid])
                bottom=mid+1;
            else
                top=mid-1;
        } 
        //当前mid为最接近被排序值的位,如果被排序值大于mid。则mid++
        if(a[i]>a[mid])
            mid++;
        //mid以后到i-1整体后移一位,被排序值与a[mid]交换
        temp=a[i];
        for(j=i-1; j>=mid; j--)
            a[j+1]=a[j];
        a[mid]=temp;
        //每次排序都打印(此处理解用)
        printf("i=%d,mid=%d\n",i,mid);
        for(int k=0; k<n; k++)
            printf("%d ",a[k]);
        printf("\n");
    }
}
int main()
{   int x[]= {23,7,823,586,2,1,6,11};
    insertsort(x,sizeof(x)/4);
    return 0;
}

2)降序
#include<stdio.h>
int insertsort(int a[],int n) {
    int bottom,mid,top,temp,i,j;
    for(i=1; i<n; i++) {
        //bottom值为0,top值为被排序值前一位
        bottom=0;
        top=i-1;
        while(bottom<=top) {
            mid=(bottom+top)/2;
            if(a[i]>a[mid])
                top=mid-1;
            else
                bottom=mid+1;
            //当前mid为最接近被排序值的位,如果被排序值小于mid。则mid++
            if(a[i]<a[mid])
                mid++;
        }
        //mid以后i-1全体后移一位,被排序值与a[mid]交换
        temp=a[i];
        for(j=i-1; j>=mid; j--)
            a[j+1]=a[j];
        a[mid]=temp;
        //每次排序都打印(此处理解用)
        printf("i=%d,mid=%d\n",i,mid);
        for(int k=0; k<n; k++)
            printf("%d ",a[k]);
        printf("\n");
    }
}
int main() {
    int x[]= {23,7,823,586,2,1,6,11};
    insertsort(x,sizeof(x)/4);
    return 0;
}

3)老师实现(哨兵位)
#include<stdio.h>
int insertsort(int a[],int n) {
    int bottom,mid,top,i,j;
    for(i=2; i<=n; i++) {
        //a[0]为空闲,作为哨兵是temp的作用,不参与排序
        a[0]=a[i];
        bottom=1;
        top=i-1;
        while(bottom<=top) {
            mid=(bottom+top)/2;
            if(a[0]>a[mid])
                bottom=mid+1;
            else
                top=mid-1;
        }
        for(j=i-1; j>=top+1; j--)
            a[j+1]=a[j];
        a[top+1]=a[0];
        //每次排序都打印(从a[1]打印到a[n])
        printf("\n");
        for(int k=1; k<n+1; k++)
            printf("%d ",a[k]);
        printf("\n");
    }
}
int main() {
    int x[]= {0,23,7,823,586,2,1,6,11};
    insertsort(x,sizeof(x)/4);
    return 0;
}

66.求自然对数e=1+1/1!+1/2!+…+1/n!

#include <stdio.h>
int main()
{
    long n = 0, ns = 1;
    double x=0.0, e = 1.0;
    while(1) {
        n++;
        ns *= n;
        x = 1.0 / ns;
        if (x < 1e-8)
            break;
        e += x;
    }
    printf("%9.10f\n", e);
    return 0;
}

67.顺序插入排序法

#include "stdio.h"
int main() {
    int c[]= {23,1,56,234,7,0,34},i,j,t;
    for(i=1; i<7; i++) {
        t=c[i];
        j=i-1;
        while(j>=0 && t>c[j]) {
            c[j+1]=c[j];
            j--;
        }
        c[j+1]=t;
    }
    for(i=0; i<7; i++)
        printf("%d ",c[i]);
    putchar('\n');
}

68.一个球自由100m落下,每次反弹一半,问第10次落地经过多少m,最后一次反弹多高


#include<stdio.h>
int fun(float m,int n) {
    if(n==0) {
        printf("m=%f\n",m);
        return 0;
    } else {
        return fun(m/2,n-1)+m;
    }
}
int main()
{
    int m,n,c;
    scanf("%d,%d",&m,&n);
    c=fun(m,n)+m;
    printf("c=%d",c);
    return 0;
}

69.计算字符串长度

#include<stdio.h>
int len(char * s) {
    char * p=s;
    while(*p++);
    return(p-s);
}
int main()
{
    int i=len("123456");
    printf("%d",i);
    return 0;
}

70. 库函数strstr()源码

#include<stdio.h>
char *strstr (char *str1,char *str2) {
    char *cp = str1;
    char *s1, *s2;
    if ( !*str2 )
        return(str1);
    while (*cp) {
        s1 = cp;
        s2 = str2;
        while ( *s1 && *s2 && !(*s1-*s2) )
            s1++, s2++;
        if (!*s2)
            return(cp);
        cp++;
    }
    return(NULL);
}
int main()
{
    char *p=strstr("a12345633258","56");
    printf("%s",p);
    return 0;
}

71.库函数strchr()源码

#include<stdio.h>
char *strchr (char *string,int ch) {
    while (*string && *string != (char)ch)
        string++;
    if (*string == (char)ch)
        return(string);
    return(NULL);
}
int main()
{
    char *p=strchr("123",'3');
    printf("%c",*p);
    return 0;
}

72.实型比较大小(因为double和float精度不同所以直接判断有可能会判断失败)


#include<stdio.h>
#include<math.h>
int main()
{   double a=1.0000000000000001;
    float b=1.0000000001;
    if(fabs(a-b)>1e-8)
        printf("a>b");
    else
        printf("a<b");
    return 0;
}

73.读程序结果


#include<stdio.h>
int main()
{   //四舍五入
    double x =1.234;
    printf("%.2f",x);
    return 0;
}

74.求π的近似值,不同的方法近似度不同(课本p131)


第①种  π/4=...

#include<stdio.h>
#include<math.h>
double pi(double eps)
{
    double t=1.0,s=0.0;
    int n=1,i=0;

    while(fabs(t)>=eps) {
        t=pow(-1,i)/(2*n-1);
        s+=t;
        i++;
        n++;
    }
    return s*4;
}
int main()
{
    //eps为精度控制
    double eps=1e-6;
    printf("%.6f\n",pi(eps));
    return 0;
}

#include <stdio.h>
#include <math.h>
int main(){
    float s=1;
    float pi=0;
    float i=1.0;
    float n=1.0;
    while(fabs(i)>=1e-6){
        pi+=i;
        n=n+2;
        s=-s; 
        i=s/n;
    }
    pi=4*pi;
    printf("pi的值为:%.6f\n",pi);
    return 0;
}

第②类 π/2=1+1/3+1/3*2/5+1/3*2/5*3/7+1/3*2/5*3/7*4/9+...

#include<stdio.h>
double pi(double eps)
{
    double s=0.0,t=1.0;
    int n;
    for(n=1; t>=eps; n++) {
        s+=t;
        t=n*t/(2*n+1);
    }
    return 2*s;
}
int main()
{
    //eps为精度控制
    double eps=1e-6;
    printf("%0.6f\n",pi(eps));
    return 0;
}

第③种  π²/6=...  

#include<stdio.h>
#include<math.h>
double pi(double eps) {
    double s=0.0,t=1.0,n;
    for(n=1; t>=eps; n++) {
        t=1/(n*n);
        s+=t;
    }
    return sqrt(6*s);
}
int main() {
    //eps为精度控制
    double eps=1e-6;
    printf("%0.6f\n",pi(eps));
    return 0;
}

75.读程序结果

#include <stdio.h>
#define PR(ar) printf("%d ",ar);
main()
{
    int j, a[] = { 1, 3, 5, 7, 9, 11, 15 }, *p = a + 5;
    for (j = 3; j; j--)
        switch (j)
        {
        case 1:
        case 2:
            //*号的优先级很低
            PR(*p++);
            break;
        case 3:
            PR(*(--p));
        }
    printf("\n");
}
Copyright © 2020 - 2024 saowu. All Right Reserved
Powered by Gridea